Over the past few years, I have learned many different programming languages. Having typed program after program (“Hello World” after “Hello World”), I have come up with a very specific programming style for formatting my code.  Instead of just throwing a bunch of code into a text editor – hoping that I can decipher it when I look at it again in a few months – I carefully indent all of my code to the correct width, and use a lot of comments. Below, I have put together some of my most-used practices so that you can (hopefully) benefit from and build off of them.

1. Indenting Two Spaces

One of the most obvious ways to make your code more readable is to indent all of your lines to the correct width. I usually use two spaces for my tabs, an option you can specify in most text editors.

Indenting 2 Spaces

2. Code Bracket Placement

As I said before, it’s always nice to keep your code style the same throughout all of your code. One of the things I am a little “finicky” about is the placement of the code brackets that surround my code blocks (if statements, while loops, etc.).

I place my beginning bracket one space after the ending parenthesis of the code block header.  Take a look at this image to see what I mean:

Placement of Beginning Bracket

3. Comment, Comment, Comment

Alright, I admit it, I’m a comment freak.  I go wild with comments all over the place, and I have descriptions of almost every line of code in my files.  I find that if I stick to one format for all of my comments, it makes the overall readability a whole lot better.

When I comment whole sections of code, using the comment as a sort of “header”, I put my header text inside a multi-line comment, and place this right before the beginning of the section of code that I’m describing.

Header Comments

When describing just a single line, I use a single-line comment, and tab it one or two tabs from the line I’m describing – like this:

Single Line Comments

4. Parenthesis and Brace Padding

To make my code more readable, I add spaces inside all of my parentheses and square braces as a kind of “padding” for them.  This can look strange at first, but if you get used to it, you’ll see how much more readable it can make things.

Parenthesis Padding

Conclusion

I hope that everyone who reads this post gets something out of it, and applies it to their own coding.  Now don’t misunderstand me – this is not the only way to format your code – there are so many different ways you can do this, and the important thing is that you find the style that you’re comfortable with and stick with it.

Until next time, happy coding!