Magic numbers are unexplained values that often should be replaced with a named constant.
Questions tagged [magic-numbers]
193 questions
2
votes
4 answers
Getting rid of magic numbers in Java
How do I get rid of magic numbers in java without declaring a massive amount of finals or static finals? Keep in mind looping, arrays are not allowed. It doesn't seem possible? Help appreciated. thanks.
Example code:
drawOval(5, 5, width,…

Larry21
- 169
- 3
- 7
2
votes
2 answers
Checking "Magic Bytes" or Mime Types in PHP?
So, I've currently used a few different methods to check the mime type. The user uploads a file using a form, I grab the mime type, if it's application/zip, I allow it, if it's anything else, I deny it. The issue is that something (browsers I…

user2376223
- 21
- 1
- 3
2
votes
2 answers
Can a GDBM database created in C be opened in Python?
I have a Python program in which I am trying to use this rhyming dictionary to look up rhymes.
Part of the dictionary library setup works by a C program creating three gdbm .db files. The code that does this is publicly available here (from 'get…

Bill Cheatham
- 11,396
- 17
- 69
- 104
2
votes
6 answers
How to identify the file type even though the file-extension has been changed?
Files are categorized by file-extension. So my question is, how to identify the file type even the file extension has been changed.
For example, i have a video file with name myVideo.mp4, i have changed it to myVideo.txt. So if i double-click it,…

Maximin
- 1,655
- 1
- 14
- 32
2
votes
2 answers
Solution for Magic Number issue......?
Consider the following code segment...
public static UserStatus getEnum(int code) {
switch (code) {
case 0:
return PENDING;
case 1:
return ACTIVE;
case 2:
return SUSPENDED;
…

Ruchira Gayan Ranaweera
- 34,993
- 17
- 75
- 115
2
votes
2 answers
Xcode/Clang magic numbers?
I'm using Xcode with Clang as the compiler, and I'm currently getting an EXC_BAD_ACCESS when calling a function on a pointer whose address is 0xCDCDCDCD. I've also seen some variables set to 0xABABABAB around. I've looked around and noticed that…

benwad
- 6,414
- 10
- 59
- 93
2
votes
5 answers
"Magic Number" exercise in java
So my assignment is to find all the "magic numbers" within a range of numbers (input by the user). A magic number is a number whose factors (except itself) sums up to that number. So 6 would be a magic number because it's factors besides itself are…

biohax2015
- 281
- 2
- 6
- 15
2
votes
1 answer
BlackBerry - Problem with GZip decompression
There is a strange problem I've run in using RIM compression API, I can't make it work as it's described in documentation.
If I gzip plain text file using win gzip tool, add gz to resources of blackberry project and in app try to decompress it,…

Maksym Gontar
- 22,765
- 10
- 78
- 114
2
votes
3 answers
When are numbers NOT Magic?
I have a function like this:
float_as_thousands_str_with_precision(value, precision)
If I use it like this:
float_as_thousands_str_with_precision(volts, 1)
float_as_thousands_str_with_precision(amps, 2)
float_as_thousands_str_with_precision(watts,…

theanine
- 986
- 3
- 10
- 21
1
vote
0 answers
python compiled bytecode `.pyc` not compatible with every versions. can we make it general? How to remove python magic number error?
I have compiled this visible_code.py file into .pyc compiled byte code file, then I am running the function fun from .pyc file.
# visible_code.py
def fun():
print("Hello")
# convet to bytecode
import…

Visrut
- 360
- 4
- 14
1
vote
0 answers
Should I declare a number as constant if it is passed as a parameter to a meaningful named function?
Suppose we a validation code similar to the following:
Rule(account => account.CompanyName).MaxLength(50)
Or
Rule(account => account.Balance).MustBeGreaterThan(0)
Do we still call the numbers 50 or 0 a magic number and define a constant for it,…

Kush Grover
- 363
- 1
- 6
- 16
1
vote
1 answer
Flutter: how to get magic numbers of file?
I'm using file_picker to choose file from storage and sometimes it doesn't give an extension but I need to know an extension to send it to server side. I'm going to identify it by Magic Number like this
print(lookupMimeType('test.html',…

Zhu
- 387
- 4
- 15
1
vote
2 answers
Using -1 as special value in API functions taking numeric arguments
Consider this example function from a fictive game engine API:
function Entity.SetHealth( Number health )
Is it bad to have functions like this accept -1 as parameter, causing, in this case, the entity to become invincible?
Should I rather use two…

Frukt
- 11
- 1
1
vote
1 answer
What is the Byte Order Mark preceding the magic number of this PDF?
I am processing files and use magic numbers to identify file type validity.
I am using the Medsea mime-util JAR for Java to investigate the magic number and determine mime. This library accounts for two different PDF sequences it checks from…

JoshDM
- 4,939
- 7
- 43
- 72
1
vote
1 answer
Magic number constant name convention for tests ONE, TWO,.. ONE_HUNDRED vs _1 , _2, _100
so I'm doing some code refactoring/sonar fixes, and there are some tests that contain magic numbers, 5, 123, 567 or whatever, i wanted to create a NumberConstant class where we save numbers used in tests, everything is good we have something like…
user10359550