I use Eclipse, so to me my use of //$FALL-THROUGH$
comments is common practice on switch statements and the like. But my coworker uses Netbeans and questioned what on earth I was doing with these. And trying to google anything with a symbol in it is like trying to pull teeth with a pair of frozen mittens and no tools...
Is the use of the //$FALL-THROUGH$
comment, dollar signs and all, a standard java thing, or some sort of Eclipse magic? If I load up the same code in Netbeans or another IDE, or run it through the standalone java compiler, will fall through switch statements still be marked with warnings, even with said comment or not? Is there a standard way to do this beyond using the @SupressWarning
annotation (which would simply clutter code given how you have to use it)?
Things like this are IDE/style-check-tool dependent. There are no "standard Java comments".
(Well, Javadocs are standard, but they don't control compilation/error reporting.)
An explanation of the $FALL-THROUGH$
construct is mentioned in eclipse release documentation:
The compiler problem for expected fall-throughs in switch case statements can now be suppressed by preceding the following case statement with a comment that starts with $FALL-THROUGH$. This is especially interesting for code that can't use the J2SE-5.0-style @SuppressWarnings("fallthrough") annotation.
Note that warning names for the @Suppresswarnings
annotation are compiler dependent, and not a Java language standard.
I've seen other tools (e.g. FindBugs) that employ a similar commenting scheme. But nothing is standardised.
In most cases, these warnings are only thrown when the case statement contains code prior to the fall through. In most cases, you can avoid this by factoring out the common code.
For example this will not normally trigger a warning:
switch (foo) {
case 1:
case 2:
doSomething();
break;
...
}
But this would:
switch (foo) {
case 1:
doSomething();
case 2:
doSomethingElse();
break;
...
}
I personally think the warnings are there for a reason. I consider it more readable (and safe) to refactor out common code and have no non-empty fall-throughs:
switch (foo) {
case 1:
doSomething();
doSomethingElse();
break;
case 2:
doSomethingElse();
break;
...
}
This comment is IDE specific and may fail with other ide and code checkers, although findbugs is smart enough to check comments with word fall in them. But, in any case you should not rely on comments to do more than direct the fellow programmer, you are just creating one more thing to maintain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With