Best Practices for Code Best Practices
I’m of the opinion that code is meant to be read by humans. What a crazy thought, right? If you’re writing code, it should be readable, and it should be easy to understand. This is the most important thing. You shouldn’t need to depend on a friend or colleague who previously worked on the same codebase in order to work with it. It should be easy to understand and follow.
When I first began working at PEAR Deck, we had a lengthy list of “best practices” that every developer was required to follow. I asked my manager if there was any particular reason why we followed these best practices. The response I got was something along the lines of “Some people wrote them down and said we should follow them.” That doesn’t seem like a good reason for following them, so I decided to do some research and see if there were any actual benefits behind following each of these rules.
Peardeck Code is a blog dedicated to helping software developers write quality code. The site features articles and blog posts on best practices and tips for writing great code.
The content on this site will be developed by the contributors of the blog, who are Senior Software Engineers at Peardeck. Many of these contributors have worked at companies such as Google, Microsoft, Amazon, Facebook, Intel, Palantir and more.
The mission of Peardeck Code is to help software developers write clean code and write it well.
It is a well known fact that “best practices” help developers write better code and lead to better development processes.
This blog will provide a best practice for writing best practices.
In my time as a software engineer, I have seen many different styles of code. From the best to the worst, I have seen it all. Some may say that styles are personal and there is no way to improve them. However, after reading this blog post, you can be sure that your code will be better than ever before.
Let’s start with the basics: indentation and commenting. Indentation is an important aspect to code styling, because if you don’t indent your code then how would anyone know how to read it? The logic behind how you should indent your code is simple: every time you see an opening bracket ( { ), you should indent your code once. Every time you see a closing bracket ( } ), you should unindent your code once. This logic can be applied recursively.
The next step in writing beautiful code is using comments. If a function has more than one line of code, then I believe a comment is necessary! Comments are great for adding extra information about what the function does and why it does what it does! In order to tell the computer that this line of text is a comment and not actual code, we use something called a “hashtag.”
At Pear Deck, we’re writing a lot more code than we used to. As a startup matures, the amount of code you need to write can increase exponentially. When you’re this small, it’s easy to just ask around or get on G-chat and discuss something with another developer. But as you grow, that won’t work anymore.
So how do you standardize your code? How do you make sure developers are writing consistent code? Here’s what we’ve done so far.
Write down best practices
The first step is to write down some best practices in a centralized location. We use a wiki for this because it makes it easy for everyone to contribute and find information. We have one page for common conventions that all developers should know, and also specific pages for each language we use (Javascript, Java, Ruby). This is also where we store documentation about our systems and processes.
If you can’t find a guideline in the wiki, ask on Slack, then add it to the wiki. If someone else asks the same question on Slack later, point them at the wiki (and hope they actually read it 🙂 ) . You want the information to be available so people don’t have to ask again. If they do ask again, remind
The best way to write quality code is to understand the fundamental principles behind all of your programming languages.
We’ve all seen code best practices. Some are helpful, others are just silly. But have you ever really sat down and thought about what makes a good code best practice?
Take this for example:
int i = 0;
for (i=0; i<10; ++i) { // do a thing } That's not good code. It's bad code, in fact. Why? Because it skips the first value of i when iterating! It should be written like this instead: for (int i=0; i<10; ++i) { // do a thing }