Multiply by -1 to change sign considered harmful

Recently, I had the honour to delve into the depths of some legacy code. The program handled some financial data, and there was a section where the value had to change sign. The exact details aren’t important to the story, so let me come up with a suitable example. Uh… payment!

The monetary value stored in the database was considered positive (or profit). When people pay you $10, you have a record with $10 in the database. What happens when you pay other people? You use a negative value. This keeps the column data storage logic consistent.

So the variables used in the program stored positive values of the amount you need to pay other people, performing calculations as normal. When it came time to store in the database, there was a last line of code before the database update:

decPayment = decPayment * (-1);

Was -1 a magic number (as in, it was hardcoded, and could be some other number)? Was the multiplication in error (maybe it was supposed to be addition)? Was this line of code even supposed to exist?

It turned out that the programmer wanted to change the sign of the variable value. This method of changing sign has some downsides:

  • Multiplication is expensive. Maybe processor chips are fast now, but still…
  • The intent is not clear (what is it trying to achieve?)
  • It has more characters than the more optimal way

So what’s the “more optimal way”?

decPayment = -decPayment;

You don’t have to multiply by -1 to change the sign of a variable. Just directly negate it.

  1. Will Dwinnell

    “The exact details arenโ€™t important…”

    Oops! One detail is important: Which programming language are you discussing? From the two lines of code provided it is not clear. Running a quick experiment (which is not definitive) in MATLAB, I found no measurable difference between the two techniques.

  2. Vincent

    @DK: yes, some people do that…

    @Will: It was C. Also VB.NET. And PowerBuilder. And SQL statements. Like I said, “The exact details aren’t important…” ๐Ÿ™‚

    I think the measurable difference is about the programmer who’s reading the source code, gets shocked. Or stunned. Assuming the programmer cares enough to figure what’s going on.

  3. Vincent

    Cees, the modern compilers will most probably compile the same machine code as you say.

    I’m worried that the modern *programmers* can’t understand the intent…

Comments are closed.