When I was a bit younger we used to play a game called Pitfall where you would guide your player across various obstacles, including chomping alligators, deadly quicksand, and rushing rivers. In programming there are also pitfalls, and they may not be as bad as an alligator pit, but they will consume your time and leave you drained at the end of the day.
Software errors will always happen, it comes down to how quickly you can identify them and correct them that make all the difference. Those that argue that code should be written error free the first time have obviously not written much code. We can all agree that production code should be error free, but to achieve error free code means you will have to identify and eliminate errors.
I wanted to share a few thoughts on how to cut down on errors in your code.
Use proper indentation, and formatting. This is almost never taught in class, and most of the books that I have seen even contain code that runs and is compact but is not really easy to read. It’s important to write all your code as if you are going to spend the rest of your career maintaining it.
This sounds great but how can I implement it? Let’s take a look at some code.
I like to use a tab for each block of code. Look at the difference between block a and block b.
A
Ext.override(Ext.form.Radio, {
setValue : function(v) {
if (typeof v == 'boolean') {
Ext.form.Radio.superclass.setValue.call(this, v);
} else if (this.rendered) {
var els = this.getCheckEl().select('input[name=' + this.el.dom.name + ']');
els.each(function(el){
if (el.dom.value == v) {
Ext.getCmp(el.dom.id).setValue(true);
} else {
Ext.getCmp(el.dom.id).setValue(false);
}
}, this);
}
return this;
}
});
B
Ext.override(Ext.form.Radio, { setValue : function(v) {
if (typeof v == 'boolean') {
Ext.form.Radio.superclass.setValue.call(this, v);
} else if (this.rendered) {
var els = this.getCheckEl().select('input[name=' + this.el.dom.name + ']');
els.each(function(el){
if (el.dom.value == v) {Ext.getCmp(el.dom.id).setValue(true);
} else {Ext.getCmp(el.dom.id).setValue(false);
}}, this);}
return this;
} });
Hard to believe that’s the same code right? Now imagine this throughout a file containing a thousand, ten thousand, hundreds of thousands of lines of code.
Use curly braces even when you don’t have to. Wait won’t that bloat my code? I’ve been taught that I should keep things as compact as possible what’s up with this. Let me explain in code:
A
if (condition1)
{
if (condition2)
{
statement1;
} else {
statement2;
}
}
B
if (condition1) if (condition2) statement1; else statement2;
To the compiler, both pieces of code above will achieve the same result; however, the top block is far easier to read for us humans. Which code would you like to maintain.
Don’t use variable names that have no meaning – remember comments in the code should not be used to explain what your variables are, let their names tell all. This rule needs no code explanation, just stay away from one letter variables like a, b, or c and try to spell them out like
A
double a;
double b;
a = (b - 32) * 0.555555556
B
double celsius;
double fahrenheit;
celsius = (fahrenheit - 32) * 0.555555556
Bet you didn’t know that example A did until you got a look at B, right.
Test often and test early. How many lines do you write before you stop and see if what you have compiles. Do you write test cases for all your functions when you code them and actually test them before connecting them to the user interface? This helps you tackle bugs close to the source without wading through hundreds of lines of code.
If your IDE does not have a nice auto save feature like Jet Brain’s WebStorm, then please remember to save early and save often. There is nothing more frustrating than losing hours of good code.
Nobody ever wanted an app that ran slower, keep that in mind and optimize, optimize, optimize. You can use tools like Page Speed, or Chrome’s Developer Tools to get your JavaScript on track. In addition Google and Yahoo have written detailed articles that are essentially a lessons learned document