Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Having simple code like this:

int A=5;
object X=Console.ReadLine()
if(Condition)
   DoSomething();
else
   DoStuff();
DoSomethingElse();

Some sources say there are actually 4 branches: First unconditional, two for the IF and another unconditional after the IF statement. Some say there are only two branches. What would be correct? E.g. here: http://www.ruleworks.co.uk/testguide/BS7925-2-Annex-B7.asp

share|improve this question
3  
The npath complexity of this code is 2. Can you point to a source that claims four branches for this code? The term 'branch' might be being misused or misunderstood. –  MichaelT Jan 30 '13 at 16:23
    
Branch is just a block of code. Some sources consider only two branches in IF, but there is also a code before and after the IF. This is not npath complexity. The code consists of 4 pieces - block before the IF, two possible blocks for IF, and a block after the IF. –  user970696 Jan 30 '13 at 16:24
5  
A branch is not a block of code - it is a specific point in the code that changes how the flow of the program executes. The term 'branch' comes from branch operations in assembly. These are if statements in higher level languages. –  MichaelT Jan 30 '13 at 16:34
    
Ok I see, please check the link. They say there is 10 branches in the code sample there which is the point they make in distinction between branch and decision coverage. If there was 6 branches, it would be the same as decision coverage then. –  user970696 Jan 30 '13 at 16:35
3  
It looks like the people who wrote the document you are referring to decided to invent their own meaning of branch which is very inconsistent with the common understanding. This is why names matter, A LOT. While it is common and expected to commandeer names in politics, it should really be taboo when it comes to science and the technical realms. –  Dunk Jan 30 '13 at 16:52
show 6 more comments

3 Answers

up vote 0 down vote accepted

The origins of the word 'branch' in code comes from assembly. An example of this can be seen in MIPS assembly. A branch is a conditional statement.

Before anyone jumps on me for point out that MIPS has a b instruction which has the description of "branch unconditionally" the key to the difference between b (branch unconditionally) and j (jump) is that the branch statements work off of relative addresses and the jump statements work off of absolute addresses.

There is also some terminology of unconditional branch in the realm of branch prediction. A CPU with a pipeline will try to guess which way a branch statement goes and where it will end up after the branch. It is possible to analize the assembly to see that a given branch will always execute given a certain set of conditions at the start of the pipline. For example, the branch is based on if register 1 is greater than 0 or not. If register 1 is 0, and no instructions in the pipeline change that, it is in essence an unconditional branch which can be executed safely (and not having to worry about flush the pipeline after speculative execution or stall until the branch can be decided).

All of the above was for very low level code. In higher level code (as demonstrated in the question) the terminology of a branch is where code may follow two (or more in the case of a switch (pun not intended)) different paths. From wikipedia

A branch is sequence of code in a computer program which is conditionally executed depending on how the flow of control is altered at the branching point.

Unconditional code is not conditionally executed and thus is not a branch.

share|improve this answer
    
From your answer I feel like you are calling branches the decision nodes, while the branch is actually the path (each of them). Maybe my misunderstanding in English. However the main question is, how many branches does my example have? –  user970696 Jan 31 '13 at 6:50
    
@user970696 2 branches and the npath complexity is 2. In the context of unit tests, for complete branch coverage you need 2 tests. Calling DoSomething(); is one branch and calling DoStuff(); is another branch. –  MichaelT Jan 31 '13 at 14:42
add comment

I think branch here is meant to represent a block of code with a single linear path (no jumps starts or ends in the block) better known as a basic block.

This concept is handy for compilers and other analysis tools to decide how a piece of code might behave (in the absence of exceptions) as each basic block is then an atomic operation in terms of the code around it.

For example, optimising compilers can look at each such block and decide how the dependencies for each individual instruction lies and possibly reorder them to make more efficient use of registers and the stack. Or, when only one exit path exists to a another block with only one entry, you can concatenate the blocks and eliminate the jump.

share|improve this answer
add comment

It depends on your context. For some analysis, such as what compilers do - you have 4 branches, since the post-if block can vary its behavior depending on which path the if goes (and if either conditional branch returns early). For some analysis like you might see in code coverage will vary to perhaps consider that somewhere between 2 and 4 branches depending on what is being done and if priority is given to the choice or to the path.

Neither view is particularly "correct" or not. They're human inventions to try and gain information and insight. Whichever metric does that better for the problem at hand is the "correct" one.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.