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?
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 .
boolean
initializeAtStartup
( ) , . , . .
. Java, - - . , , , .
, - , , , - ; "- ", - , ( ) , .
, preereq ANYthing ( ), , , , - ( - , , !).
", ", , , " , ", , , / . , , ; ... " " " ", , .
, ( , ;), , , " " " ! -). , , .
, , . , readonly ( # ). .
, -, " , ", , - . , /, 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.
/, . , , , - . .
( , , ).
readonly? .
, , , ?
, , ( , .) , .
, , , , , , , . , .
( , ) .
FWIW - #define . const , k '(' konstant '- not' c ', , count '' char ').
#define
const
k
c
, "k" , , , all-caps .
, . , , . ..
" " , .
Java , . , Sun Java, Java.
C ++ () , all-caps , . , , , .
, , - , , . / , , :
. Java . - , , . JIT Java getter, .
- , . . , , . , , : .
, , - , , - . C ++ ALL_CAPS:
; , : .
( ).
, , :
(, ),
, , .
- . 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 } ... }
, - , , .
: ?
, , " " - , ALL_CAPS... ( )...
, , ALL_CAPS , a) ; b) (, AS3 , )...
" " , ... , (! , , , !)... ... , , ... , , , , , , , ... actall - , , ... ...
, 100%, - , , - ... ...
, readonly, , ...
, , ++ :
#include <iostream> int main() { int i = 0; std::cin >> i; const int CONST = i; std::cout << CONST; //displays i system("PAUSE"); return 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.)
Source: https://habr.com/ru/post/1716407/More articles:How to enable one Arduino port using C #? - c #How do I bind data to two user interface elements so that they can immediately update each other in Silverlight? - data-bindingWhen you type $ (in visual studio for jquery ... it will automatically convert to $ addHandler (- jqueryHow to align text vertically using CSS? - htmlCSS: Как вертикально выравнивать текст в div? (или любые другие элементы, которые не являются таблицей) - htmlHow to run a subset of OCUnit tests in Xcode - objective-chttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1716409/looking-for-ideassolutions-to-manage-application-configuration-settings&usg=ALkJrhhU5e0olAWGwTFgo4n68wuhuiPIrwКак NSLog пиксель RGB от UIImage? - iphoneLinux Development - user-interfaceSVG → linear gradient WPF - wpfAll Articles