Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Eclipse's $FALL-THROUGH$ comment standard?

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)?

like image 293
Tustin2121 Avatar asked Oct 09 '12 19:10

Tustin2121


3 Answers

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.

like image 150
Dave Newton Avatar answered Oct 23 '22 16:10

Dave Newton


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;
  ...
}
like image 40
Duncan Jones Avatar answered Oct 23 '22 16:10

Duncan Jones


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.

like image 30
ali köksal Avatar answered Oct 23 '22 14:10

ali köksal