Why we should use proper code styles
Have you ever been working on a project with your friends and realized that the code of your friends feels hard to read because it’s naming variable is not consistent or maybe sometimes there are white spaces between =
and sometimes they don’t use it? Or maybe you are too generous and looking forward to contributing to an open source project in GitHub and you find your pull request was closed due to your code style? Then you’re at the right place. Today I’m going to give tips about maintaining good code that is easier to read by yourself, and also other people.
What is code style?
Code style, programming style, or coding conventions is a set of rules or guidelines when writing code. The motivation behind creating code style is to make sure developers write based on the guideline thus it will be able to be read by other developers. Besides, it also helpful to avoid developers creating mistakes while writing code. Most programming languages have more than one code style. This happened due to companies or open source organizations usually have their own code style. One of the weird naming conventions that I’ve ever found is the code style of Android source code where I have to write an extra m
in front of name variable for some certain type of variables. I hardly accept that convention, the only time I comply with that code convention is when I’m teaching on workshops (I’m such a bad developer!).
Where can I learn a code style for a particular programming language?
The answer to this question may be vary. As I stated earlier, usually organizations or companies have their own code style. However, most of them are actually using a well-known code style out there or some of them might modify those code style a bit, to match their preference.
Here are some various code styles available out there.
1. Python
2. PHP
3. Javascript
And there are still a lot of other code styles out there. As we can see, some organization such as the one behind Python already create a code style named PEP 8, however suddenly Google also released their own (it might be based on PEP 8, I’m not sure but I’m sure they have differences). This also applies to different programming languages!
Which one to use?
It really depends on which project you are working on. If you are working on your personal team project in university or just in your own startup, you can actually use any of them or even make your own. But even if it’s your own startup, I suggest you follow some available code styles out there. Why? Because I think there’s a reason for every single rule in the code style, it might make your code more readable by your partner. Yeah you might change a bit, but I still strongly recommend you to create your own style based on the available code style. If you are working on an open source project such as what’s available on GitHub, usually they already create a section for the code style (either in README.md or in CONTRIBUTING.md) they are using in the GitHub project, make sure you comply with that rules or your pull request may have been closed. What if we’re working on our own very personal project? I still suggest you to comply with any popular code style, at least you will become used to using the code style.
Final Words
Do you have any favorites code style in certain programming languages that worth to use? Or you have another opinion about using code style? Share your responses, suggestions, or simply if I make a mistake in this story you can tell me and we can discuss about it. That’s all for today’s story, for next week weekend story edition will be about how to comply code style without having to remember all of the rules.