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”