I have this code block, where I search for the value of Ordnungsbegriff
. I have to search through a tree of my CurrentDocument.
How can I rewrite it to be more dynamic? I don't know at which level I will find my value.
string xy = "";
try
{
if (string.IsNullOrEmpty(CurrentDocument.Ordnungsbegriff))
{
if (string.IsNullOrEmpty(CurrentDocument.ParentDocument.Ordnungsbegriff))
{
if (string.IsNullOrEmpty(CurrentDocument.ParentDocument.ParentDocument.Ordnungsbegriff))
{
if (string.IsNullOrEmpty(CurrentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff))
{
if (string.IsNullOrEmpty(CurrentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff))
{
if (string.IsNullOrEmpty(CurrentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff))
{
if (string.IsNullOrEmpty(CurrentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff))
{
xy = "Nach 6. Ebene, Suche abgebrochen.";
}
else
xy = CurrentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff;
}
else
xy = CurrentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff;
}
else
xy = CurrentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff;
}
else
xy = CurrentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff;
}
else
xy = CurrentDocument.ParentDocument.ParentDocument.Ordnungsbegriff;
}
else
xy = CurrentDocument.ParentDocument.Ordnungsbegriff;
}
else
xy = CurrentDocument.Ordnungsbegriff;
}
catch (Exception)
{
xy = "Fehler in Suche";
}
-
\$\begingroup\$ Welcome to CodeReview. What exactly do you mean by "I don't know at which level I find my value." Is this your code? \$\endgroup\$Legato– Legato2015年03月26日 12:09:59 +00:00Commented Mar 26, 2015 at 12:09
-
\$\begingroup\$ Yes, this is my code. I have CurrentDocument.Ordnungsbegriff, if this is null then i search at CurrentDocument.ParentDocument.Ordnungsbegriff and so on, until i find a value for Ordnungsbegriff \$\endgroup\$Al Salvador– Al Salvador2015年03月26日 12:27:39 +00:00Commented Mar 26, 2015 at 12:27
3 Answers 3
I find recursion complicated You can use a while loop:
private string SearchDocument(Document doc)
{
while (doc != null)
{
if (!string.IsNullOrEmpty(doc.Ordnungsbegriff))
{
return doc.Ordnungsbegriff;
}
doc = doc.ParentDocument;
}
return null;
}
Edit: As a further point - it's better not to mix languages in your code: Ordnungsbegriff
should be Keyword
(according to Google).
-
2\$\begingroup\$ A very nice alternative. I hadn't considered an iterative approach. \$\endgroup\$RubberDuck– RubberDuck2015年03月26日 12:33:53 +00:00Commented Mar 26, 2015 at 12:33
Recursion. You need some recursion friend.
Create a function that returns the value if it's found or calls itself on the next level if it's not found. This may not be a 100% working solution, because I don't have quite enough context for that, but it should give you a good start.
private string SearchDocument(Document doc)
{
if (doc == null)
{
return "Nach 6. Ebene, Suche abgebrochen.";
}
if (string.IsNullOrEmpty(doc.Ordnungsbegriff))
{
return SearchDocument(doc.ParentDocument);
}
return doc.Ordnungsbegriff
}
If you don't know much (or anything) about recursion, I recommend this video from Computerphile. It's a very good primer on the topic.
-
1\$\begingroup\$ Have you ever read the little schemer? It blew my mind then I forgot most of it but it's the closest I've ever come to being comfortable with recursion! \$\endgroup\$RobH– RobH2015年03月26日 12:36:25 +00:00Commented Mar 26, 2015 at 12:36
-
\$\begingroup\$ No, but I'm about to @RobH. =) It took me a while to get comfortable with recursion myself, but once you get it you get it. I really do recommend the video linked in my answer. It's a very good explination. \$\endgroup\$RubberDuck– RubberDuck2015年03月26日 12:38:23 +00:00Commented Mar 26, 2015 at 12:38
First of all, I really like RobH's solution. If you find that traversing the document hierarchy is a common operation in your code, consider introducing a method AncestorsAndSelf()
to your Document
class (alternatively, make it an extension method):
public IEnumerable<Document> AncestorsAndSelf()
{
for (var document = this; document != null; document = document.ParentDocument)
{
yield return document;
}
}
This will allow you to write the method in a more declarative way
private static string SearchDocument(Document document)
{
return document.AncestorsAndSelf()
.Select(d => d.Ordnungsbegriff)
.FirstOrDefault(o => !string.IsNullOrEmpty(o));
}