Some times, I need to copy a block with a variable, for instance:
Address addr = list.get(0);and paste it to create something similar like this:
assertEquals("city", "Uppsala", addr.getCity());
Address addr2 = person.getAddress()Did you see the bug?
assertEquals("city", "Uppsala", addr.getCity());
The trick I use to avoid bugs like this, is to change the variable name in the original block:
Address addr1 = list.get(0);Then the bug becomes obvious.
assertEquals("city", "Uppsala", addr1.getCity());
Address addr2 = person.getAddress()
assertEquals("city", "Uppsala", addr.getCity());
I can't tell you how many times this has saved me.
5 comments:
Hi,
Good little suggestion given.
Thanks
Prashant Jalasutram
http://prashantjalasutram.blogspot.com/
copy & pasting sourcecode is an extremely bad programing practice. IDEs should get rid of this "feature".
@martinus: That's too general, but I agree that code duplication is a huge problem.
With super-cool IDEs like IntelliJ the unused variable is highlighted, so it will become blatantly obvious that a variable is unused. In your case, addr2 would be highlighted.
Anyway, cool suggestion
Post a Comment