Edit: Since many seem to misunderstand my intentions and my point, I wish to clarify: When writing code, you should make your code as readable as possible. That is not what the article is about, though. I wrote this to show newcomers to the world of programming that it doesn’t matter if you use conventions religiously or not, if the end result is the same. What I wanted to say with this is that it doesn’t matter if you put one or two spaces between the brackets or if you put newlines before curly brackets or not, unless the project requires a very strict convention.
I’m not against conventions in any way, I use them religiously because I feel it is easier to do. Neither am I saying to throw all conventions to the wind, I’m saying that convention might be the last thing a newcomer to programming should worry about and that people intending to help newcomers should bear that in mind as well.
As a programmer of many languages, I have read and followed countless tutorials and guides. I’ve read many a book on the subject and I have discovered one thing sorely lacking from all of these, something that is so basic, yet it is implied that you should already know it.
When it comes to programming, you will often find yourself in need of help. When asking others, you will often hear you did something wrong and that they know a better way. This is when I most often stop them and say “No, I did not do something wrong, I did something different.”.
This is the most fundamental thing in programming, as long as the end result is to satisfaction, how you got there doesn’t matter. What coding convention you use is entirely up to you, what language, framework, methods, functions, variable types, database schemas… it’s all up to you as the architect of the code, but others will have you believe their way is the only way.
Sure, there are less efficient and less effective and less expandable ways, but that doesn’t mean they are wrong.
Take the following example:
Customer X wants a website. The website should have two pages, a main page and a “contact us” page. The customer does not expect this to be expanded upon in the future.
Now, a simple way to do it is to code everything in HTML and add the CSS styling directly to it. This is a compact way of doing it and with copy/paste, you can quickly get the elements that are the same over to the other file. It works, the work is done, the customer is satisfied.
People who see your code might say this is the “wrong” way of doing it in 2015. You SHOULD use something like PHP or .NET to make it more expandable and more efficient. The problem here is that you would then have to learn any of those languages, implement it and there’s a few more steps to doing things, for the same end result. Sure, if something changes later, it is easier to change it using PHP and external CSS sheets, but it isn’t needed for this case.
People will also get hangups about coding conventions, such as variable naming and placements of brackets. These things affect legibility of the code, but not the function. Each coder have their own convention, but most stick somewhat to one of the more common practices in some regards.
You’ll often hear teachers say that you should always write constants in only capital letters or always start methods with a capital and use camel-case. You could do that, but a constant will still be a constant if it is written in lower-case letters and the method will still function properly if you write it entirely in capitals.
It doesn’t matter if you have whitespace, tabs, newline or no space at all on each side of a bracket, the code will work the same. You can end with a semicolon directly, have a tab or a space between, it doesn’t make one bit of difference.
If you have made an entire application on your own, using your own convention, it will take an experienced coder about 2 minutes to figure out your convention and get used to reading it. Legibility will be intact!
A few exceptions
There are a few exceptions though, a few cases where convention is important. Anything visible to the user should follow the current conventions. A save button showing the image of a folder does still work, but it’s less intuitive for the user, who is used to seeing a symbol of a floppy disk as saving and is used to seeing the folder symbol as loading.
If the code needs expandability, it is probably best to use one of the expandable languages, preferably with some sort of framework to make it easier for future coders (or even future you, as a couple of hundreds of lines of code could be hard to remember a few months or years down the line). It’s also a common courtesy to use proper coding conventions in these cases, as it will help speed things up a bit.
If more than one person is working on the project, everyone involved who needs to read the code should agree on a common coding convention, to make it easier to quickly read the code.
So many hours spent on discussions about code convention could have been avoided, if people stopped criticising the choice of convention. Most Q&A forums I’ve visited has had every single poster’s code questioned for not using a certain convention. Longwinded discussions arise on why they didn’t do this or that, and the asking poster has to explain their entire code up until that point, and most of the time, the person answering still don’t understand why the poster did what he did and just decide to drop out of the conversation, answer still not reached.
So when you hear someone say “this is wrong”, kindly reply “no, it’s different”. Take what they say to heart, they only mean well, but also let them know that your code is yours and that you do have reasons to do what you are doing.