Search Results

Search found 724 results on 29 pages for 'constants'.

Page 1/29 | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • The purpose of using a constants pool for immutable constants

    - by patstuart
    Originally posted at stackoverflow.com/q/23961260 I come across the following code with a lot of frequency: if (myArray.length == Constants.ZERO_INT) or if (myString != null && !myString.equals(Constants.EMPTY_STRING)) Neither of these makes much sense to me. Isn't the point of having a constant pool for ease of code appearance and to allow for modularity? In both of the above cases, it just looks like needless noise that accomplishes neither objective. My question: what is the purpose of using a constants pool for variables like this which will never change? Or is this just cargo cult programming? If so, then why does it seem to be prevalent in the industry? (I've noticed it with at least two different employers I've worked with).

    Read the article

  • Using nested classes for constants?

    - by antirysm
    What's wrong with using nested classes to group constants? Like so: public static class Constants { public static class CategoryA { public const string ValueX = "CatA_X"; public const string ValueY = "CatA_Y"; } public static class CategoryB { public const string ValueX = "CatB_X"; public const string ValueY = "CatB_Y"; } } Used like so: Console.WriteLine(Constants.CategoryA.ValueY); Console.WriteLine(Constants.CategoryB.ValueX); You could also make the "Constants"-class partial...

    Read the article

  • Extreme Optimization –Mathematical Constants and Basic Functions

    - by JoshReuben
    Machine constants The MachineConstants class - contains constants for floating-point arithmetic because the CLS System.Single and Double floating-point types do not follow the standard conventions and are useless. machine constants for the Double type: machine precision: Epsilon , SqrtEpsilon CubeRootEpsilon largest possible value: MaxDouble , SqrtMaxDouble, LogMaxDouble smallest Double-precision floating point number that is greater than zero: MinDouble , SqrtMinDouble , LogMinDouble A similar set of constants is available for the Single Datatype  Mathematical Constants The Constants class contains static fields for many mathematical constants and common expressions involving small integers – if you are doing thousands of iterations, you wouldn't want to calculate OneOverSqrtTwoPi , Sqrt17 or Log17 !!! Fundamental constants E - The base for the natural logarithm, e (2.718...). EulersConstant - (0.577...). GoldenRatio - (1.618...). Pi - the ratio between the circumference and the diameter of a circle (3.1415...). Expressions involving fundamental constants: TwoPi, PiOverTwo, PiOverFour, LogTwoPi, PiSquared, SqrPi, SqrtTwoPi, OneOverSqrtPi, OneOverSqrtTwoPi Square roots of small integers: Sqrt2, Sqrt3, Sqrt5, Sqrt7, Sqrt17 Logarithms of small integers: Log2, Log3, Log10, Log17, InvLog10  Elementary Functions The IterativeAlgorithm<T> class in the Extreme.Mathematics namespace defines many elementary functions that are missing from System.Math. Hyperbolic Trig Functions: Cosh, Coth, Csch, Sinh, Sech, Tanh Inverse Hyperbolic Trig Functions: Acosh, Acoth, Acsch, Asinh, Asech, Atanh Exponential, Logarithmic and Miscellaneous Functions: ExpMinus1 - The exponential function minus one, ex-1. Hypot - The hypotenuse of a right-angled triangle with specified sides. LambertW - Lambert's W function, the (real) solution W of x=WeW. Log1PlusX - The natural logarithm of 1+x. Pow - A number raised to an integer power.

    Read the article

  • Java best practice Interface - subclasses and constants

    - by Taiko
    In the case where a couple of classes implements an interface, and those classes have a couple of constants in common (but no functions), were should I put this constant ? I've had this problem a couple of times. I have this interface : DataFromSensors that I use to hide the implementations of several sub classes like DataFromHeartRateMonitor DataFromGps etc... For some reason, those classes uses the same constants. And there's nowere else in the code were it is used. My question is, were should I put those constants ? Not in the interface, because it has nothing to do with my API Not in a static Constants class, because I'm trying to avoid those Not in a common abstract class, that would stand between the interface and the subclasses, because I have no functions in common, only a couple of constants (TIMEOUT_DURATION, UUID, those kind of things) I've read best practice for constants and interface to define constants but they don't really answer my question. Thanks !

    Read the article

  • Namespace constants and use as

    - by GordonM
    I'm having some problems with using constants from a namespace. If I define the constant and try to use as it, PHP seems unable to find it. For example, in my file with the constants I have code along the lines of the following: namespace \my\namespace\for\constants; const DS = DIRECTORY_SEPARATOR; Then in the consuming file I have: namespace \some\other\namespace; use \my\namespace\for\constants\DS as DS; echo (realpath (DS . 'usr' . DS 'local')); However, instead of echoing '/usr/local' as expected I get the following notice and an empty string. Notice: Use of undefined constant DS - assumed 'DS' If I change the code as follows: use \my\namespace\for\constants as cns; echo (realpath (cns\DS . 'usr' . cns\DS 'local')); I get the expected result, but it's obviously quite a bit less convenient than just being able to pull the constants in directly. You can alias a class/interface/trait in a namespace, are you not able to alias a constant too? If you can do it, then how?

    Read the article

  • Sharing constants across a WCF service

    - by Sandor Davidhazi
    I have certain strings which contain special characters so they can not be shared as enum members across a WCF service. (Actually, they are keys for configuration values.) I want to be able to pass in the keys at client side and get back the config values. If there is a change, I only want to change the config keys at one place. Constants would be ideal, because they can be changed as strong references across the entire solution, and the underlaying value could be updated with a service reference update. Currently I can think of two possible solutions: Create a shared assembly and place the constants there Share the constants across the service. The problem is, I can't get the datacontractserializer to serialize the constants. Is that possible at all? Is the shared assembly the only option I have?

    Read the article

  • How to manage Constants in Application

    - by changed
    what is the best way to use Application Constants ? What i usually do is create a separate table in database of constants and reference them as foreign key in other table. In Java i use enum. But how to keep a single place of authority for constants in application and what are the different ways i can do that(like table or enum).

    Read the article

  • Delphi: All constants are constant, but some are more constant than others?

    - by Ian Boyd
    Consider: clHotlight: TColor = $00FF9933; clLink = clHotLight; //alias of clHotlight [Error] file.pas: Constant expression expected and the alternate wording that works: clHotlight = TColor($00FF9933); clLink = clHotLight; //alias of clHotlight Explain. Then consider: AdministratorGUID: TGUID = '{DE44EEA0-6712-11D4-ADD4-0006295717DA}'; SuperuserGUID = AdministratorGUID; //alias of AdministratorGUID [Error] file.pas: Constant expression expected And fix.

    Read the article

  • Where should you put constants and why?

    - by Tim Meyer
    In our mostly large applications, we usually have a only few locations for constants: One class for GUI and internal contstants (Tab Page titles, Group Box titles, calculation factors, enumerations) One class for database tables and columns (this part is generated code) plus readable names for them (manually assigned) One class for application messages (logging, message boxes etc) The constants are usually separated into different structs in those classes. In our C++ applications, the constants are only defined in the .h file and the values are assigned in the .cpp file. One of the advantages is that all strings etc are in one central place and everybody knows where to find them when something must be changed. This is especially something project managers seem to like as people come and go and this way everybody can change such trivial things without having to dig into the application's structure. Also, you can easily change the title of similar Group Boxes / Tab Pages etc at once. Another aspect is that you can just print that class and give it to a non-programmer who can check if the captions are intuitive, and if messages to the user are too detailed or too confusing etc. However, I see certain disadvantages: Every single class is tightly coupled to the constants classes Adding/Removing/Renaming/Moving a constant requires recompilation of at least 90% of the application (Note: Changing the value doesn't, at least for C++). In one of our C++ projects with 1500 classes, this means around 7 minutes of compilation time (using precompiled headers; without them it's around 50 minutes) plus around 10 minutes of linking against certain static libraries. Building a speed optimized release through the Visual Studio Compiler takes up to 3 hours. I don't know if the huge amount of class relations is the source but it might as well be. You get driven into temporarily hard-coding strings straight into code because you want to test something very quickly and don't want to wait 15 minutes just for that test (and probably every subsequent one). Everybody knows what happens to the "I will fix that later"-thoughts. Reusing a class in another project isn't always that easy (mainly due to other tight couplings, but the constants handling doesn't make it easier.) Where would you store constants like that? Also what arguments would you bring in order to convince your project manager that there are better concepts which also comply with the advantages listed above? Feel free to give a C++-specific or independent answer. PS: I know this question is kind of subjective but I honestly don't know of any better place than this site for this kind of question. Update on this project I have news on the compile time thing: Following Caleb's and gbjbaanb's posts, I split my constants file into several other files when I had time. I also eventually split my project into several libraries which was now possible much easier. Compiling this in release mode showed that the auto-generated file which contains the database definitions (table, column names and more - more than 8000 symbols) and builds up certain hashes caused the huge compile times in release mode. Deactivating MSVC's optimizer for the library which contains the DB constants now allowed us to reduce the total compile time of your Project (several applications) in release mode from up to 8 hours to less than one hour! We have yet to find out why MSVC has such a hard time optimizing these files, but for now this change relieves a lot of pressure as we no longer have to rely on nightly builds only. That fact - and other benefits, such as less tight coupling, better reuseability etc - also showed that spending time splitting up the "constants" wasn't such a bad idea after all ;-)

    Read the article

  • Constants by another name

    - by Dave DeLong
    First off, I've seen this question and understand why the following code doesn't work. That is not my question. I have a constant, which is declared like; //Constants.h extern NSString * const MyConstant; //Constants.m NSString * const MyConstant = @"MyConstant"; However, in certain contexts, it's more useful to have this constant have a much more descriptive name, like MyReallySpecificConstant. I was hoping to do: //SpecificConstants.h extern NSString * const MyReallySpecificConstant; //SpecificConstants.m #import "Constants.h" NSString * const MyReallySpecificConstant = MyConstant; Obviously I cannot do this (which is explained in the linked question above). My question is: How else (besides something like #define MyReallySpecificConstant MyConstant) can I provide a single constant under multiple names?

    Read the article

  • Variables versus constants versus associative arrays in PHP

    - by susmits
    I'm working on a small project, and need to implement internationalization support somehow. I am thinking along the lines of using constants to define a lot of symbols for text in one file, which could be included subsequently. However, I'm not sure if using variables is faster, or if I can get away with using associative arrays without too much of a performance hit. What's better for defining constant values in PHP, performance-wise -- constants defined using define("FOO", "..."), or simple variables like $foo = "...", or associative arrays like $symbols["FOO"]?

    Read the article

  • Using Constants in Perl

    - by David W.
    I am trying to define constants in Perl using the use Constant pragma: use Constant { FOO => "bar", BAR => "foo" }; I'm running into a bit of trouble, and hoping there's a standard way of handling it. First of all... I am defining a hook script for Subversion. To make things simple, I want to have a single file where the class (package) I'm using is in the same file as my actual script. Most of this package will have constants involved in it: print "This is my program"; package "MyClass"; use constant { FOO => "bar" }; sub new { yaddah, yaddah, yaddah. I would like my constant FOO to be accessible to my main program. I would like to do this without having to refer to it as MyClass::FOO. Normally, when the package is a separate file, I could do this in my main program: use MyClass qw(FOO); but, since my class and program are a single file, I can't do that. What would be the best way for my main program to be able to access my constants defined in my class? The second issue... I would like to use the constant values as hash keys: $myHash{FOO} = "bar"; The problem is that %myHash has the literal string FOO as the key and not the value of the constant. This causes problems when I do things like this: if (defined($myHash{FOO})) { print "Key " . FOO . " does exist!\n"; } I could force the context: if (defined("" . FOO . "")) { I could add parentheses: if (defined(FOO())) { Or, I could use a temporary variable: my $foo = FOO; if (defined($foo)) { None of these are really nice ways of handling this issue. So, what is the best way? Is there one way I'm missing? By the way, I don't want to use Readonly::Scalar because it is 1). slow, and 2). not part of the standard Perl package. I want to define my hook not to require additional Perl packages and to be as simple as possible to work.

    Read the article

  • Single-letter prefix for PHP class constants?

    - by keithjgrant
    I've noticed many (all?) PHP constants have a single-letter prefix, like E_NOTICE, T_STRING, etc. When defining a set of class constants that work in conjunction with one another, do you prefer to follow similar practice, or do you prefer to be more verbose? class Foo { // let's say 'I' means "input" or some other relevant word const I_STRING = 'string'; const I_INTEGER = 'integer'; const I_FLOAT = 'float'; } or class Bar { const INPUT_STRING = 'string'; const INPUT_INTEGER = 'integer'; const INPUT_FLOAT = 'float'; }

    Read the article

  • Javascript: Machine Constants Applicable?

    - by DavidB2013
    I write numerical routines for students of science and engineering (although they are freely available for use by anybody else as well) and am wondering how to properly use machine constants in a JavaScript program, or if they are even applicable. For example, say I am writing a program in C++ that numerically computes the roots of the following equation: exp(-0.7x) + sin(3x) - 1.2x + 0.3546 = 0 A root-finding routine should be able to compute roots to within the machine epsilon. In C++, this value is specified by the language: DBL_EPSILON. C++ also specifies the smallest and largest values that can be held by a float or double variable. However, how does this convert to JavaScript? Since a Javascript program runs in a web browser, and I don't know what kind of computer will run the program, and JavaScript does not have corresponding predefined values for these quantities, how can I implement my own version of these constants so that my programs compute results to as much accuracy as allowed on the computer running the web browser? My first draft is to simply copy over the literal constants from C++: FLT_MIN: 1.17549435082229e-038 FLT_MAX: 3.40282346638529e+038 DBL_EPSILON: 2.2204460492503131e-16 I am also willing to write small code blocks that could compute these values for each machine on which the program is run. That way, a supercomputer might compute results to a higher accuracy than an old, low-level, PC. BUT, I don't know if such a routine would actually reach the computer, in which case, I would be wasting my time. Anybody here know how to compute and use (in Javascript) values that correspond to machine constants in a compiled language? Is it worth my time to write small programs in Javascript that compute DBL_EPSILON, FLT_MIN, FLT_MIN, etc. for use in numerical routines? Or am I better off simply assigning literal constants that come straight from C++ on a standard Windows PC?

    Read the article

  • Where should I define constants in scripts?

    - by bshacklett
    When writing scripts using a modern scripting language, e.g. Powershell or JavaScript, where should I define constants? Should I make all constants global for readability and ease of use, or does it make sense to define constants as close to their scopes as possible (in a function, for instance, if it's not needed elsewhere)? I'm thinking mostly of error messages, error IDs, paths to resources or configuration options.

    Read the article

  • What is the use of interface constants?

    - by kunjaan
    I am learning Java and just found that the Interface can have fields, which are public static and final. I haven't seen any examples of these so far. What are some of the use cases of these Interface Constants and can I see some in the Java Standard Library?

    Read the article

  • Js constants with variables inside.

    - by johnnyArt
    I know I'm able to this in PHP, but I can't remember the name or the way to do it, so I'll just explain what it is, and when someone tells me how it's called I'll update this question. I have some error messages defined as constants on javascript, however, some of those messages need to contain dynamic part as in the following example. "The username must be between 4 and 20 characters" In php, If I'm not mistaken there was some option for storing that string in a way that when called it would replace the variables with the data provided on the call. I want to do that on javascript, something like: config['string',vars] And have javascript insert those vars inside the string so it's customized. Wow, this must be the worst question I've made! I'm sorry for the lack of information, I'm kinda braindead on Sundays.

    Read the article

  • The Benefits of Constants

    - by onaclov2000
    I understand one of the big deals about constants is that you don't have to go through and update code where that constant is used all over the place. Thats great, but let's say you don't explicitly declare it as a constant. What benefit(s) exist(s) to take a variable that HAPPENS to actually not be changed and make it a constant, will this save on processing, and/or size of code...etc? Basically I have a program that the compiler is saying that a particular variable is not changed and thus can be declared a constant, I just wanted to know what the benefit to adding the constant qualifier to it would be, if it makes no difference then making that change adds no value and thus no point wasting time (this same scenario occurs in more then one place) going back and "fixing" all these variables. Thank you, onaclov

    Read the article

  • How best to implement publicly accessible constants in C#

    - by DanM
    There seem to be three choices for implementing publicly accessible constants in C#. I'm curious if there are any good reason to choose one over the other or if it's just a matter of personal preference. Choice 1 - private field plus property getter private const string _someConstant = "string that will never change"; public string SomeConstant { get { return _someConstant; } } Choice 2 - property getter only public string SomeConstant { get { return "string that will never change"; } } Choice 3 - public field only public const string SomeConstant = "string that will never change"; Which do you recommend and why?

    Read the article

  • Delphi Typed Constants in Case Statements

    - by Andreas Rejbrand
    What is the most elegant (or least ugly) way of using typed constants in a case statement in Delphi? That is, assume for this question that you need to declare a typed constant as in const MY_CONST: cardinal = $12345678; ... Then the Delphi compiler will not accept case MyExpression of MY_CONST: { Do Something }; ... end; but you need to write case MyExpression of $12345678: { Do Something }; ... end; which is error-prone, hard to update, and not elegant. Is there any trick you can employ to make the compiler insert the value of the constant (preferably by checking the value of the constant under const in the source code, but maybe by looking-up the value at runtime)? We assume here that you will not alter the value of the "constant" at runtime.

    Read the article

  • C++ initializing constants and inheritance

    - by pingvinus
    I want to initialize constant in child-class, instead of base class. And use it to get rid of dynamic memory allocation (I know array sizes already, and there will be a few child-classes with different constants). So I try: class A { public: const int x; A() : x(0) {} A(int x) : x(x) {} void f() { double y[this->x]; } }; class B : A { B() : A(2) {} }; Pretty simple, but compiler says: error C2057: expected constant expression How can I say to compiler, that it is really a constant?

    Read the article

  • modifying constants in objective c during run time

    - by moshikafya
    I am trying to solve the following problem. I have the following constants in my header file: #define PREFS_MY_CONSTANT_1 @"bla1" #define PREFS_MY_CONSTANT_2 @"bla2" #define PREFS_MY_CONSTANT_3 @"bla3" ... In one of my functions, I'd like to print to the UIbutton one of those strings based on user input, so say the user inputs '1', i would show bla1. Instead of creating a huge switch (i have 100 of those), I am looking for a way to combine the constant with the variable holding the user input, so something like that ideally: NSInteger input; [button setTitle:PREFS_MY_CONSTANT_{$input} forState: UIControlStateNormal]; Is something like that possible? what would be the best way to solve this?

    Read the article

1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >