Newlines are free.

Avoid creating really long lines of code.

Many programming languages allow you to contain a frightening amount of logic in a single line of code. This can lead to very dense code that is hard to read. This style of writing has its origins in command line interpreters, a method of entering commands that predated visual editors.

While that may have been convenient fifty years ago, it causes problems for us today. One of the goals of your code should be to make as easy to read and understand as possible. Remember, when writing code, you are composing a message to future programmers that will try to communicate your intent. Thus, the clearer the message, the better.

It does not cost anything to add newlines to your code. Thus, one suggestion is to avoid making long chains of methods on a single line of code. Instead, it will be much easier to read if each method call is on its own line. (Also, long chains of method calls tend to create brittle, tightly coupled code.)

For many years, about 80 characters was the rule of thumb for the maximum length of a line of code. This was because many visual editors used terminals that rendered on a screen that was only 80 characters wide. With long, descriptive class, method, and argument names, it is easy to go past this size. Ultimately, if people have to scroll windows horizontally to see all the code, it will be more difficult to read and understand.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: