 |
Ctrl-c, Ctrl-v
Posted by erikhatcher on June 11, 2003 at 03:02 PM | Comments (9)
I went to Joshua Bloch's and Neal Gafter's More Programming Puzzlers session. It was fantastic! I won't embarrass myself with how many of the ten puzzlers I got wrong. One piece of advice Bloch gave was to "copy-and-paste" declarations to avoid issues with mis-overridden methods like this:
public class Name {
public boolean equals(Name o) { ... }
// hashCode omitted
}
The error is that the equals method of Object is not really overridden because the signature should be public boolean equals(Object o).
I think the advice of copy-and-paste really should be more along the lines of "use great tools". In IntelliJ IDEA, for example, I'd simply hit Ctrl-o, select the equals method under Object, and press return - voila, method signature created automatically. In fact, several mistakes that were pointed out in this presentation could be avoided with the right tools.
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
Alt-Ins
Better yet Alt-Ins and let Intellij fill in the method for you :)
Posted by: jknowles on June 11, 2003 at 03:09 PM
-
Ctrl-c, Ctrl-v
For those that can't afford IDEA the Eclipse IDE has the same ability. CTRL-SPACE ouside of other methods will bring up a list of superclass methods that you might want to override.
Posted by: swpalmer on June 11, 2003 at 04:14 PM
-
really?
Looks like the signature is different because it's taking an arg of type 'Name' instead of object...
Posted by: kereszturi on June 11, 2003 at 04:38 PM
-
If in JBuilder
If you are using JBuilder, use alt-z m to do the same thing.
Posted by: bob on June 12, 2003 at 05:07 PM
-
If in JBuilder
oh, actually, I forgot, ctrl-j eq actually gives you a properly subtyped code snippet that is a good basic implementation for equals as a code template.
Posted by: bob on June 12, 2003 at 05:09 PM
-
Alt-Ins
In Eclipse,right click,then choose source-->Overwrite/Implement Methods,you even can assign a hotkey.
Posted by: sunliwei on June 12, 2003 at 07:03 PM
-
Or, better yet, look at PMD/CPD
The SourceForge project PMD, a java linting tool, has a sub-tool called CPD, or "Copy-Paste-Detect".
It's absolutely frightening to run that tool on a large code base.
Posted by: javalori on June 12, 2003 at 10:38 PM
-
Thanks!
Glad you liked the talk!
I quite agree that you should use good tools rather than copy-and-pasting the declaration if it's possible for you to do so. Unfortunately, not everyone has the option. Occasionally (or not-so-occasionally) most of us find ourselves programming with text editiors.
Regards,
Josh
Posted by: jbloch on June 15, 2003 at 02:50 PM
-
Alt-Ins
But where is the Ins key on my PowerBook?! :( Thanks for that tip!
Posted by: erikhatcher on June 16, 2003 at 06:50 AM
|