Tips for Programming Comments, White Space, and Indentation on Your BeagleBone
There’s no such thing as a perfect method of programming. Advanced programmers usually develop their own style. There are definitely standards for writing better code, however. If you’re working with a team, contributing to an open-source project, developing a program that will take several days or weeks to complete, or writing something that you may look at again a couple of months later, you should always do the following:
Comment your code. Typing descriptive comments requires just a slight writing effort that can make an enormous difference in the long run. With proper comments, you never need to figure out what a snippet of code does. The explanation is right there!
Use white space. Adding extra spaces between your variable names and functions, as well as lines between instructions, makes your code prettier and more readable. Most programming languages ignore extra white space, so there’s absolutely no issue in using it to promote readability.
For example, you can use white space for organization in the form of extra lines between instructions to create blocks of similar code, as in this example:
import Adafruit_BBIO.PWM as PWM #import objects from libraries import Adafruit_BBIO.ADC as ADC import Adafruit_BBIO.UART as UART import math #import libraries import time import serial led = "P9_16" #define variables for pins sensor = "P9_40" PWM.start(led, 0) #initialize modules ADC.setup() UART.setup("UART1")
Indent your code. Python forces you to use indentation, but many programming languages don’t. Believe us when we say that being lazy in your programming and not caring about indentation may lead to a lot of frustration if a bug occurs. The bug may simply be a missing or extra closing brace (}), which is easy to detect if your code is indented — and a pain to find if it isn’t.
The origin of the term bug comes from a literal bug in the system. The term was first used in computer science in 1946, when computer pioneer Grace Hooper revealed that the cause of a malfunction in an early electromechanical computer was a moth trapped in a relay.