I'm fairly new to programming and from learning I have seen different ways of formatting code, comments, etc; and have been recommended on different techniques.
I mostly program in C#, C++, and Java so I want to know what is the the best way to layout code so that if other people where to go through it, they would be impressed by how simple and easy to understand it is.
Whitespace is any string of text composed only of spaces, tabs or line breaks (to be precise, CRLF sequences, carriage returns or line feeds). These characters allow you to format your code in a way that will make it easily readable by yourself and other people.
White space is essentially any bit of space in your code that is not taken up by „physical“ characters. White space is critical for organizing our code, because we tend to find some relations between “things“ to instinctively find a meaning in the composition.
Use only spaces, and indent 4 spaces at a time. Do not use tabs in your code. You should set your editor to emit spaces when you hit the tab key. Put it this way will make the code format always correct with all text editors.
Of course, readability can suffer from code being too dense as well as too spread out, so use your judgement. But in general, minimize use of vertical whitespace. Some rules of thumb to help when blank lines may be useful: Blank lines at the beginning or end of a function very rarely help readability.
The best rule to follow is: (and probably the only rule everyone will agree on)
Be consistent!
Pick one style and stick with it. Always put your braces in the same place. Use similar naming conventions. Don't mix tabs and spaces, etc...
That being said. It is also usually a good idea to try and follow the conventions already in place for a language.
When working on a team with people, make sure that you all agree upon a style and stick with it. If you don't, often times one developer will reformat a file with their auto-formatter, and then you can have hundreds of little conflicts that were just caused by whitespace.
Thing almost everyone will agree on:
for
loop declarations){}
) by one tab or four spaces (your choice)}
that ends a block (with a few exceptions)There are a lot more things than that which some teams will insist upon for consistency, but these elements of code formatting are universal.
There are basically two ways to deal with if
blocks and anything with the same format (for
, while
, using
, etc):
if (condition) {
/* code */
}
versus:
if (condition)
{
/* code */
}
This is purely a matter of preference. Pick one style and stick with it (or conform to the rest of your team).
One possible exception to the "newline after }
" rule is for grouped if/else if/else
, blocks, try/catch
blocks, or other closely-connected blocks, which many people prefer to space thusly:
if (condition) {
/* code */
} else if (condition2) {
/* code */
} else {
/* code */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With