Naming convention for a variable that works as a constant

I have a variable that I use as a constant (it will never change). I cannot declare it as a constant because the value is added at runtime.

Would you use a variable name to help understand what this means?

Or do you not want to, because it is contrary to the convention and makes things more confusing?

The bigger question is:
do you follow conventions, even if the scenario is not typical of the convention, but close enough so that it can personally help you understand things?

+3
source share
16 answers

Encapsulate it.

#include <iostream>

class ParamFoo
{
    public:
        static void initializeAtStartup(double x);
        static double getFoo();
    private:
        static double foo_;
};

double ParamFoo::foo_;

void ParamFoo::initializeAtStartup(double x)
{
    foo_ = x;
}

double ParamFoo::getFoo()
{
    return foo_;
}

int main(void)
{
    ParamFoo::initializeAtStartup(0.4);
    std::cout << ParamFoo::getFoo() << std::endl;
}

, , . , private guard boolean, , initializeAtStartup .

+1

( ) , . , . .

. Java, - - . , , , .

+10

, - , , , - ; "- ", - , ( ) , .

, preereq ANYthing ( ), , , , - ( - , , !).

", ", , , " , ", , , / . , , ; ... " " " ", , .

, ( , ;), , , " " " ! -). , , .

+9

, , . , readonly ( # ). .

+1

, -, " , ", , - . , /, ALL CAPS "".

public class BadClass
{ 
    public static final double PI = 3.1;     
      // PI is very constant.  Not according to the business roles modeled by my 
      // application, but by nature.  I don't have a problem making this publicly
      // accessible--except that [Math] already does, with much better precision)

    public static /*final*/ int FOO = null;
      // FOO is constant only by convention.  I cannot even enforce its "constness".
      // Making it public means that my enemies (overtime, for example) can change 
      // the value (late night programming), without telling me.
}

public class BetterClass
{
    public static final double PI = 3.1;
    private /*final*/ Integer foo = null; 

    public int getFoo() {
        return this.foo.intValue();
    }
    public void setFoo(int value) {
        // The business rules say that foo can be set only once.
        // If the business rules change, we can remove this condition 
        // without breaking old code.
        if ( null == this.foo ) {
           this.foo = value;
        } else {
           throw new IllegalStateException("Foo can be set only once.");
        }
    }
}

, [BetterClass], , foo "constness" . , - foo ( 2:00 !), - . - .

- foo - - - const.

/, . , , , - . .

( , , ).

+1

readonly? .

0

, , , ?

, , ( , .) , .

, , , , , , , . , .

0

( , ) .

0

FWIW - #define . const , k '(' konstant '- not' c ', , count '' char ').

, "k" , , , all-caps .

0

, . , , . ..

" " , .

Java , . , Sun Java, Java.

C ++ () , all-caps , . , , , .

, , - , , . / , , :

  • -, / , , , .
  • , , .

. Java . - , , . JIT Java getter, .

0

- , . . , , . , , : .

, , - , , - . C ++ ALL_CAPS:

  • ; , : .

  • ( ).

, , :

  • (, ),

  • , , .

0

- . initField (..) getField (..). initField // , . ( , , .)

java, . - :

public final int MY_INT = Integer.getInteger("a.property.name");

(. java.util.Properties) , -D . :

public class Foo {

public static final int MY_INT;

static {
Properties p = new Properties();
try{
p.load( new FileInputStream("app.props"):
} catch(IOException e) {
//SWALLOW or RETHROW AS ERROR
}
MY_INT=Integer.parseInt( p.getProperty("my.int","17") ); //17 is default if you swallo IOException
}
...
}
0

, , .

, - , , .

0

: ?

, , " " - , ALL_CAPS... ( )...

, , ALL_CAPS , a) ; b) (, AS3 , )...

" " , ... , (! , , , !)... ... , , ... , , , , , , , ... actall - , , ... ...

, 100%, - , , - ... ...

, readonly, , ...

0

, , ++ :

#include <iostream>

int main()
{
    int i = 0;
    std::cin >> i;

    const int CONST = i; 
    std::cout << CONST; //displays i

    system("PAUSE"); 
    return 0;
}

, , ( ).

0

- , - . - .

Follow the style used in your chosen language - in 80% of cases, which will be clear enough. An alternative is a high-boost system that sacrifices performance for perfect technical correctness (which few people will even actually absorb if you ever achieve it.)

0
source

Source: https://habr.com/ru/post/1716407/


All Articles