Questions tagged [constants]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
1 answer
146 views

Should private static constants be in declaration (header) or defintion (source)?

Given a class which has certain private, constants (e.g., configuration), should these (A) be included in the declaration of the class (in the private section) or (B) should it be "hidden" ...
Patrick Wright's user avatar
18 votes
6 answers
4k views

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 ...
wcminipgasker2023's user avatar
1 vote
2 answers
1k views

How to deal with constants that are shared between multiple packages?

I'm looking at creating my first packages to clean up my codebase. Below is a very simplified version of my current project structure: my_project/ |-database.py |-app_1.py |-app_2.py |-constants.py ...
Jossy's user avatar
  • 311
2 votes
2 answers
436 views

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....
TonySalimi's user avatar
1 vote
1 answer
73 views

Term for a map of constants [closed]

Considering there's a map of string constants, most commonly with a key and a value being equal, e.g. in TypeScript: const itemTypes = { FOO_BAR: 'FOO_BAR', BAZ_QUX: 'BAZ_QUX', } as const; They ...
Estus Flask's user avatar
3 votes
3 answers
2k views

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 ...
pstatix's user avatar
  • 1,027
144 votes
3 answers
28k views

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's user avatar
  • 1,023
0 votes
1 answer
161 views

Visibility of constants

Should constants of my class only be public if I know that they will be used from their users or doesn't it hurt to make all of my constants public per se? Is not exposing all constants cleaner?
Kai Neuwerth's user avatar
1 vote
1 answer
512 views

Should I use accessors or public static fields for global constants?

I have to work on some code that was CAST-audited. The report says that it is bad in Java to use public static and that accessors should be preferred. That is also what I was taught at school. The ...
The Once-ler's user avatar
4 votes
4 answers
844 views

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

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 ...
ocomfd's user avatar
  • 5,702
0 votes
2 answers
450 views

Referencing Database Primary Keys from Codebase

I don't have a lot of experience with these kind of issues, but I feel I need to consult on this issue. The current codebase I'm working on is using what I consider to be a questionable technique to ...
Ashton Spina's user avatar
3 votes
5 answers
1k views

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 ...
Hazel へいぜる's user avatar
3 votes
1 answer
108 views

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 ...
ocomfd's user avatar
  • 5,702
2 votes
3 answers
262 views

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....
ocomfd's user avatar
  • 5,702
1 vote
1 answer
222 views

Should I make constant for values of three choices (trivalent/ternary)?

For two choices there is boolean. In my case, I have positive, negative, and neutral which are three choices and cannot be represented by boolean. I've see there's method Math.signum(x) in Java which ...
fikr4n's user avatar
  • 154
0 votes
1 answer
5k views

Is it a good practice to store constants in configuration files [duplicate]

In my project I have some constants where I reference almost everywhere: public sealed class Constants{ public static readonly int MAX_QUAL { get; } = 1080; public static readonly bool CC { ...
Hanjun Chen's user avatar
3 votes
1 answer
135 views

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="...
ocomfd's user avatar
  • 5,702
28 votes
7 answers
11k views

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 ...
GregT's user avatar
  • 407
3 votes
1 answer
364 views

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 ...
Daniel's user avatar
  • 149
6 votes
1 answer
31k views

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 <string> class AppConstants{ public: static int getMax(); ...
ggrr's user avatar
  • 5,755
3 votes
4 answers
610 views

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=...
ggrr's user avatar
  • 5,755
35 votes
1 answer
28k views

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/): //...
jlarson's user avatar
  • 511
49 votes
7 answers
46k views

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 ...
FabianB's user avatar
  • 932