Improve your code style with simple tips

I 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 style and its practice.

I might repeat, but knowing and sticking to one good practice is a skilled professional sign and I am sure that you are one or want to be one of them. So you have to know and you can not forget that your program (not only websites) is like a living beast with whom many people might work in the future. Your code is like an art piece that needs editing, renewal and etc.  You can not just think that you will always be the person who manages everything and supports all the bugs.

By understanding this you also agree that the code must be maintainable and readable not only for you after a year or two but to other people that may not be so skilled or advanced and otherwise.

Tabs or spaces

There are two types of tabs – hard tabs and soft tabs. Hard tabs are usually  made by pressing tab button on your keyboard and soft ones are the ones you put by pressing spacebar several times.

I have heard many thoughts about both of them, but the main principle here is that hard tabs depend on your machine and IDE that you or someone else is using for their work. So, for example, if your IDE tab is four spaces and you open the file with some other software, you might see spoiled and collapsed indentation. If this happens, your code become worthless because programmer can not read it properly.

Everything else is just a small talk, because there’s no really a difference which technique is used more easily and faster and etc. If you choose one type of tabs and you use it for your everyday job, you will become a master at writing it. I think everything depends on your habits.

Aligning assignments with spaces

It is also a good practice to align similar assignments blocks to make the code more readable. Moreover, I agree that this practice is time consuming and just boring, but I still stay with my opinion that everything depends on habits.

$username = $_POST['username'];
$email    = $_POST['email'];
$address  = $_POST['address'];

And I really appreciate this kind of writing code because it really makes easier to find mistakes and understand the main logic of function or file.

Here is an example of aligned assigns with whitespace :

Using braces

I talked about braces a lot on my recent post, but I want to make another point here.

People say that you do not need to put braces if your conditional statement or loop has one line. However, this practice is faulty, because it becomes hard to maintain the code. Imagine if I had to put some new lines there, so I would need to add braces too. This helps to prevent from simple mistakes which annoys all of us.

So if you already put those braces, you do not need to think one more time if you need them or not. Moreover, by putting braces you also decide which way of typing them others have to use in your code and so on.

Conclusion

Good writing practice will help you not only to show that you are skilled and professional programmer, but to also maintain the code better in the future or make it readable and easy to change for other people. Many people tend to say that programmers are lazy, so be lazy wisely and you will forget all the annoying problems that generally come up.

Leave a comment

Comments

  • ichmbch
    December 3rd, 2010 / 2:06 PM
    Reply

    Actually there is nor problems with hard tabs on different IDEs and the code is always readable if you follow the rules specific to tabbed programming. For example you must NEVER use spaces (in any case) for code alignment when you use tabs. Aligning assignments with spaces is also not suitable for tabbed programming.

  • Richard Smaizys
    December 3rd, 2010 / 9:52 PM
    Reply

    You might be right about hard tabs problem on different browsers, but I remember I had many problems with DevC++ and MS Visual Studio tabs. In addition, people usually do not keep the rule you mentioned, so the code becomes unreadable in many cases.

  • Richard Smaizys’ Blog: Improve your code style with simple tips | Development Blog With Code Updates : Developercast.com
    December 3rd, 2010 / 10:18 PM
    Reply

    […] Smaizys has a new post to his blog with a few simple tips you can follow to help improve your code’s style and readability. So […]

  • bob kyle
    December 4th, 2010 / 1:11 AM
    Reply

    After having to deal with 5+ languages I’ve gotten used to all the nuances in people code. Sure it’s personal style, but I’ve learned to deal with it. I have my own style, other people have theirs, in the end, I’ll fix it the way I want to and as quick as possible. I’ll let my team do the same.

    I see the value of in coding standards, but not enough to enforce it. I let people code without fear and during beers we can discuss how I agree or don’t agree.

    python ruby have no braces, php does, some enforce spaces, others tab…in the end does the code work and was there any time lost figuring out code? Probably hard to measure.

    The big issue I find in code readilibity and maintainability is between amateur developers and senior devs. The amateurs could certainly use a coding standard and maybe that’s why after years of coding they enforce it on others because someone enforced a standard on them.

  • eRadical
    December 4th, 2010 / 1:14 AM
    Reply

    I’m accustomed to a 4 space tab. My mentor (& ex boss) was accustomed to 8 space tab. I was using an IDE and I was happy with my 4 space tab… he was using a simpler editor and he was happy with his 8 space tab… while both were looking at the same project/file…

    If I would have entered 4 spaces instead of a tab his setting (of an 8 spaced tab) would have been useless…

    I think it’s better with tabs. So far all teams I worked in used tabs.

  • tom
    December 4th, 2010 / 2:23 PM
    Reply

    Jeeez. You rant about the style of writing (code) but you can’t even formulate a sentence in English right. Come on. “art piece which sometimes need”? “hard tabs depends”? “which one is putted”?!?!?!
    You should check some tips on improving your writing style.

    • Richard Smaizys
      December 4th, 2010 / 5:01 PM
      Reply

      Thank you for pointing out those mistakes. 😉

  • Jaik
    December 6th, 2010 / 8:02 PM
    Reply

    A good text editor should be able to speed up any boring formatting automatically. For instance, aligning assignments can be done on an entire block in Textmate by pressing Cmd+Alt+]

    If you’re getting bored of formatting your code then it’s time to set up your editor properly 🙂