“Programs must be written for people to read, and only incidentally for machines to execute.”
—Harold Abelson
Hello there, welcome to another post in the “Coding conventions” series. This post is solely based on question, “How to align code?”. If you are coming to this post directly then it is highly recommended that you read this post about naming different sections of code first. It contains both introductory talk about coding conventions and their importance and tips on naming things viz. methods, classes and variables.
We humans are naturally in love with symmetry(tell me if I am wrong). Symmetry of faces, colors, things and their organization. A well-designed and aligned piece of writing makes reading both easy and fun. And when learning is fun remembering is easy. Undoubtedly things are no different in programming. A well aligned code is expressive and eye soothing. Correctly indented code blocks divided into equally sized chunks of statements really enhances quality and maintainability of code. Always remember this,“A programmer is by heart poet of logic and program is his/her poetry”. So, program must be treated the same way.
There are numerous ways to align code and At very heart of each of them are rules. Rules which can be summarized into following statements:
- Position of opening and closing parenthesis Or brackets(U.K. English) after blocking statements(I'll explain what are they? Latter).
- Number of Tabs or spaces to be inserted after statements belonging to sub Or child blocks.
- Ideal count of number of characters to be written in each line of code.
Not too fast kid!. Before moving onto alignment section and flooding your “biological storage drives” with rules and do's and don't. Let me first introduce you to some words crucial to matter followed; which belong to programmer's glossary.
- Indentation:
- An indent is simply that extra space added just before starting code statement to reflect the parent-child or belonging relationship between unintended and intended block of code. It can be done either by using tab character or space. (1 tab is roughly equal to 4 space characters).
- Blocks:
- A code block is collection of code statements grouped together to perform or complete single ultimate goal. For example body of function.
Now, as you know what blocks and intends are, let's move on to some tips to make your code beautiful.
- Be consistent both in code and life. If you have used space for intending code in one file then use spaces in each and every file in that code. This rule also applies to the style of brackets that you are using.
- Don't intermix spaces and tabs at least not in the same file. Mixing spaces and tabs can cause serious issues that's why languages such as Python has completely prohibited doing so. The root reason behind this is that every OS represent tabs using different amount of space characters, some use 4 spaces to represent one tab other might use 8. And when you transfer code from one OS to another conversion has to be done by Integrated Development Environment and this might render code useless.
- Keep it detailed Keep it simple always. Think of code as piece of poem. Poems has definite structure each stanza(paragraphs in poems) consist of only those lines which have meaning in current context which are there to complete the goal in current context. Let me give you few examples.
- Incorrect way:
- Correct way:
- If it doesn't fit in one line then it doesn't have to be. Exactly, sometimes single statement of code becomes so large that it is hard to remain in limit of 80 characters. In these kinds of cases try breaking a single line into multiple sub lines. Follow style used by Java developers.
Okay, that's all for this one. If you have any question you can ask them in comment section, I will try to respond ASAP. And do follow this blog(show some love naa ❣). Have a nice time.
Comments