Is it just me, or is Java's Override annotation actually useless when programming in a decent IDE?
Yes, I understand that the Override annotation prevents me from accidentally not overriding a super method b开发者_开发技巧y misspelling the method name or messing up the method parameters. However, common sense tells me this is very unlikely to happen in modern IDEs, for the following reasons:
- The IDE shows a marker (i.e. a small icon) on the left side of the editor that indicates whether the method is overridden/overriding. Therefore, no explicit Override annotation is needed to show the Override intention.
- If I'm about to rename a method and I see such an override marker, I never, ever edit the method name directly - I instinctively reach out for the keyboard shortcut that opens the IDE's renaming assistant. Likewise, if I intend to change the method signature and I see the override marker, I instinctively use the IDE's method signature assistant instead of doing it by hand (except for very simple cases). Method names and signatures are thus almost always changed correctly, so adding Override annotations becomes (in my opinion) little more than a paranoid and useless safety check - same as spreading 'final' all over the place.
Sure, if you have to write your Java code in Notepad or whatever, the Override annotation obviously helps. But my point is, in a modern IDE, the increase in safety provided by this annotation is not worth the decrease in readability. Any opinions?
Well you have to trust yourself to see those markers. There is a human element to it and humans are fallible. From personal experience I often tend to overlook those warnings and 'markers'. On the other hand, the compiler is far more exacting and if you annotate a method with Override it will make sure that you are really overriding a superclass method.
If you @Override something which is not overridden this is obvious in an IDE. However if you remove a method or change its signature in a parent, it is not obvious all the places that used Override this method signature. Using an IDE to perform the remove/change will help but you may not get every reference if the right module/version is not checked out and used.
Yes, it is pretty much useless. For people who find it useful, good for them. But the scenarios they used to justify it is not realistic. The fact is, many don't use @Override, and they never encountered any problem it supposedly solve.
python dude: you didn't mention keyboard shortcut(Ctrl-O in IntelliJ) when overriding a method. I never manually type signature of the method I'm overriding, so there won't be typo to begin with.
精彩评论