• (nodebb)

    private static final int WTF_FRIST = 1;

  • (nodebb) in reply to Paddles

    private static final int WTF_SNECOD = 1;

  • (nodebb)

    Hmmm, the obFrist comment seems no funnier when I post it than when someone else does.

    Maybe the punishment for declaring magic constants like CONST_FOURTEEN = 14 should be that the next clueful developer works out which clueless developer is to blame and then replaces the constant name with a reference to that person e.g. PADDLES_IS_A_FOOL_FOURTEEN = 14 in the hope that someone, somewhere learns that lesson.

    Bonus points if the code is open-source.

  • (nodebb)

    Could be worse. My company's codebase still contains a comment that someone else wrote:

    * WARNING: This is temporary code to be removed by Steve in 3.6. It is clearly racy.

    The "Steve" in the comment refers to me. We will soon be releasing 4.7, and no, I haven't removed the code in question.

  • Pabz (unregistered) in reply to jkshapiro

    Do you have an off by 1 error there?

  • Smithers (unregistered) in reply to Steve_The_Cynic

    /* HM 2001-11-06: This code is not required and will be removed after the code review.. */ I was in school when that comment was written, so I can only hope "HM" did get that review. The block was finally removed by my colleague last summer, "after the code review" by a margin of 2 decades.

  • Smithers (unregistered)

    Feature request: a formatting preview button so I can see how my comment will be rendered (and have a chance to edit in the correct number of line breaks) before I submit it.

  • (nodebb)

    Wait! The use of "magic numbers" is wrong, but private constants is OK?

    if (something == 3 ) then {code;} <<< No good

    private static final int XXX_ONE=1; if (something == XXX_ONE) then {code;} <<< Good?????

  • (nodebb)

    I have this in my code:

    private static final int FIRST = 1;
    private static final int SECOND = 2;
    private static final int THIRD = 3;
    private static final int FOURTH = 4;
    private static final int FIFTH = 5;
    private static final int SIXTH = 6;
    

    But that's because that particular class is dealing with raw JDBC, which doesn't do binding of parameters by name, and I'm using a strict linter. It's ugly but the numbers don't mean anything more than what they are.

    A separate class in that codebase also declares MAGIC1 and MAGIC2 constants, but they're weird constants that get poked into firmware to configure FPGA registers and the people who knew what it all meant have retired. Multiple previous versions of that code (in separate languages) also used those names.

  • (nodebb)

    We're forced to create constexpr int k_XXX = 2; (etc) because SonarQube complains about code smells for all numbers other than 0 and 1. Too much code smell, and our PR will fail and can't be merged.

    So I have one named k_half = 2, used when centering some drawing code.

    At least most of our half meaningful names. Except for those in the unittest code (k_one, k_two,...). And they're in local namespaces within the given file.

  • Jonathan (unregistered)

    Well, the linter has a point.

    String formattedField = fieldFormatMethod(someArray[14]);

    isn't very good code.

    Clearly the 14th field has some meaning. If we were processing all the fields we'd see something like someArray[i]. So, to make it meaningful and satisfy the linter, lets suppose it's the SSN

    int ssnIdx = 14;
    String ssnFormatted = fieldFormatMethod(someArray[ssnIdx]);
    

    If you want to say that the linter is then going to complain about *that* 14, then how did

    private static final int XXX_ONE = 1;

    get past it?

  • Jonathan (unregistered)

    Oops, I meant:

    ...how did

    private static final int XXX_FOURTEEN = 14;

    get past it?

    (Seriously, we need a preview window.)

  • Pauller (unregistered)

    There's code that makes me go "WTF?" and code that just makes me cry. This is one of the latter. (Shaking my head ...)

  • MaxiTB (unregistered)

    "static final" doesn't make a constant, it makes an instance which cannot be overwritten after construction. A constant is a design time literal, "static final" is euqal to dotnets "static readonly".

    There is a major difference obviously, because constants can savely be fully inlined and optimized by the compiler (including precalculated math) while static members obviously are more limited in that regard.

  • Your Name (unregistered) in reply to dcon

    God, SonarQube is what happens if you let some bored hipsters loose. There are sometimes real bugs it finds, but so much is just incredibly stupid code smells. Fortunately in our company we've checked with security, and we are allowed to turn off stupid rules that do nothing but mess up the code for "major" bugs, like having no caption in a HTML table, or more than 4 IFs inside a function.

    They'd better put their energy to make it also scan without msbuild (any bigger decent project can NOT build with msbuild only). Every other static code analysis tool is able to.

  • (nodebb) in reply to dkf

    When faced with that, one of my coworker does this:

    int i = 1; statement.setString(i++, foo); statement.setString(i++, bar); statement.setInt(i++, whatever);

    It's still ugly, but it evades the "magic number" checker (0, 1 and 2 are usually excluded from the magic number rule).

    I'm not sure if I can say I like it, but it's definitely better than defining 12 useless constants with no meaning.

    Addendum 2023-04-12 02:50: Nice "edit" button we have here. It's a good to never be able to fix wonky formatting...

    Addendum 2023-04-12 02:50: or missing words...

  • (nodebb) in reply to MaxiTB
    "static final" doesn't make a constant, it makes an instance which cannot be overwritten after construction.
    Actually, it does. And according the JLS (4.12.4, 13.1.3), the compiler MUST inline "constant variables" (which includes static final primitives and Strings).
  • (nodebb) in reply to LordOfThePigs

    Interesting - how does that work if you set the member in the constructor with a value from the arguments? At this point the compiler cannot know which value the member will have, so how will it inline this unknown value somewhere else or did I miss anything?

    Addendum 2023-04-12 08:20: Oh nvm, I forgot that Java doesn't even have static constructors; doh. Always bet on basic features simply missing with Java :-)

  • (nodebb) in reply to LordOfThePigs

    I've tried that too. The best approach I found was to work through an Object...args argument. It needs a little bit of smartness in how to convert to the right type for the DB, but it's mostly simple. OTOH, I've not rewritten all of the code to be done that way.

  • (nodebb) in reply to MaxiTB

    I forgot that Java doesn't even have static constructors

    It does have something like that, but you don't have arguments available.

    static {
        // Do something here to initialize static variables.
    }
    

    Those don't get inlined. Only the ones that are known at compile time (most!) are required to be inlined, and they still can't be eliminated from the class outright (because of reflection).

  • jgh (unregistered)

    private static final int FIVE = 6;

  • (nodebb) in reply to Pabz

    Do you have an off by 1 error there?

    Nope. What's the index of the second element in an array?

  • (nodebb)

    If your linter reckons that a constant is ALWAYS better than a literal then you need a new linter.

Leave a comment on “Constantly Named”

Log In or post as a guest

Replying to comment #:

« Return to Article