我有一个可怕的习惯,那就是输入下面的内容,直到进入测试阶段才抓住它:
int i = 1;
int j = 2;
i =+ j; //i equals 2, not 3 as intended; assign only, + is unary and works on the j
当然,正确的版本是
int i = 1;
int j = 2;
i += j; //i equals 3, as intended with additive & assignment compound operator
我犯了这个错误无数次。有一些代码中存在一个逃脱测试用例的错误,这并不让我感到惊讶。必须有一种方法可以系统地防止这种情况发生。有任何想法吗?