Questions tagged [magic-numbers]

Magic numbers are unexplained values that often should be replaced with a named constant.

193 questions
13
votes
6 answers

How to get magic number of a binary file

There is a magic number associated with each binary file , does anyone know how to retrieve this information from the file?
cppb
  • 2,319
  • 8
  • 32
  • 37
12
votes
10 answers

Is -1 a magic number? An anti-pattern? A code smell? Quotes and guidelines from authorities

Possible Duplicate: Constant abuse? I've seen -1 used in various APIs, most commonly when searching into a "collection" with zero-based indices, usually to indicate the "not found" index. This "works" because -1 is never a legal index to begin…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
11
votes
1 answer

What's the significance of -532459699?

This is a number that's returned as an exit code in many .NET exceptions (particularly COM exceptions, I think). In this question someone used Reflector to find out that this value was initialized to a private variable in nearly every Exception…
Kris Harper
  • 5,672
  • 8
  • 51
  • 96
11
votes
2 answers

How should i define something constantlike in Erlang

I have a module which does some non constrained minimization. I'd like to keep its' interface as simple as possible, so the best choice would be to reduce it to a single function something like: min_of( F ). But as soon as it is brutal computation,…
akalenuk
  • 3,815
  • 4
  • 34
  • 56
10
votes
2 answers

EWOULDBLOCK equivalent errno under Windows Perl

G'day Stackoverflowers, I'm the author of Perl's autodie pragma, which changes Perl's built-ins to throw exceptions on failure. It's similar to Fatal, but with lexical scope, an extensible exception model, more intelligent return checking, and…
pjf
  • 5,993
  • 25
  • 42
9
votes
6 answers

Why are people using magic values instead of null in their code?

I have seen this in legacy code and in some .NET open source projects. I can't imagine a reason to do this. Just using "null" seems so much easier to me. Example: public class Category { int parentID; bool HasParent { get { …
Paco
  • 8,335
  • 3
  • 30
  • 41
9
votes
3 answers

Original file bytes from StreamReader, magic number detection

I'm trying to differentiate between "text files" and "binary" files, as I would effectively like to ignore files with "unreadable" contents. I have a file that I believe is a GZIP archive. I'm tring to ignore this kind of file by detecting the…
Tom Hunter
  • 5,714
  • 10
  • 51
  • 76
8
votes
4 answers

Find Magic Numbers C++

Magic Numbers A positive integer is “magic” if, and only if, it can be reduced to 1 by repeatedly dividing it by 2 if it’s even or multiplying it by 3 and then adding 1 if it’s odd. So, for example, 3 is magic because 3 reduces first to 10 (3*3+1),…
Chaz
  • 83
  • 1
  • 6
8
votes
2 answers

Avoiding using magic numbers in JavaScript - alternatives that work with JsHint

JSHint's inspections now built into PhpStorm informed me about JavaScript magic numbers and I realise it'll make for clearer code to avoid using them. I tried this: var constants = { millisecs: 1000, secs: 60 }; and also this: var constants…
bcmcfc
  • 25,966
  • 29
  • 109
  • 181
7
votes
2 answers

How to get the magic number from File in java

I have file from UploadedFile button, and I want to print the extension files by use in magic number, My code: UploadedFile file = (UploadedFile)valueChangeEvent.getNewValue(); byte[] fileByteArray = IOUtils.toByteArray(file.getInputStream()); pay…
user1012506
  • 2,048
  • 1
  • 26
  • 45
7
votes
2 answers

Gradle dependency causing error "Invalid Magic Number"

I have a project on GitHub that I work on both in the office at home. For about 2 months it was working fine on both machines. Then two weeks ago, it stopped running on my home PC, but still works fine on my work PC. This is the error I…
JuiCe
  • 4,132
  • 16
  • 68
  • 119
7
votes
11 answers

Magic Numbers In Arrays? - C++

I'm a fairly new programmer, and I apologize if this information is easily available out there, I just haven't been able to find it yet. Here's my question: Is is considered magic numbers when you use a literal number to access a specific element…
Alex
  • 64,178
  • 48
  • 151
  • 180
6
votes
2 answers

Design to prevent magic numbers and strings

I am building a service that retrieves from and submits to a third party. The third party delivers a complex data model which includes three different status types, all of them integers with an obscure number series. An example is provided below…
Marius Agur
  • 272
  • 3
  • 11
6
votes
1 answer

AddOutParameter - non-magic number way of finding length of DBType.Int32

I have a magic number in the following code... Microsoft.Practices.EnterpriseLibrary.Data.Database db = /* code omitted */; db.AddOutParameter(command, "@ParamName", DbType.Int32, 8); Is there a clean way to get the length of DbType.Int32, as…
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
6
votes
2 answers

SonarQube: Should magic numbers be allowed in java constructor enums

Concerning rule squid: S109 Magic numbers should not be used Shouldn't it be allowed to have numbers in the constructor of an enum in java? The code below shouldn't violate the rule in my opinion. public enum Color{ RED(42), GREEN(123456), …
DEG
  • 65
  • 1
  • 5
1
2
3
12 13