Trivial issue, but comes up a lot for me, and I imagine others too. Does anybody have a really good, really clever solution?
void some_function (obj &A, obj &B) { // do stuff with A... //e.g. double number_A = (value - A.member_func() ) * A.other_func(); // do stuff with B. similar BUT NOT EXACTLY like A... //e.g. double number_B = (value + B.member_func() ) * A.other_func(); // !!!! // Big time TYPO - should say "B.other_func()", not "A.other_func()" !!!! // !!!! } Any good guards against these types of errors?
I often have to work on two analogous variables, say one named version "A" and the other "B".
Because the code for each one is similar, I often use the code that worked on "A" as a "template" (i.e. copy & paste) for the code that works on "B" - making the small adjustments so that the code becomes appropriate for B.
Becuase I am human, I sometimes forget to change "A" to "B" in certain locations when copying the code. If I am lucky, this will cause the program to crash. Either way, this is disastrous.
Does anybody know any clever tricks for preventing such typos?
I've thought of...
- enclosing brackets
{ }to try to restrict the scope of variables - but if objects A and B are in the function arguments, then this doesn't solve it. - dividing every function into sub-functions - one for A and one for B. This is a bit cumbersome from the developer perspective (passing/returning many of variables).
- work only with pointers to the objects themselves - not the actual objects. This way we can
{ scope-control }the pointers. Also cumbersome, (and the overhead for defining a pointer is negligible, even if I call the function very, very often, right?)