Solved

Trying to avoid code repetition  - C++

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

861 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

25 Experts available now in Live!

Get 1:1 Help Now