Skip to content

Formatting

You should take care that your code is nicely formatted.

The Purpose of Formatting

Code formatting is important. Code formatting is about communication, and communication is the professional developer’s first order of business.

Vertical Formatting

Small files are usually easier to understand than large files areal formatting tells about How big should a source file be?

Keep our source files are small because Small files are usually easier to understand than large files are.

The Newspaper Metaphor

We would like a source file to be like a newspaper article. The name should be simple but explanatory. The name, by itself, should be sufficient to tell us whether we are in the right module or not.

The topmost parts of the source file should provide the high-level concepts and algorithms. Detail should increase as we move downward, until at the end we find the lowest level functions and details in the source file.

Vertical Openness Between Concepts

separate the package declaration, the import(s), and each of the functions with white space.

Vertical Density

If openness separates concepts, then vertical density implies close association.So lines of code that are tightly related should appear vertically dense.

Vertical Distance

Concepts that are closely related should be kept vertically close to each other. Clearly this rule doesn’t work for concepts that belong in separate files.

But then closely related concepts should not be separated into different files unless you have a very good reason.

Variable Declarations.

Variables should be declared as close to their usage as possible. Because our functions are very short, local variables should appear a the top of each function.

Control variables for loops should usually be declared within the loop statement.

Instance variables

The important thing is for the instance variables to be declared in one well-known place. declare instance variables on top.

Dependent Functions.

If one function calls another, they should be vertically close, and the caller should be above the callee, if at all possible. This gives the program a natural flow.

Conceptual Affinity.

Certain bits of code want to be near other bits. They have a certain conceptual affinity. Affinity might be caused because a group of functions perform a similar operation. So keep those functions near each other.

Vertical Ordering

a function that is called should be below a function that does the calling.

Horizontal Formatting

This suggests that we should strive to keep our lines short.

Horizontal Openness and Density

We use horizontal white space to associate things that are strongly related and disassociate things that are more weakly related.

Horizontal Alignment

Do not write like this:

1
2
3
4
private     Socket      socket;
private     InputStream     input;
private     OutputStream    output;
private     Request         request;

Write like this:

1
2
3
private Socket socket;
private InputStream input;
private OutputStream output;

Indentation

Do not write like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class FitNesseServer implements SocketServer { private FitNesseContext context; public FitNesseServer(FitNesseContext context) { this.context =context; } public void serve(Socket s) { serve(s, 10000); }}```

write like this:
``` java
public class FitNesseServer implements SocketServer { 

    private FitNesseContext  context;

    public FitNesseServer(FitNesseContext context) { 
        this.context =context; 
    }
     public void serve(Socket s) { 
        serve(s, 10000);
     }
}

Breaking Indentation. It is sometimes tempting to break the indentation rule for short if statements, short while loops, or short functions. Do not write like that follow indentation rule.

Dummy Scopes

the body of a while or for statement is a dummy. Some times we can not avoid them . Instead of putting semicolon in same line put that semicolon in second line. Like this:

1
2
while (dis.read(buf, 0, readBufferSize) != -1)
;

Team Rules

A team of developers should agree upon a single formatting style, and then every member of that team should use that style.