Skip to main content

Coding conventions part 2 : Aligning Things


“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.
As I have said earlier there are a lot many styles to align code(I know I am being ‘redundant’ here). I will only explain the best one(Lazy programmer. Hell yeah!).
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:
    See, In second example each class has its own goal and at the end objects of these classes are packed together to provide single ultimate solution to all the things related to file handling.
  • 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

Popular posts from this blog

Kotlin Coroutines : A Comprehensive Introduction

Photo by Fleur Treurniet on Unsplash “Redesigning your application to run multithreaded on a multicore machine is a little like learning to swim by jumping into the deep end.” —Herb Sutter, chair of the ISO C++ standards committee, Microsoft® In this article What are coroutines? Blocking vs. Non-Blocking Kotlin Coroutines Suspending functions CoroutineScope Coroutine builders Coroutine dispatcher Coroutine start Conclusion What are coroutines? Coroutines have been around for quite a time now. They are in-fact one of the ideas which helped develop multitasking operating systems. A coroutine in trivial most is a subroutine or function generalisation which in non-preemptive environment (operating system or OS in short) can voluntarily yield the CPU time so that other such sub-routines can use it for themselves without losing the results of previous computations and then can conti

Programmer? A Computer Programmer?

“Programming is like sex. One mistake and you have to support it for the rest of your life.” —Michael Sinz Well, to start with, “A programmer is someone who writes computer code, which tells computer what to do? and how to do it?” . Computing machines are dumb, trust me they are. They have enormous processing power but they don’t have hardware to direct this power to any productive use. And thats exactly where programmer kicks in. Programmers however are not the wizard depicted in the movies. They are humans like me and you. Its just that they think much more rationally than average human does. Types of programmers: The System Programmers: First of all a great tribute to these guys. They are the one who deal with software at the lowest level. They have veteran low level languages (languages which closely resemble with the hardware) —Assembly Language, Machine language in their arsenal. A typical system programmer works closely with hardware engineers bec

Kotlin : An introduction

Photo by wu yi “A language that doesn't affect the way you think about programming is not worth knowing.” ―Alan J. Perlis World was nice and there was only assembly(“real programmers code in assembly”) but then one day out of blue he (hey! don't look at me like that I too don't know his name) came and said let there be compilers and interpreters(line by line) that will translate languages easy to read(uh! nah not really) and write into assembly to make programming fun again and that gave birth to an era of exotic new programming languages called procedural programming languages. Procedural programming languages were doing fine but after some time lot's of programmer started loosing their objects because of stateless functions which globally caused chaos. Seeing all this made him angry. So he said, “Procedural languages, I command you to treat everything as an ‘Object’ which will have it's own properties(attributes) and behavior(methods)”. They respond