There are some professional programmers often talking about good and bad practices of code writing. I would like to talk about this topic a bit more because I think that there some important points for beginners to understand.
First of all, the best code practice is an imaginary way of writing code that is easy to read, debug, edit for you and other developers that may work in one team or after you. Why do I say that this practice is imaginary? Well, there are about 5 different styles of writing codes and they were created by different type of developers and for various programming languages or systems. I believe we could find even more of them, but most of us use the most popular ones. So how there could be a perfect style of code if there’s different needs for development? Moreover, only a person knows which one is suitable for him individually.
Secondly, the best practices of writing code is created to help people maintain standards on this amazing variety of different and also similar languages. For example, PHP, C, C++ syntaxes a very close and these standards let us to read code easily on both of them. You should familiarize with some different practices of writing code and choose which one you will use for your project.
Thirdly, by looking at some examples (also that are shown below) you need to decide which one best suites you and choose it. However, do not forget that it would be your coding standard that should be easily understandable for you above all. And after a while this code standard will become not the best one on the whole programming styles but yours. It is very important to understand this point
So how to choose the right one?
There are most talks about loops and conditional sentences. Here is some examples of most popular code styling practices.
if (a > 5) { // This is K&R style }
if (a > 5) { // This is ANSI C++ style }
if (a > 5) { // This is GNU style }
I mostly prefer K&R style because I find it easy to read and understand. In addition, you do not have to push additional enter for making a first line to the brace. However, by having second option, you can easily comment out the condition or loop sentence in order to test program.
You can find more information about coding styles at Wikipedia also at some well known frameworks such as Zend Documentation
It is worth to remember that you should use only one coding style on project. You may change them, but on different projects. It is hard to maintain code if it is written on different types of styling.
Just try to find your best coding style and write clean code for your projects.
Comments
Hi, I found a mistake in your example code, at the line:
// This is K&R style
Thanks for the help – sometimes WordPress annoys me making those symbols to entities after saving or maybe this was because I used other plugin for syntax highlighting.
[…] have already written about best practices for choosing how to put brackets which style of variable writing to choose. However, I would like to talk more about good programming […]
[…] covers two things in this post (there’s this other about brackets) – tabs versus spaces (or hard vs soft tabs) and a bit more on braces, specifically about the […]