Java constant with Underscore

862 views
Skip to first unread message

dave.go...@apconsult.de

unread,
Oct 10, 2015, 3:45:18 AM10/10/15
to SonarQube
Hi,

I recently received a mail from a colleague, asking me to stop using underscores in Java constants because of SonarQube.

Now, this has been part of the Java language for over 4 years, so when will it be supported by the sonar parser?

All the best,
Dave

Patroklos Papapetrou

unread,
Oct 10, 2015, 12:58:15 PM10/10/15
to SonarQube
Hi Dave

Are you sure about that? The only rule related to Constants in java is this one and it suggests that you don't start the constant variable with underscore.
You can use it anywhere else.
Are you talking about something else?


Patroklos Papapetrou

SoftwareGarden.io

Linkedin | Twitter | Author Speaker | Blog | Google+ | GitHub |

Dave

unread,
Oct 10, 2015, 2:03:01 PM10/10/15
to SonarQube
Patroklos,

I'm not talking about a rule.  Apparently SonarQube parses constants with underscores incorrectly.

Dave

Patroklos Papapetrou

unread,
Oct 10, 2015, 2:17:21 PM10/10/15
to SonarQube
Sorry but I'm confused.
What kind of report did you get from SonarQube regarding java constants. I mean how did you understand that SonarQube is complaining about underscores in Java constants?

Patroklos Papapetrou

SoftwareGarden.io

Linkedin | Twitter | Author Speaker | Blog | Google+ | GitHub |



--
You received this message because you are subscribed to a topic in the Google Groups "SonarQube" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sonarqube/3UFuk4jLC0E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/6e1798ae-992c-45c9-84e4-ef13cdfa9428%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Dave

unread,
Oct 10, 2015, 2:22:31 PM10/10/15
to SonarQube
Unfortunately, I didn't get a report, only the information from our guy who is running it, that SonarQube parses it incorrectly.
He says SonarQube uses some proprietary parser.

Patroklos Papapetrou

unread,
Oct 10, 2015, 2:27:21 PM10/10/15
to SonarQube
Well something is not right here :)
I'm using SonarQube for several years and I'm pretty sure that it never complains about using underscore in java constants.... Can you please ask your colleague to share with you the rule?

Thanks

Patroklos Papapetrou

SoftwareGarden.io

Linkedin | Twitter | Author Speaker | Blog | Google+ | GitHub |



Dave

unread,
Oct 10, 2015, 2:34:19 PM10/10/15
to SonarQube
Patroklos,

I'm trying to get more information from my colleague.
In the mean time, do you actually have any code with underscores in constants?
(in particular, hexadecimal constants, which is where our problem seems to be)
Its a very useful feature, but is not widely used.

All the best,
Dave

Michel Pawlak

unread,
Oct 10, 2015, 4:06:51 PM10/10/15
to SonarQube

Dave

unread,
Oct 11, 2015, 3:23:56 AM10/11/15
to SonarQube
Hi Patroklos,

ok, I tried the SonarQube Get Started in 2 Minutes.
It is an error in the SonarQube parser.
Here is some test code:
    public int LOW_ORDER_30_BITS_INT_RAW = 1073741823;                          // ok
    public int LOW_ORDER_30_BITS_INT_FMT = 1_073_741_823;                       // ok
    public int LOW_ORDER_30_BITS_HEX_RAW = 0x3FFFFFFF;                          // ok
    public int LOW_ORDER_30_BITS_HEX_FMT = 0x3F_FF_FF_FF;                       // ok

    public int INT_FMT_AND_INT_CONST     = 123_456 & LOW_ORDER_30_BITS_HEX_FMT; // ok
    public int INT_RAW_AND_INT_RAW       = 123456  & 1073741823;                // ok
    public int INT_RAW_AND_INT_FMT       = 123456  & 1_073_741_823; // NumberFormatException
    public int INT_FMT_AND_INT_FMT       = 123_456 & 2_073_741_823; // NumberFormatException

All the best,
Dave

Dave

unread,
Oct 11, 2015, 5:46:23 AM10/11/15
to SonarQube
ok, after some research, it seems this Issue is fixed in the current Java Plugin (v3.6).
(The current SonarQube is packaged with the v3.0 Plugin & LTS even older)

All the best,
Dave
Reply all
Reply to author
Forward
0 new messages