Solved

Trying to avoid code repetition  - C++

Posted on 2010-08-17
4
454 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 30

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now