Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to use whitespace while programming? [closed]

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.

like image 947
Immanu'el Smith Avatar asked Jun 14 '10 17:06

Immanu'el Smith


People also ask

What is the purpose of whitespace in programming?

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.

Is whitespace good in code?

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.

How should I space my code?

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.

Why is white space an issue in clean coding?

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.


2 Answers

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.

like image 130
jjnguy Avatar answered Oct 07 '22 00:10

jjnguy


Thing almost everyone will agree on:

  • Put spaces after commas
  • Put newlines after semicolons (with the exception of for loop declarations)
  • Indent the body of all blocks (anything inside {}) by one tab or four spaces (your choice)
  • Put a newline after a closing } 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 */
}
like image 23
JSBձոգչ Avatar answered Oct 07 '22 01:10

JSBձոգչ