7
\$\begingroup\$

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";
}
RubberDuck
31.1k6 gold badges73 silver badges176 bronze badges
asked Mar 26, 2015 at 12:03
\$\endgroup\$
2
  • \$\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\$ Commented 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\$ Commented Mar 26, 2015 at 12:27

3 Answers 3

10
\$\begingroup\$

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).

answered Mar 26, 2015 at 12:31
\$\endgroup\$
1
  • 2
    \$\begingroup\$ A very nice alternative. I hadn't considered an iterative approach. \$\endgroup\$ Commented Mar 26, 2015 at 12:33
8
\$\begingroup\$

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.

answered Mar 26, 2015 at 12:28
\$\endgroup\$
2
  • 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\$ Commented 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\$ Commented Mar 26, 2015 at 12:38
4
\$\begingroup\$

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));
}
answered Apr 1, 2015 at 23:14
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.