The solution is obvious, but this is question about nice solution.
(EDIT: by nice I mean e.g. 1) without code redudancy 2) without comprimising performance 3) without forcing programmer to make some unnecessary function or temporary variables )
Consider situation when I would like to execute 3 different blocks of code depending on which of numbers a
,b
,c
is smallest.
The code would look like this:
if( a < b ){
if( a < c ){
// code block for "a is minimum" case
}else{
// code block for "c is minimum" case
}
}else{
if( b < c ){
// code block for "b is minimum" case
}else{
// code block for "c is minimum" case
}
}
What I don't like is that I have to copy the // code block for "c is minimum" case
twice.
There are several solutions to that. E.g. I can put the block of code for "c is minimum"
into an inline function or macro. But I don't like it (seems less clear to read).
Old school solution would be use goto
like:
if( a < b ){
if( a < c ){
// code for "a is minimum" case
goto BRANCHE_END;
}
}else{
if( b < c ){
// code for "b is minimum" case
goto BRANCHE_END;
}
}
// code for "c is minimum" case
BRANCHE_END:
but people don't like to see goto (for good reason). On the other hand in this particular case it is even very well readable.
If the block of code would be independent function it can be written like
void myBranching( double a, double b, double c ){
if( a < b ){
if( a < c ){
// code for "a is minimum" case
return;
}
}else{
if( b < c ){
// code for "b is minimum" case
return;
}
}
// code for "c is minimum" case
return;
}
(which is actually almost the same as that goto
) But in many cases similar block of code have to be part of more complex algorithm and it is inconvenient to put it inside function. Encapsulation in function would require passing many variables which are used both inside and outside ( e.g. see the use case below) .
Is there any control structure in C/C++ which would solve this in elegant way.
NOTE: Consider performance critical code. (e.g. ray-tracer, GLSL shader, physical simulation ). Anything which would add some unnecessary computational overhead is out of question.
Additional questions / comments
- This is one of a few examples when I feel like Structured programming tie my hands, and that it is just subset of what is possible to do with
jump
instruction. Do you know other examples where algorithm would be more simple and clear usinggoto
rather than standard control structures ? - can you imagine more complex branching where it would be necessary to copy some blocks of code even more times ?
EDIT : Use case
I think some confusion resulted from the fact that I did not specified context in which I want to use this. This is part of algorithm which raytrace regular triclinic 3D grid ( something like Bresenham's line algorithm in 3D, but the starting point is float (not aligned to center of any box) )
but please, do not focus on algorithm itself, it may be also wrong, I'm currently debugging it.
double pa,pb,pc,invPa,invPb,invPc,mda,mdb,mdc,tmax,t;
int ia,ib,ic;
// for shortness I don't show initialization of these variables
while( t<tmax ){
double tma = mda * invPa;
double tmb = mdb * invPb;
double tmc = mdc * invPc;
if( tma < tmb ){
if( tma < tmc ){ // a min
t += tma;
mda = 1;
mdb -= pb*tma;
mdc -= pc*tma;
ia++;
}else{ // c min
t += tmc;
mda -= pa*tmc;
mdb -= pb*tmc;
mdc = 1;
ic++;
}
}else{
if( tmb < tmc ){ // b min
t += tmb;
mda -= pa*tmb;
mdb = 1;
mdc -= pc*tmb;
ib++;
}else{ // c min
t += tmc;
mda -= pa*tmc;
mdb -= pb*tmc;
mdc = 1;
ic++;
}
}
// do something with ia,ib,ic,mda,mdb,mdc
}