8 ms·
> Don't use numeric literals for anything but 0, 1, and -1. Excuse me?! So if I'm writing a prng, should I write the numerical constants like int modulus =
by klyrs 7y ago
> Don't use numeric literals for anything but 0, 1, and -1.
Excuse me?! So if I'm writing a prng, should I write the numerical constants like
int modulus = 1 + (1+1+1)*(0 + (1+1+1)*(-1+... )...);
- monadic2 7y agoI’m assuming the alternative would be to refer to an easy to deduct constant reference.
- klyrs 7y agoYou might have replied before my edit, but one rarely "deduces" magic constants that result in good rngs, hashes, crypto, etc.
- __s 7y agoTheir point is you should have const int MAGIC_THING = 0xdeadbeef & then say MAGIC_THING throughout the code, rather than using 0xdeadbeef in all your expressions
- klyrs 7y agoSure. But OP said not to use numeric literals, which 0xdeadbeef certainly is.
- wpietri 7y agoIn case you somehow haven't figured it out by now, he meant in the body of one's code. He's fine with named constants. Protip: If a smart person says something that seems obviously dumb to you, it's worth trying to find an interpretation that isn't dumb. Doubly so when, as here, it's from a piece that others are clearly finding smart and useful.
- monadic2 7y agoAck, it autocorrected from “dedupe”. Also, you are referring to an orthogonal problem—you would presumably want to put magic constants into a static constant to reduce the chances of disagreement across references. Hell even if there’s a single usage I would expect magic constants to be clearly demarcated and documented. Edit: didn’t see sibling constant, didn’t mean to dupe the reply itself. This certainly refers to in-line use of numeric literals vs those used in constant definitions.
- jameshart 7y agoObviously not. So since you found a counter example we should discount the original advice and feel free to use numeric constants all over our code without any explanation.
- happytoexplain 7y agoThis isn't a helpful response. I have the same question as the parent.
- ztjio 7y agohttps://en.wikipedia.org/wiki/Magic_number_%28programming%29 https://en.wikipedia.org/wiki/Magic_number_%28programming%29
- wool_gather 7y agoAccording to the thread, there was a size of something, and that size was used in two places. The size was specified as a literal number in both those places. The bug was caused by changing the literal in one place but not the other. One way that could have been avoided is to use a named constant for the size, so that when the literal number was changed, both uses of the name would see the new value.
- regularfry 7y agoThe reverse is also dangerous: when you have the same numeric value doing different jobs in different parts of the code. If they're literals, you've got no clue whether they should both be changed or not.
- necovek 7y agoThe idea is not to replace your constant 2048 with A_2048_NUMBER. But eg. MAX_BLOCK_SIZE in one place, and STACK_SIZE in another, even if they are the same value today. Of course, if you are repurposing the code to do something new, you need to be extra careful not to use the constant just because "it's the right number". And sure, it sometimes gets tricky, but using literals will not make it any less so.
- ztjio 7y agoI'm going to assume you're not deliberately trying to misunderstand him for the sake of a hot take, because, that would be pretty lame. So instead I'll explain for you and anyone else who doesn't understand him what he meant. What he is suggesting is that you generally not include literals in your code, and instead, use a constant/variable that can be traced back to a single place in order to make changes more visible and easier to deal with. That he makes exceptions for 1, 0, and -1 is explained by conventions in various languages where those values in context end up as generic and meaningful as any other language keyword and thus would not benefit from being referenced from a constant. That doesn't mean you wouldn't ever end up with constants that are 0, 1 or -1 though, just that you wouldn't assume every random place you might use those values (such as sorting algorithms) justifies a constant placeholder. For further review on the topic, may as well start here: https://en.wikipedia.org/wiki/Magic_number_%28programming%29 https://en.wikipedia.org/wiki/Magic_number_%28programming%29
- klyrs 7y ago> I'm going to assume you're not deliberately trying to misunderstand him for the sake of a hot take, because, that would be pretty lame. I'll admit, my example was overwrought with comedic intent, but I stand firm on this issue. Use a magic number twice? Yeah, go ahead and put that numeric literal somewhere convenient (but you still have a numeric literal) to the consumers of it and easy to find by your readers (not a top-level magic_numbers.h) I'm a mathematician. The fear of numbers in code is an affront to the domain that I work in. YMMV. I put a lot of work into documenting my code, but for the love of pete, 2 is not a magic number: my example was no more overwrought than OP's rule.
- mattkrause 7y agoIt depends on why you're using the 2. In some cases, I'd say that 1 can be a magic number. For example, it would better to write fprintf(STDOUT, output_string) than fprintf(1, output_string) However, 0 and ±1 have a special role in picking out the first/next/previous elements of a sequence (and related things), which is so common that it'd be silly to insist on defining POSSIBLE_NEXT_INDEX and POSSIBLE_PREDECESSOR. That said, people do seem to love Python's itertools, so....maybe that's where we're headed. I had somebody aggressively complain about the readability of the all-pairs for-loop, which I thought was basically standard. for(auto i=0; i<N-1; i++) for(auto j=i+1; j<N; j++) process(X[i], X[j]) The flip side is that I don't think you always need to do this. For example, this is silly: double triangle::area() const { const double NORMALIZATION = 1/2.0; return this->base * this->height * NORMALIZATION; } The point is just to make it clear why that particular number is being used and where it came from.
- mattkrause 7y agoMaybe something more like: /* From Park and Miller (1988, pg 1195), using their notation */ const int A = 16807; const int M = 2147483647; const int q = 127773; const int r = 2836; ... instead of "inlining" them into the code like test = 16807 * (seed % 127773) - 2836 * (seed/127773) The "ban" on literals obviously exempts their definition. No one, outside a number theory textbook, is defining A as successor(successor(...(successor(1)))
- morsch 7y ago> Park and Miller (1988, pg 1195) That's pretty cool. Did you at least have to look it up?
- mattkrause 7y agoI'm flattered you think there's even a tiny possibility I'd remember the constants or page :-) In reality, I vaguely remembered the authors and part of the title, then guess-and-googled the rest. It's a nice paper: https://dl.acm.org/doi/10.1145/63039.63042 https://dl.acm.org/doi/10.1145/63039.63042 though obviously no longer the state of the art. I recommend the citation-in-the comments idea though. We do it for lab stuff and it's very helpful for everything from debugging to writing up results.