Structuring code is a good programming practice. It makes code reusable and maintainable. The objective is to make clean code which is understandable. Structuring is not only limited to coding, python package and resources should be well structured to, we will discuss that later. In python the code executes line by line from line one to last unless referred by a pointer or function. In this article we will see some methods through which we can improve structuring of our code to make it clean, readable and easy to understand.
Comments are essential part of good programming. They are written as a instruction or to remember the function performed by developer. Comments not only make code easily readable for developer but as well as for testing teams and designers. Comments are optional but an important part and good programming practice. How to write comments in python? Any sentence followed by a “#” is comment. Comments do not affect program flow as they are ignored by compiler. Comments play an important part while the maintenance and reuse of code. It also helps trace error in code. For example
Whitespaces before a statement are important in python because it can affect indentation and python syntax needs proper indentation. So, whitespaces should be used carefully. But in other cases, whitespaces can be used freely as they do not affect code such as while declaring a = 2. Spacing should be proper so that code is well structured and easily understandable. They are important in separating the keywords from the variables. Let us check through the example of how whitespaces improve the structure of the python program. Line continuation:
While programming complex systems sometimes long statements are written in a single line mostly inside loops and conditional statements. That leads to long statements line that is scrolled to read them fully every time. To overcome that problem python allows to write a single statement in multiple lines, so we will simply divide that one long statement into two. There are two types of line continuation in python.
- Implicit Line Continuation: In implicit line continuation just put the whole statement inside the parenthesis () and write a statement in multiple lines like in the example. According to python guidelines, the implicit line continuation method is more appropriate and recommended
- Explicit Line Continuation: In implicit line continuation just put / symbol at the end of each line. This symbol is known as an explicit line breaker.it divides a single unreadable line into multiple easy-to-read lines. So, a long string is divided into three lines making it readable. But there is one problem with explicit line continuation. Do not put a blank space after an explicit line breaker it will give a syntax error, this point is important to remember. Let us see on the compiler.
https://www.python.org/dev/peps/pep-0008/Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.
Practice your Code online here
Software engineer| technical writer and a cybersecurity enthusiast