My New Coding Habits
I think any programmers worth their weight improve their practices and styles on a regular basis. I've noticed my code has changed drastically in the last year or so. It's more organized, structured, and maintainable than it was, but I've also picked up what I think are some small but good changes:
-
No
varin globals (JavaScript)I'd be lying if I said I wasn't externally influenced on this one. Douglas Crockford has a lot of great recommendations, and this is one I tend to agree with. When applicable and realistic, making your JavaScript code smaller is always a good thing. It means a faster download for your users which leads to a better user experience.
-
Not abbreviating beyond readability
This is probably a lesson most programmers learned long ago. I went back to some of my older code recently, hoping to reuse it. The good news was that the code itself was written well enough that I could use it with minimal editing. The bad news was that I first had to figure out what many of the variables were for because they were so badly named:
I'm not suggesting Hungarian notation, but descriptive variable and function names are the way to go. You don't want to hate yourself a month later (or for whoever is working with your code to hate you) when it needs to be maintained later. And thanks to tools like YUI Compressor, even JavaScript files won't be negatively affected by adding a few characters here and there.
-
CSS dashes instead of camelCase
The logic here is that a language should look like itself. I understand why I used to prefer camelCase; even minified CSS files can't alter class names or IDs, so dashes make for a larger download. But when revisiting a file, it looks less alien when the nomenclature is cohesive. This makes everything easier.
-
Leaning more towards OO
Again, I think many programmers have learned this lesson already, and likewise I've been using these practices for a while now. The difference is that my code has become so much more maintainable that I preach these lessons often and have become something of an advocate of these patterns.
-
Clean, organized MySQL queries
Thoughout my entire programming career (more than four years now), I have constantly changed the way I organized MySQL queries in my PHP code. Recently I noticed that editing my older code was a pain in the ass, and I sought to change that. Adding a
WHEREclause or an extraSELECTparameter should be an easy task, and the solution to me was to break each section into new lines, tabbing in the same manner that I did in my code blocks: -
Using a Single Event Listener When Possible
Events can be tricky. They can be the cause of memory leaks, and having too many can bog browsers down. So when dealing with multiple instances of similar events, why not combine them? Events are not difficult to parse to figure out what happened to what:
Small improvements like these can add up to make much better code. If you're a developer and you haven't noticed a positive change in your code in the last few months, I highly recommend reading up on useful patterns and practices.
About the 6th point - you mean you use event delegation method right?
@Binny V A: Yes, and I probably should have used that term since it’s getting pretty well recognized.