I am practicing how to find and remove dead code. I have the following code:
int c1 = Integer.parseInt(args[0]) ;
int c2 = Integer.parseInt(args[1]) ;
int c3 = Integer.parseInt(args[2]) ;
/* 1 */ c1 += 7 ;
/* 2 */ System.out.println( c1 ) ;
/* 3 */ while (c1 % 8 != 0)
/* 4 */ if ( c1 % 16 == 0 ) ;
/* 5 */ else
/* 6 */ do
/* 7 */ {
/* 8 */ c1 += 7 ;
/* 9 */ System.out.println( c1 ) ;
/* 10 */ if ( c2 < c3 )
/* 11 */ { c1 = c1+c1 ;
/* 12 */ c3 ++ ;
/* 13 */ c1 /= 2 ;
/* 14 */ c3 -= 1 ;
/* 15 */ }
/* 16 */ }
/* 17 */ while ( c1 % 8 != 0 ) ;
/* 18 */ c1 += 7 ;
/* 19 */ System.out.println( c1 ) ;
}
My oppinion on this code: first the if statement can be removed, because it does not effect the execution of the rest of the code. Besides c1%16 is the same as c1%8.
How do I handle the loops?
I would start from the inner code of the loop:
For example inside the inner if you have
the first and third line cancel each other .. and the same with the second and fourth. Removing those you get the inner if like this:
which can be eliminated (also removing the need for c2, c3 vars) thus making the enclosing statement look like this:
If we go a step up and reverse the enclosing if/else we get something like this:
and the empty else can be removed. Now if you another step up you get:
An you remove the if completely since it’s already checked in the while above. Now if you write the complete code you get:
you can remove the first while and the initial add/print altogether because the first do loop will have the same semantics.
In the end you should obtain something like this:
And if you don’t need to actually print the intermediate values you can obtain the final c1 value via simple mathematics in 1-2 steps :-).