What is the best practice for writing code?

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?

  1. If you are not familiar with different kind of programming languages, you may like to see their different syntaxes and styles for a kick off. You might be using entirely different programming language in the future, so try to make a good decision that you would not need to change coding style much. Your writing of code will become a habit that would be hard to change, I think.
  2. Try to choose the one which is the easiest to read for you. You would not want to waste your time by trying to realize what have you written earlier. You should clearly see the main codes of programs, loops, conditional sentences and so on.
  3. You also have to think of your code writing speed. This is not as important as the points above, but you may thing how many of lines, tabs, enters you have to make to write a simple block of code. This may help you after a while when you will start to work on restricted deadlines.

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.

Leave a comment


  • Julius
    November 26th, 2010 / 9:42 PM

    Hi, I found a mistake in your example code, at the line:

    // This is K&R style

    • Richard Smaizys
      November 26th, 2010 / 9:49 PM

      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.

  • Tips how to write good style code - identation, braces, tabs and spaces | Richard Smaizys development blog
    December 3rd, 2010 / 10:40 AM

    […] 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 […]

  • Programowanie w PHP » Blog Archive » Richard Smaizys’ Blog: Improve your code style with simple tips
    July 11th, 2011 / 12:53 AM

    […] 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 […]

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. Cookies are only used for statistical purposes.