We’ve all read articles telling us how to write code that’s efficient, readable and top-notch. But how do your write code that is anything but? There’s definitely an art to that.
Lousy coders are everywhere, spreading their lousiness around onto well-meaning newbies. As a coder, you should be aware of coding practices that are less than perfect, and make sure you’re not making these mistakes in your own code.
So this article will share with you 6 things you absolutely shouldn’t do if you don’t want to be a lousy coder.
1. Leaving Your Code Uncommented
If there’s one thing that all lousy code has in common, it’s that it’s uncommented.
You don’t need to add in code commenting as you write your code (that would be a burden), but you should definitely add them in after your code is finished, debugged and polished. Or better yet, write code that’s so easy to understand that it doesn’t need comments.
Seriously, you might know your code like the back of your hand, but you can bet that anyone else reading it won’t. Those who read your code can’t read your mind.
Don’t believe that guy who once said:
“Real programmers don’t comment their code – if it was hard to write, it should be hard to understand.”
Suck it up. Comment your code.
2. Not Putting Any Thought Into Naming Variables
It’s time to take a look at an example of some lousily written code:
var x = 'carrot';
var y = 'orange';
Now which one was x and which one was y? If the variable names were like this:
var vegetable = 'carrot';
var fruit = 'orange';
…then it’s harder to confuse them.
So if head-scratchingly confusing code is your cup of tea, then sloppy variable names are the way to go.
If not, don’t mindlessly give your variables meaningless names. Make them something that’s easy to remember, and makes sense.
3. Not Putting Any Thought Into Naming IDs and Classes
It’s the same idea with HTML and CSS too. Are you that guy who gives your HTML elements a class name of red
because you want to give them a font color of red, and then decides later on you want them blue?
Congratulations, you’re a lousy coder. Having to go back and change your class names is a waste of time, and if there’s one thing we know about programmers, it’s that they don’t like to waste time.
‘But it doesn’t make any sense for an element with blue text to have a class name of red!’
You’re right, it doesn’t. So you should have named your class something like highlight
, which you wouldn’t have had to change later on.
Bottom line: don’t name your variables based on how they’re styled, and even worse, don’t give them a name that’s totally meaningless. Name them so that they’re easy to identify, and won’t have to ever be changed.
4. Not Formatting Your Code Neatly
Which of these two identical functions is harder to read? This:
function sayHello(fname, day) {
if (day === 'Friday') {
$('h2.subhead').text('Hi '+fname+'! Happy Friday!');
} else {
$('h2.subhead').text('Hi '+fname+'!');
}
return fname;
}
or this:
function fubar(x,y){
if(y==='Friday'){$('h2.s').text('Hi '+x+'! Happy Friday!');}
else{$('h2.s').text('Hi '+x+'!')}return x;}
Nothing more needs to be said. A snippet of code tells a thousand words.
5. Making Plenty of Syntax Errors
Spelling and grammar errors don’t make any difference on Facebook. When writing code however, errors are a recipe for disaster.
Misspell your variable names, leave out quotes, semicolons and brackets, use incorrect syntax, and you can be sure you’ve written some of the lousiest code around.
If your code doesn’t work properly, it’s likely to be because you made a syntax error – or ten. So don’t do this. Ensure correct spelling and learn correct syntax.
Oh, and if you want to be extra lousy, make sure you never proof read your code for syntax errors before asking why it doesn’t work on a forum like Stack Overflow.
6. Getting a Computer to Write Your Code For You
Building a website? Using a visual HTML editor will guarantee you some poorly structured, hard to manage, bloated, and, wait for it, lousily coded, HTML and CSS.
If you’re in need of a web page quick and don’t care much about the way it looks, you could perhaps get away with using a visual editor.
But you’re a coder, right? That’s why you’re on this site. You can guarantee that if you get a visual editor to write your HTML for you, you’ll get no respect as a web developer. Use a text editor instead.
Don’t Be a Lousy Coder
Remember that learning to program is not just about learning the syntax, it’s also about being able to craft good code. And one thing’s for sure – if you’re making any of the above mistakes, you’re definitely not crafting good code.
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers.