Code comments should help you and other developers understand the code. Or “why project code standards can result in unwanted behaviors”

The purpose of commenting your code is to help you and other developers that come after you to understand the code. If a code comment doesn’t offer anything in addition to the code itself then it’s pointless and just adds noise.

Method and variable naming is also important, a descriptive method name shouldn’t need copious additional inline comments if it already describes what it does. Obviously you don’t want the extreme and have a method name that’s hundreds of characters long. Be sensible.

I often come back to these 2 Twitter posts that illustrate how inline comments that state the obvious are useless:

Another one making the same point:

There’s many reasons why developer’s comments add little value. The main reason is probably just not putting in the effort and time to write something that’s useful. There’s another reason though that leads to documentation that looks exactly like this:

/**
 * This method retrieves an Account.
 * @param accountId the account id
 * @return the Account
 */
public Account retrieveAccountById(String accountId){
    // code here
}

First, the name of the method, the parameter and the return type tell you a lot about the purpose of this method. So why would anyone take the time to write “This method retrieves an Account” that clearly just repeats what the code already says?

Have you ever seen statements like these:

  • Every Class must have JavaDoc that describes the Class
  • Every method must have JavaDoc that describes the method and it’s parameters

This is an example where standards encourage unexpected or unwanted behaviors. The intent of both of these statements is good. In most cases it is beneficial to have documentation that helps others understand what the code is and what is does. There is a point however that if you’ve named your method, it’s parameters and it’s return clear enough, there isn’t anything left to say in the documentation for the method.

“But the code standards say we must write JavaDocs, so I have to write something!”

Standards and rules are established for a purpose, but they have to add value. Be flexible. If something doesn’t add value, you shouldn’t follow it blindly. Do what makes sense.

Displaying current git branch in MacOS zsh prompt

Previous script for setting git branch in bash shell is here.

For zsh, here’s an approach from this gist – add to your ~/.zshrc:

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

Troubleshooting serial terminal connections (VT Serial Terminal, VT132, Packet Radio TNCs) – part 2

In my previous post, I said I was going to test my two serial cables with every serial device I have to work out which combination worked and which didn’t to find out what the difference was. It didn’t take long to realize though that the DB25 to DB25 serial cable I have that I assumed was a null modem cable only worked with certain types of connections.

For example, it only worked with a VT terminal to a modem type device (in this case a PK-232 packet radio TNC), but not terminal to PC. In the first case that is a DTE to DCE type connection (which worked), whereas the second is DTE to DTE (which didn’t).

  • A DTE to DCE connection requires a straight through cable
  • A DTE to DTE requires a crossover connection (tx to rx, rx to rx)

This realization pretty much confirmed that the cable that was only working for DTE to DCE connectors was a straight through cable, and explained why it didn’t work elsewhere.

Long story short, I picked up a cheap null modem adapter that does the crossover for you, converting a straight through cable to a crossover:

From left to right:

  • USB serial dongle, connected to the Pi
  • DB9 to DB25 converter
  • DB25 to DB25 null model adapter
  • DB25 to DB25 straight through (converted to crossover with the null modem adapter)

And now I can successfully get a terminal logon to my Raspberry Pi:

To enable the serial terminal login via ttyUSB0, see this post.

Enabling serial tty login to a Raspberry Pi

Depending on what Raspbian version you are running on your Pi, the approach for enabling a serial tty login via a VT terminal differs, but on current/recent versions you can enable by enabling and starting this systemd service (steps from this post, and here):

sudo systemctl enable serial-getty@ttyUSB0.service

and then:

sudo systemctl start serial-getty@ttyUSB0.service

This assumes you are using a USB serial dongle and that it’s connected as /dev/ttyUSB0. You can check by doing a ‘ls /dev/ttyUSB*’ before you connect your USB serial adapter and after to check what device your USB dongle appears as.

If you’re running an older version of Raspbian not using systemd, you can add a line to /etc/inittab to initialize getty as described here.