Solved

Trying to avoid code repetition  - C++

Posted on 2010-08-17
4
457 Views
Last Modified: 2013-12-14
I have two elements with same name in a XML tree, one at a higher level, and others as inner nodes. Something like this...

 <mainNode>
<condition> yes </condition>
<Level-1>
                <condition> yes </condition>
</Level-1>
<Level-2>
                <condition> no </condition>
</Level-2>
<Level-3>
                <condition> no </condition>
</Level-3>
</mainNode>

While coding in C++, I need to over-ride the condition of the mainNode with that of the condition in Level Node. One way of coding is as shown below.  

The code for 'levelCondition' and 'mainNodeCondition' are the same and pretty big. Is there a way to check both the conditions in one if condition instead of repeating the same twice? Any other suggestions are also welcome.
 

               

if (levelCondition)
{
                /***********
                (actual code for 'yes' condition goes here)
                **********/
}
else if(mainNodeCondition)
{
                /***********
                (actual code for 'yes' condition goes here)
                **********/
}
else
{
                /***********
                (code for 'no' condition)
                **********/

}

Open in new window

0
Comment
Question by:sukhoi35
  • 2
4 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 33453594
Hi sukhoi35,

I'm not sure if I really understand, but if you want to execute the code you marked as '(actual code for 'yes' condition goes here)' if either 'levelCondition' or 'mainNodeCondition' is 'true' you can simply write it like this:

> if ( levelCondition || mainNodeCondition )
>{
>                /***********
>                (actual code for 'yes' condition goes here)
>                **********/
>}
>else
>{
>                /***********
>                (code for 'no' condition)
>                **********/
>}

ZOPPO
0
 

Author Comment

by:sukhoi35
ID: 33453630
Hi Zoppo,
As I have mentioned above, I need to over-ride the main condition with level condition. If I were considering one of the conditions to be true, your suggestion would hold good.

Thanks.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
ID: 33453671
Sorry, where is the repetition? In the expression to be executed in the if or else clauses? If so why not wrap up the code in a function and just call a function in each one?
if (levelCondition)
{
                process(levelNode)
}
else if(mainNodeCondition)
{
                process(rootNode)
}
else
{
                /***********
                (code for 'no' condition)
                **********/

}

Open in new window

0
 

Author Closing Comment

by:sukhoi35
ID: 33463434
Thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Eclipse. How to move up/down source folders 4 88
I could not build boost code, 10 88
Syntax Error 2 65
Support for Notepad++ (including downloading & installing a plugin) 5 120
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question