Questions tagged [constants]

22 questions
142
votes
3 answers

Where do "magic" hashing constants like 0x9e3779b9 and 0x9e3779b1 come from?

In code dealing with hash tables, I often find the constant 0x9e3779b9 or sometimes 0x9e3779b1. For example hash = n * 0x9e3779b1 >>> 24 Why is this particular value used?
bkgs
  • 1,003
  • 2
  • 6
  • 5
49
votes
7 answers

Is it a bad practice to have an interface to define constants?

I am writing a set of junit test classes in Java. There are several constants, for example strings that I will need in different test classes. I am thinking about an interface that defines them and every test class would implement it. The benefits…
FabianB
  • 932
  • 1
  • 7
  • 8
35
votes
1 answer

Original source of `(seed * 9301 + 49297) % 233280` random algorithm?

If you search for examples of creating a seeded (pseudo)Random number generator, you will run into stuff like this (specific example http://indiegamr.com/generate-repeatable-random-numbers-in-js/): // the initial seed Math.seed = 6; // in order to…
jlarson
  • 511
  • 1
  • 4
  • 8
28
votes
7 answers

May a value of a constant be changed over time?

During development phase, there are certain variables which need to be the fixed in the same run, but may need be modified over time. For example a boolean to signal debug mode, so we do things in the program we normally wouldn't. Is it bad style to…
GregT
  • 407
  • 1
  • 4
  • 7
17
votes
6 answers

Is setRGBColor(32,128,192) a valid use of magic number?

According to When is a number a magic number?, I know magic number should not exists, so I know the following code about creating a Label from some UI framework and set the color RGB needs to be improved: Magic number version: Label*…
wcminipgasker2023
  • 721
  • 1
  • 3
  • 10
6
votes
1 answer

Is it bad practice to define constants using class static methods?

Instead of using #define or const, I usually define constants using class static methods as follows: //AppConstants.h #include class AppConstants{ public: static int getMax(); static std::string…
ggrr
  • 5,725
  • 11
  • 35
  • 37
4
votes
4 answers

Is a constant name related to its current value an anti-pattern?

For example, suppose I have a string constant like this: const TITLEBAR_MESSAGE="Welcome back, %USERNAME%!"; I think it is more readable when it is named as const WELCOME_BACK_USERNAME="Welcome back, %USERNAME%!"; because I don't need to move to…
ocomfd
  • 5,652
  • 8
  • 29
  • 37
3
votes
3 answers

Define "constants" at the global or function scope?

I often define variables that will never change, i.e. constants, at the top of a script/module. But recently I've been wondering if it makes sense to define them at the function scope level if they are only used by a single method. LIST_OF_THINGS =…
pstatix
  • 1,017
  • 10
  • 16
3
votes
5 answers

I feel like these constants should be in a different class?

I have a static class called RenderingUtilities that houses several useful methods and constants. Some of these constants are related to the Earth as an object such as the Earth's radius. I believe the constants related to the Earth should be…
Hazel へいぜる
  • 1,165
  • 1
  • 8
  • 19
3
votes
1 answer

Should I replace a constant with static methods, if that constant usually 'cooperate' with a specific operator?

For example, to convert between g and kg, I have a constant 1000: public static final float G_TO_KG=1000; . . . this.result = someResult*1000; I found G_TO_KG always bind to operator '*'. So my question is, is it better to define a static…
ocomfd
  • 5,652
  • 8
  • 29
  • 37
3
votes
1 answer

Should I avoid creating a variable with shorter name for a constant?

suppose I have code like this someFunction:function(userId){ var url=SomeClass.SomeNetworkConnector.SOME_URL; if(url !== undefined && userId !== undefined){ this.openURL(url+"?userid="+userId); } } Initially I think the name of actual…
ocomfd
  • 5,652
  • 8
  • 29
  • 37
3
votes
1 answer

Should I rename variables that are already constants in my own library?

I'm writing a high level wrapper around the Python socket.socket object. Specifically, I want to do this for IPv4 TCP sockets (though it would be useful to be able to expand the library later with other families / types). As you may know, the socket…
Daniel
  • 149
  • 5
3
votes
4 answers

Should I use one class for one constant?

Originally, in most languages, I would like to put constants into a class like that: public class AppConstants{ public static final double HEIGHT_MAX=20.0; public static final int COUNT_MAX=100; public static final double…
ggrr
  • 5,725
  • 11
  • 35
  • 37
2
votes
2 answers

What is the best way to keep some constant complex data structures in a C++ code?

As implied by the title of the Q, I have a few entities and need to keep them as constant objects in a C++ project. Each entity contains very complex data structures (lists of enums, maps of maps, etc.). In addition, it is possible in future to have…
TonySalimi
  • 211
  • 1
  • 6
2
votes
3 answers

When defining constants, which is more important? Easier to find? Or narrower scope?

For example, consider I have constants VOL_MIN and VOL_MAX, which is used inside 1 function only: public void setVolume(int val){ final int VOL_MIN = 1; final int VOL_MAX = 10; val=Math.max(VOL_MIN,Math.min(val,VOL_MAX)); //some…
ocomfd
  • 5,652
  • 8
  • 29
  • 37
1
2