Communicate through meaningful names

01 Oct 2014

We humans have very sophisticated communication skills, and programming is a form of written communication, so we can take advantage of those skills. If you choose appropriate names for variables, methods, classes, etc., your code will be much easier to understand. Even having a conversation out loud about your program will actually make sense.

I remember some of my university tests. Teachers would often give us a block of code and ask us what’s the final value of a variable.

// Example program from a test.
// "What's the final value of `m`?"

int m = 100;

for (int a = 7, b = 15; a < 20; a++, b--) {
  c = a + b;

if (c % 2 == 0) {
  m += c * b;
} else
  m -= c * b;
  m *= 2;
}

I don’t know exactly what solving this is supposed to prove. I think it goes along the lines of “curly braces matter, indentation doesn’t” which IMO is sending a wrong message. It even implies that writing code like this is normal or even expected in our industry, and that your job might include deciphering the intention behind messes like this.

This kind of examples could've also been used to teach us what bad code looks like, why it's important to avoid it, and ways to prevent our programs from becoming like that. For a lesson on this I recommend reading Uncle Bob’s book Clean Code (particularly chapter 2 which talks about using Meaningful Names).