Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trying to avoid code repetition  - C++

Posted on 2010-08-17
4
Medium Priority
?
470 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

636 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