2

I have this horrible habit of typing the below and not catching it until well into testing:

int i = 1;
int j = 2;
i =+ j;  //i equals 2, not 3 as intended; assign only, + is unary and works on the j

The correct version, of course, would be

int i = 1;
int j = 2;
i += j;  //i equals 3, as intended with additive & assignment compound operator

I have made this mistake a zillion times. It would not surprise me of there is some code out there with a bug in it that escaped a test case. There has just got to be a way to prevent this systematically. Any ideas?

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Stu Thompson
  • 38,370
  • 19
  • 110
  • 156
  • There is a unary + operator in Java? Or is there an implied 0 on the left side? I would have thought your code would produce a compile-time error... – Matthew Scharley Jul 01 '09 at 11:50
  • 4
    A pre-commit hook in your VCS that greps for "=+" :-) – balpha Jul 01 '09 at 11:51
  • What possible use does a unary + operator have? Absolute(x)? – Matthew Scharley Jul 01 '09 at 11:55
  • @Matthew Scharley: If for no other reason, then for symmetry with the unary minus, I guess. Doesn't pretty much every language have that? – balpha Jul 01 '09 at 11:57
  • 1
    Mathematically it is just the identity, but it complements the unary -. Some formulas, e.g. a rotation matrix, look nicer with explicit +. Though in light of this question I doubt it is worth it. – starblue Jul 01 '09 at 11:59
  • Oh wow... I just opened LINQPad and apparently C# has one too... learn something new every day... (for the record, it has the same effect as recorded above, and no it's not abs(x)). – Matthew Scharley Jul 01 '09 at 11:59
  • I've just grepped the trunk of my main project for =+ =- =* and =/, as a one off search, and all is clear :) – Stu Thompson Jul 01 '09 at 12:09
  • @Matthew Scharley: the unary + operator at least causes an "Unary Numeric Promotion", that is, eventual unboxing and/or widening conversion. (I don't come to a real use of that...) – user85421 Jul 01 '09 at 12:10

5 Answers5

10

Regularly use tools like PMD and/or Checkstyle. Ideally as part of your build process/continuous integration.

You might have to define a custom rule for this, because I don't know if any of those tools recognizes this as a problem by default.

This will not only catch this problem, but also hint at a lot of other potential problems as well.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Hmmm...using checkstyle but have not looked at the rules in ages and run it in an embarisingly long time. Need to go check that out again. – Stu Thompson Jul 01 '09 at 11:55
  • +1 for Checkstyle. Used it on a big Java project with Eclipse, with very clever rules (some default, other custom) it was great to catch stupid mistakes and stop worrying about code style/refactoring. Now i miss it dearly on my new, 1 person, C# project under Visual. T_T – Ksempac Jul 01 '09 at 12:10
10

Depending on what IDE you use, if it does syntax highlighting, I would modify the highlighting so that it looks for the pattern =+ and makes it some awful-to-look-at color.

Matt Bridges
  • 48,277
  • 7
  • 47
  • 61
1

Use standard text utilities, such as

find . -name \*.java -exec grep -H "=+" {} \;
Christoffer
  • 12,712
  • 7
  • 37
  • 53
  • Great for a one off check, but I want as part of my development process. (Just ran it, all is good in my main code base!) – Stu Thompson Jul 01 '09 at 12:04
0

I think you have to be rigorous in your unit testing. At the moment you're worried about one particular issue, but good unit tests will capture most issues (ignoring threading/loading etc.).

You have to decompose your classes/methods such that you can test each chunk of functionality and ensure complete (or as near as you can) coverage.

Clover (commercial) or Emma (open source) will manage code coverage for you.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Unit tests in practise should be relatively straightforwards, however. I tend to be concerned if a unit test has the complexity of the code it's testing, and it's not obvious as to what is being asserted. If that's the case then I'm not convinced the code has been structured/designed with testability in mind. (Note that all the mechanisms mentioned in this thread can have bugs) – Brian Agnew Jul 01 '09 at 12:03
  • Also, as I work in a small, dynamic start up and not a Bank/NSA/whatever, I do not (and will not) have 100% code coverage + 100% test coverage. – Stu Thompson Jul 01 '09 at 12:06
  • I don't think being in a bank/whatever is a prerequisite. My work for non-corporate clients has the same coverage metric as for any other. I really think it's the solution, not just for this problem, but for most issues (whether typos or others) – Brian Agnew Jul 01 '09 at 12:08
  • I'd also like to catch it on the spot. Immediate negative reinforcement is what they call it, I think. Probably going with the most popular answer here... – Stu Thompson Jul 01 '09 at 12:11
0

As balpha commented, an easy way to find this would be to grep for the "=+" in code. It is probably never intentional.

Avi
  • 19,934
  • 4
  • 57
  • 70