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.

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.

, , , , ,

About Richard Smaizys

I am a freelance PHP developer working mostly on projects using PrestaShop for ecommerce (modules, theme integration, projects from 0 etc.) and WordPress as CMS solution.

9 Responses to “Improve your code style with simple tips”

  1. ichmbch December 3, 2010 at 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.

  2. Richard Smaizys December 3, 2010 at 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.

  3. bob kyle December 4, 2010 at 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.

  4. eRadical December 4, 2010 at 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.

  5. tom December 4, 2010 at 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 4, 2010 at 5:01 pm # Reply

      Thank you for pointing out those mistakes. ;)

  6. Artur Ejsmont December 6, 2010 at 5:27 pm # Reply

    Seems like basic stuff to me. If anyone is interested in really nice coding practices guide have look at Zend Framework’s one.

    http://framework.zend.com/manual/en/coding-standard.coding-style.html

    http://framework.zend.com/wiki/display/ZFDEV/PHP+Coding+Standard+(draft)

    In case “tom” is reading my comment, English is not my native language so back off :D

    thanks Richard

  7. Jaik December 6, 2010 at 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 :-)

Trackbacks/Pingbacks

  1. Richard Smaizys’ Blog: Improve your code style with simple tips | Development Blog With Code Updates : Developercast.com - December 3, 2010

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

Leave a Reply