Magic numbers are unexplained values that often should be replaced with a named constant.
Questions tagged [magic-numbers]
193 questions
6
votes
2 answers
Hard-coded 8191 10485 values in JavaScript rounding function
I've seen the following (bizarre) Javascript rounding function in some legacy code. After googling for it I can see that it crops up in a number of places online. However I can't work out why the hard-coded values 8191 and 10485 are present.
Does…

Matthew Hegarty
- 3,791
- 2
- 27
- 42
6
votes
2 answers
0xfbad8001 Magic Number in backtrace
I am looking at the following backtrace of a program I am debugging in GDB:
Thread 7 (Thread 3983):
#0 0xf7737430 in __kernel_vsyscall ()
#1 0x41b85412 in __lll_lock_wait () at ../nptl/sysdeps/unix/sysv/linux/i386/i486/lowlevellock.S:142
#2 …

Digital Trauma
- 15,475
- 3
- 51
- 83
6
votes
1 answer
Losing magic numbers in Java printf format specifiers to generate columns
In C, the printf function has a great wildcard feature, where you can use an asterisk where you would normally place an int that specifies minimum column width. So you can go
#DEFINE COL_WIDTH 20;
up in the preprocessor directives, and then later…

mander Don't reinstate Monica
- 245
- 1
- 8
6
votes
4 answers
Refactor CSS to eliminate "magic numbers"
I know magic numbers are bad, but I still come across times when they seem unavoidable. I've created an example that I'd love for someone to show me how to refactor and eliminate the magic number.
Hopefully, this will help me think differently about…

Kevin Perrine
- 407
- 2
- 14
5
votes
1 answer
How to avoid magic numbers when matching an event.keyCode in Javascript
Can I avoid magic numbers when matching keypresses in Javascript?
An example would be using 13 to match the enter key.
I could specify my own constants, but I don't know if these values are stable enough across different platforms/browsers/locales.

Ken
- 77,016
- 30
- 84
- 101
5
votes
1 answer
What does '0xDEAD' mean in the following code?
There is an enum structure,but I don't understand the meaning of '0xDEAD - 2' in this enum.
enum TerminatedTypes {
_not_terminated = 0xDEAD - 2,
_thread_exiting,
_thread_terminated,
…

yangyixiaof
- 225
- 4
- 15
5
votes
4 answers
SOLID principles, and hard code configuration inside a class
I have noticed in a lot of code lately that people put hard coded configuration (like port numbers, etc.) values deep inside of classes/methods, making it difficult to find, and also not configurable.
Is this a violation of the SOLID principles? If…

skb
- 30,624
- 33
- 94
- 146
5
votes
2 answers
Java's checkstyle, MagicNumberCheck
I am using checkstyle to get reportings about my source-code. This question is about the MagicNumberCheck.
I am using Date/(org.joda.)DateTime in my source code like this:
DateTime dateTime = new DateTime(2013, 2, 27, 23,…

Markus Schulte
- 4,171
- 3
- 47
- 58
4
votes
7 answers
Database Design Lookup tables
I'm currently trying to improve the design of a legacy db and I have the following situation
Currently I have a table SalesLead in which we store the the LeadSource.
Create Table SalesLead(
....
LeadSource varchar(20)
....
)
The Lead…
NaughtyNaughty
4
votes
3 answers
How to search a image/varbinary field for records that start with a binary pattern
I am trying to find all images that do not start with the magic number ff d8 ff e0 (the signature for jpg) According to the MSDN I should be able to use patindex on my data. However
SELECT TOP 1000 [cpclid]
FROM [cp]
where patindex('FFD8FFE0%',…

Scott Chamberlain
- 124,994
- 33
- 282
- 431
4
votes
1 answer
Magic number removal on ROT13 in ColdFusion
I have this function that calculates ROT13.
string function rot13(required string inString) output="false" {
var j = 0;
var k = 0;
var out = "";
for (var i = 1; i <= Len(arguments.inString); i++){
j = asc(Mid(arguments.inString, i,…

James A Mohler
- 11,060
- 15
- 46
- 72
4
votes
4 answers
C++ read and compare magic number from file
i have i file i want to read in C++. First thing i have to read and check is the magic number of the file. In my case it is the Hex-Value: 0xABCDEF00
I read and compare the number this way:
ifstream input ("C:/Desktop/myfile", ios::binary);
if…

Opa114
- 518
- 4
- 12
- 28
4
votes
5 answers
Should I use an enum or multiple const for non-sequential constants in c++?
I'm writing porting file-io set of functions from c into a c++ class. "Magic numbers" (unnamed constants) abound.
The functions read a file header which has a number of specific entries whose locations are currently denoted by magic numbers.
I…

Jason R. Mick
- 5,177
- 4
- 40
- 69
4
votes
1 answer
How to determine if a byte is null in a word
I am reading the "strlen" source code from the glibc, and the trick developers found to speed it up is to read n bytes where n is the size of a long word, instead of reading 1 byte at each iteration.
I will assume that a long word has 4 bytes.
The…

Brendan Rius
- 610
- 9
- 18
4
votes
1 answer
What is 0xbbadbeef used for in Webkit?
While working with Webkit I encountered an error with a pointer set to 0xbbadbeef.
What is BadBeef used for in Webkit?

Krzak
- 1,441
- 11
- 12