Code conventions, lazy or untrained?
April 28, 2011 | Posted by [email protected] under Programming |
It’s unbelievable how so many programmers flaunt code conventions to the point that they seem either lazy or untrained! It’s also equally unbelievable how they can get away with it, without the employer knowing anything about what’s going on! I recently took over a site from a previous developer (who I will not mention) that fit this criteria.
While working on the site, I happened to encounter massive code duplication, no standards on variable or function naming, lack of indentation, lack of folder structure, using old depreciated methods, not to mention the same problems within the site’s database, and the list continues. I have spent a good deal of time rebuilding the site from scratch because much of what was there was unusable.
Just the other day I hit the worst I have ever seen (I thought I had already) : One page had over 5,000 lines of code which was then copied and pasted into another page with only a few values changed. On top of that both of these were duplicated again into a French directory with just the text changed to French.
So for two simple pages in French and English, there were over 20,000 total lines of code.
I spent most of last night revamping it, starting from scratch and refactoring the logic. The result? One page of 143 lines of code (instead of over 20,000), which covers the two pages in both French and English. So the big question, was this programmer lazy or untrained? I would have to say both! The employer had no clue that what he/she was investing so much money into was useless, and a maintenance nightmare!
I strongly urge programmers to learn and to continue learning and so as not to provide a client with this type of work. At the same time employers need to spend some time in verifying they are receiving what they are paying for.
These are standards that I work by, which may help someone out there, so listen up!
1. Indent
your
code!
If you are not indenting then 90% of the time its because you are copying and pasting code!
2. Do not copy and paste code – Do not copy and paste code!
Copying and pasting code is the result of a lazy programmer. This results in a big maintenance problems and long iterations to complete future changes.
3. Structure your {files}
You need to create a folder structure, this will help developers to know where to look and easy to maintain.
4. Naming Standards
This is a must: you MUST choose a standard and KEEP to it. This is the same for both your database naming and code naming. The importance is not on the convention you use but making sure you keep the same convention throughout the code. Some rules to follow:
a. The classes, method names and variable names should say exactly what it does or represents. The rule you should follow is, if i am adding a comment to a method or a variable, then I did not name it properly.
b. No prefixes or suffixes in any case.
c. Choose a standard convention and keep to it. A good one out there right now is using Camel Case syntax. For private variables, they should be camelCase (first letter lowercase), and methods/functions and classes should be CamcelCase (first letter uppercase). No underscores, or dashes.
Example:
class User {
var firstName = “test”;
function CheckIfUserHasFirstName()
{
//do something
}}
5. Functions/Methods should have around 10-20 lines of code
If your methods have many lines of code, its time to re-factor them. If the method is doing more than the detailed method name says, its time to re-factor them. Make smaller methods that will be used within the other methods and again name them precisely.
6. There should be very little to no code duplication
In your code, there should be more or less no code duplication, if its duplicated, then it should be re-factored into a class (service, manager, factory, etc..), or method somewhere which then can be reused.
7. Seperate your business logic, user interface and data layer as much as you can
Try your best to separate the layers. This is done easier with object orientation programming which I think will benefit most programmers out there. So if you have not learnt it yet, get out there and start! Most languages support it or are built around it. (PHP, Javascript, Java, C#.net, MVC.NET, etc..)
Hopefully I did not miss any thing there, if I notice I did I will update it later.
Recent Comments