Rename your way to cleaner code…

Sometimes I see things like this…

if (input(DIGITAL_IN1) == HIGH)
{
   ...
}

if (input(DIGITAL_IN2) == HIGH)
{
   ...
}

if (input(DIGITAL_IN3) == HIGH)
{
   ...
}

This code led me to having to open schematics and figure out what digital input 1, 2 and 3 where.

So I used the free CodeBlocks editor refactor “Rename Symbol” feature to change their names to:

if (input(ROBOT_ON_FIRE_IN1) == HIGH)
{
   ...
}

if (input(BATTERY_OVERLOAD_IN2) == HIGH)
{
   ...
}

if (input(DUST_BIN_FULL_IN3) == HIGH)
{
   ...
}

…but using names that made sense for my application ;-)

Someone must have thought it was important to include the input number (IN1, IN2 and IN3) — possibly so they would not need to look through defines to see what value goes to what input — so I kept those in my names.

But now, when I find this code months down the line, I’ll immediately know which function is most likely detecting that the robot is on fire.

Pretty easy, isnt’ it?

Until next rename…

2 thoughts on “Rename your way to cleaner code…

    1. Allen Huffman Post author

      As much as I hate leaving the PIN number in, it may mean something. But, what if a hardware revision changes the pin? A new project I am working on has had pins moved three times so far.

      Reply

Leave a Reply to Dave Philipsen Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.