Best Coding Practices and Code Conventions: Why Are They Important?
Today I want to talk about best practices when it comes to coding or coding conventions in general. What are they and why are they important for you to understand? Let's talk about it today.
We're talking about programming best practices or code conventions. Let's start off with a definition.
What is a code convention? Well, think of them as guidelines for programming in general. They often address things like programming style, programming practices and methods for all aspects of the programming life cycle. With that said, why would you want to use code conventions?
Well, first and foremost, everyone who follows a code convention is actually on the same page and that's a good thing. Also keep in mind that these code conventions have often been formulated over a period of time. They've stood the test of time and they've been created, or contributed to, by lots of developers so they're pretty stable and, you know, they've just come about from years of actually hard real world experience. So, don't assume looking at a code convention, that you necessarily know better. That's the thing there, that code conventions have been around for a long time and updated and particularly if you are using a recent version, chances are they are going to be pretty accurate.
Now, there are a number of different types of conventions out there. Firstly, there's general conventions. Now, that's applicable to software development in general. It doesn't target a specific programming language or style or whatever, it's just sort of general guidelines. But then we have got code conventions for specific programming languages, often put out by the creator of the programming language. Then we've got company conventions, for a specific employer who ask their programmers who come aboard and are employees of that company, to follow certain code conventions.
Let's look at a few quick examples of code conventions. I'm not going to go into a lot of detail, but you can see it's an example here (see image) where Oracle published their best practices for Java.
Now, that's actually quite an old document so make sure that you're using the latest version, or that the code convention document you're using is of the latest version. These code conventions that I found for Java date back to 1997, it's very, very old. So, no doubt with that particular one, given that's over 20 years old, there's probably parts to it or aspects of it that are no longer valid or at least should be re-looked at.
A great place to find coding conventions is Google search engine. There are a lot of great material out there if you take the time to go through and search for them. But don't blindly assume everything you read about code conventions are correct. You may want to check the credentials of the person or company who created the code conventions and if you can't verify where they've come from, so to speak, then look at multiple sources and try and get some multiple places that talk about the same types of conventions and sort of agree on conventions so that you know you are actually dealing with something that is actually valid and has been agreed on by a range of people.
When you start a new programming job, it's really important to look at the code conventions and often when you start, you'll be told this anyway. Depending on the size of the company, it might be a formal document, some sort of policy that has been put in place or it may actually just be a README file, just a simple text file somewhere in the code base or you may even just have to find it. Find out what the code conventions are by studying the source code, sort of seeing how they go about writing things and making sure that your code is very similar.
In general, in line with that, don't come into a new company and try and code in a completely different way. So, you want to really understand the code conventions that are in place, if any, for that company, study the code base, and try and make sure your code is being written in a very similar way to the existing code. Also, in general, you never want to actually change code purely to fix what you think is a code convention violation. That's a really poor reason to actually change code and again that could be a really subjective thing. You don't want to be changing things especially when you are just starting, on the assumption that you are doing a good thing there. So, be really careful of that.
Take your time to read and understand the conventions and best practices because by doing that, in general, (this is for the company conventions) if you're working for them but code conventions, it will actually make you a better programmer. Because again, these conventions and best practices have been put together over large periods of time. Lots of code base, lots of developers, so the chances are because of that, because it's been so well used by a lot of programmers, that they've thought about the ramifications of doing things in a certain way and you can be sure that if you follow those conventions it's highly likely to be on the right track.
So, make sure you invest some time in learning code conventions. It will certainly benefit you and your career. You've made it to the end of this post and I hope that helped. If you've got any questions, feel free to leave a comment and I'll get back to you.