Solved

Trying to avoid code repetition  - C++

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays 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.

726 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