Paranoid Programming: no such thing as optional braces
Posted by james on Nov 22, 2009 12:00 AM
The more I program, the more paranoid I become. It's started to become more of a game of statistics and chances than cleverness or brilliance. No matter how smart or experienced I am, there's an X% chance that I'll introduce a bug. If I change the way I code, that becomes a slightly smaller percentage. It's all about methodology. When you create programs for an assignment in school, it only matters if it works once, for the specific dataset from the assignment, and then it's thrown out.
In the "real world", you have to deal with maintenance. That changes everything.
So, one programming method I've taken up most recently: always use braces. This doesn't apply to python, but it does for Javascript, C++, and other C-like languages. Clever programming gives you this:
if (some_condition)
function1();
x=3;
function2();
However, it's just too easy to screw this up later when adding lines:
if (some_condition)
function1a();
function1b();
x=3;
function2();
Now you've got a bug. Worse yet, it's a silent fail (the bane of programming), which allows the program to run but give completely wrong results.
So, always use braces:
if (some_condition) {
function1();
}
x=3;
function2();
It's harder to mess up that code. The 12% chance of introducing a bug into those 3 lines of code just became 8%. It's all about method.

What I have not done but considered doing is the same thing for logical comparisons, ie:
if (foo == True) and (bar == False):
doSomething()
vs.
if foo == True and bar == False:
doSomething()
Login or Sign Up to post comments