Skip to main content
1 of 3
gnasher729
  • 47.5k
  • 4
  • 68
  • 133

The code you linked to is not editable. If you don’t have to touch it, leave it. If you have to touch it, having unit tests would be nice. So write unit tests, and make sure that if you change any single condition, some unit test breaks.

Now the linked code is not difficult to change but difficult to change without making any mistakes. One way that helps is two people making the changes individually and then you check that you have the exact same code twice. It improves your chances.

Now a systematic way to fix it in this case of massively nested ifs and else’s: Add a variable “bool done = false”. Then you change from “if(cond1) { things1; if (cond2) { things2; if (cond3) { things3 } } } to

If (! Cond1) done = true;  
If (! Done) thing1;  
If (! Done && ! Cond2) done = true;
If (!done) thing2;
If (! Done && ! Cond3) done = true;
If (! Done) thing3;

And you can do that for miles.

gnasher729
  • 47.5k
  • 4
  • 68
  • 133