Python Network Programming
上QQ阅读APP看书,第一时间看更新

Indentation

This is similar to what we do when we write in plain English. Indenting a program is mandatory in some scripting languages, but as a best practice it should be followed for any program that we write in any programming language. Indentation improves the readability of a program because it helps the programmer or someone else reading the program to quickly understand the flow of the program.

Let's see an example where we have a nested condition in which we check if a Car is Red and if it is a Sedan and if it is Automatic.
A bad way of writing this would be as follows:

if (Car is 'Red')
if (Car is 'Sedan')
if (Car is 'Automatic')
do something

Now, think of adding multiple lines like this to a long program, and you will get easily confused by the flow of program as you read through it.
A better and recommended way to write this is as follows:

if (Car is 'Red')
if (Car is 'Sedan')
if (Car is 'Automatic')
do something

This provides a clear flow of the program. Only check the other conditions if the Car is Red; otherwise, don't check for the other conditions. This is where we say we are nesting the conditions inside each other, which is also called nested conditions.

This also clears a lot of confusion while troubleshooting a complex program. We can easily identify the problematic code or instructions by quickly parsing through the program and understanding the flow for each segment of the program.