- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
evil constants?
private final int I = 1; private final int V = 5; private final int X = 10; private final int II = I + I; private final int III = I + I + I; private final int IV = V - I; private final int VI = V + I; private final int VII = V + I + I; private final int VIII = V + I + I + I; private final int IX = X - I; . . .
private final int l = 1; private final int l0 = l * 10; // ten private final int l00 = l0 * l0; // one-hundred private final int l000 = l0 * l0 * l0; // one-thousand private final int l000000 = l000 * l000; // one-million private final int l000000000 = l000000 * l000; // one-billion . . .
Admin
See, here's the beauty of number constants:
To fix string30, you just have to set
Admin
static const int ONE_INT32 = 1; static const unsigned int ONE_UINT32 = 1u; static const short ONE_INT16 = 1; etc...
I also agree that doing this is a bit extreme, I usually see it alongside other such constants like PI and FEET2METERS.
Admin
NUMBER_TO_DIVIDE_GDP_IN_POUNDS_BY_TO_GET_GDP_IN_BILLION_POUNDS 1000000000 uk_gdp_in_billions = uk_gdp / NUMBER_TO_DIVIDE_GDP_IN_POUNDS_BY_TO_GET_GDP_IN_BILLION_POUNDS
Admin
Having seen code like this, I'm really surprised that the values of the numeric constants weren't loaded from an XML configuration file.
Admin
Indeed. They THINK this qualifies, but it's the exact same problem pushed back a bit. string30(str) is really no different than string_prefix(str,30) where 30 is a magic number.
Somewhere there should be a DESCRIPTION_PREFIX_LENGTH = 30; or whatever that imparts specific meaning to an otherwise arbitrary 30.
Admin
(*) Yes I Have Been Trolled Or I Didn't Get The Joke So Give Me A Whoosh.
Admin
Ethicectomy required. Lobotomy helpful.
Admin
#define String00(x,y,z) return (in.length() > x) ? in.substring(ZERO, y) : in; #define String30(x) String00(x, THIRTY, TWENTYNINE) #define String31(x) String00(x, THIRTYONE, THIRTY) ... #define String50(x) String00(x, FIFTY, FORTYNINE)
(I figured I'd introduce them to the ?: operator to encourage future WTFs.)
Take that, Java! #define, fuck yeah! Java droolz, C++ roolz!
Admin
If you have to ask, you can't be one. HPC know everything so they never need to ask a question!
Admin
My father was a Highly Paid Consultant™ once, and I can assure you it was no laughing matter.
Admin
I'm patiently waiting until the moment he finds out that the preprocessor he is using truncates all constant names to the first eight characters.
According to TV Guide, hijinks will ensue.
Admin
I tend to assume we're doing well if Highly Paid Consultants can successfully feed themselves.
Admin
Admin
It can be handy to prevent certain typos and to enhance documentation. If you want to use 2 it is harder to put in THREE instead of TWO than it is to type 3 rather than 2. At least it is harder to do accidentally. Also a s/TWO/FOUR/ will likely have fewer mis-hits than s/2/4/ at least in code that I have written. And,yes it does cover you from the case where TWO is now FOUR as in "the length of an integer has changed from 2 bytes to 4.", but IMHO if you are using it like this you will probably introduce more bugs that are more obscure and hard to find than special constants.
Admin
While I understand where you are coming from, the OP is justified in his calling the constants ONE and ZERO. It is standard in mathematics to refer to the multiplicative identity and additive identities in a ring as 1 and 0 respectively, even if the ring doesn't contain numbers. In some cases the ring might even contain the traditional 1 and 0 and these will NOT correspond to the ring's 1 and 0.
Admin
No, the most naive compiler, by definition, would not. That's what the word "naive" means.
And it's not trivial to do so, either. You're essentially creating an interpreter for your language and running it at compile time.
Admin
Humans solved this problem many centuries ago; and there are no reasons why languages couldn't use the exact same solution: magnitude suffixes. 100_M and 1_G (and their relatives 100_Mi and 1_Gi) are all quite clear and distinct.
Admin
If you say "MAX_AIRSPEED_BLAH_BLAH_BLAH = 1*BILLION" there is no question.
I personally do this long before that... starting in the 10 or 100 thousands if it's an even multiple of 1000.
I feel like something like that is being talked about for C++1y. I've gotten a bit out of touch with that scene though.Admin
Sounds to me like they were used to Fortran. Fortran passes all arguments by reference, whereas C passes all arguments by value. You need to use a level of indirection to get back to the original data in C, but it "just works" in Fortran (this dates back to the Fortran II days in the 60's).
Admin
Turn-around time on this site was recently proven to be about 3 years.
Admin
The ring itself would never have the concept of 1 and 0 other than the multiplicative/additive identities. Representations, maybe - but then you're no longer talking about the ring itself, just a manifestation of it.
There's no justification in using ONE and ZERO as constants. In your case, MULTIPLICATIVE_IDENTITY and ADDITIVE_IDENTITY maybe. Mathematicians use 1 and 0 because they hold intuitive sense to themselves, the reader, and it's quick to write and everyone is happy. Spelling it out as ONE or ZERO is, invariably, short-sighted and pretty stupid if you want it to actually make sense later on.
As for the specific point of the conversation, variable/method names should hold semantic meaning, not necessarily refer to the variable itself. For an arbitrary vector, you don't have a method getTheSquareRootOfXSquaredPlusYSquared() - you use getMagnitude(), or for those still stuck in the backwards world of god-awful naming conventions, mag().
For logic to be expressed in its most effective format - especially for future maintenance - it has to present itself logically. Shortcuts are great in derivations - Dirac and Feynman are great examples of people who think outside the box to make stuff easier to think about - but in implementation it should be a punishable crime not to be clear-cut.
Or, in other words, if you have to explain to someone what an accessible variable/method MEANS given the name - and you've moved on since the naming restrictions of Fortran < 1991 - one of you need some education.
Captcha: dolor - Lorem ipsum dolor sit amet.
Admin
Go back to Haskell
that's a compliment
Admin
It's not so much having a number versus having a constant as it is that the name of the constant is nearly meaningless. I know what 1 is, but why is it being used?
This makes more sense:
Admin
TRWTF is Snoofle, for writing out a ternary operator on three separate lines. Seriously, who does that?
Admin
+1
Admin
Admin
Counting zeroes is so tough!
Admin
I have done something similar a few times. Back in .Net 3.5, there was no class for complex numbers, so I wrote a class to do what I needed in VB.Net. It included:
Admin
But this lets you change what the values are. For example, ZERO could be 10...
Admin
I am a highly paid consultant myself, but I would never stoop to these standards!
Admin
And in C you can write
So much more obvious than the fortran. It'll compile just fine too.
On a slight change of tack, one reason for not using magic numbers was exemplified in a codebase I worked on. There are 86,400 seconds in a day. Except in the bit of code that thought there were 84,600 seconds in a day.
To add to the fun, 6pm was an important time. 6pm is 64,800 seconds after midnight. And yes. Somewhere those two numbers got mixed up.
Admin
MAX_AIRSPEED_OF_AN_UNLADEN_SWALLOW_IN_MICROMETERS_PER_HALF_HOUR = 1000000000 //one billion
that was tough
Admin
I am already HPC mode developer here. After completing Prince 2 methodology and PMP certification, here I come.
Admin
Why not use exponential notation? You know exactly how many zeroes there are then. MAX_AIRSPEED_OF_AN_UNLADEN_SWALLOW_IN_MICROMETERS_PER_HALF_HOUR = 1*10^9
There could be problems if you have a dodgy compiler that was programmed by someone who failed primary school math and doesn't understand order of operations. 2*10^9 ends up becoming 512 billion instead of 2 billion
Admin
You're kidding, right? In C, you can write
but it doesn't mean what you think. I gets assigned the value 1 and the comma operators effectively cause the 0's to be ignored.
Admin
I didn't say it'd do what you wanted to. I just said it'd compile. I'm not quite as daft as you think.
Admin
Admin
Lines of code is a great metric to use - as long as no one knows you are using it. Anytime a metric is known programmers will game it hard.
You can find high performers and low performers based on how little or how much code they write. Some high performers write a lot of code and some write a little code. Some low performers write a little code and some write a lot of code. Normally, these two groups do not end up with an average amount of code. You can use these stats as hint to look at the actual code to judge it.
Admin
Admin
Actually, they were COBOL "experts."
Later, I replaced their stupid external C function with the equivalent Microfocus COBOL native functionality. "Learn to read the manual!" dudes. :-/
(I normally do C, C++, Java, C#, etc. But I have used more primitive technologies to pay the bills sometimes.)
Admin
It's not like they could have just done
or just called str.substr() wherever they needed it.
Admin
Admin
Wait, I was always under the impression that any decent compiler (VS, say) would take something like "int num = 3*47 + 121;" and do the calculation during compilation so the generated assembly code only contained "num dw 262".
I might have to go back and touch up a few things...
Admin
In a case like that I would use something like
static const long MAX_PRODUCTION_RUNS = 100 * 1000 * 1000;
The compiler will resolve it to the same value at code generation time, but the intention is much clearer, similar to how 100,000,000 is a lot more readily human-parsable than 100000000.
Admin
Admin
About ONE_BILLION... which side of the pond is it, and is it old enough ?
It seems north America has it at 1e+9 whereas old English granny took it at 1e+12. (depend on long vs short scale... )
Admin
Reminds me of my Atari BASIC days.
Admin
That is cheating a bit: in a ring, the "numbers" 0 and 1 are actually not numbers, but ring elements named 0 and 1. So there is a very good reason to have constants called ZERO and ONE, but there are not just 0.0 and 1.0
Admin
Not sure if this answers your question.
ONE == 1, TWO == 2 etc. don't make much sense. But numeric consts such as MAX_BUFFER_SIZE = 1024 * 64, MIN_PASSWORD_LENGTH = 6 etc. do make sense.
Essentially, you should have a constant when it contains a number which is arbitrary, from the implementation's point of view, and might be changed at will by the business side decision makers. If you do use constants, you should name them according to what they are used for, not what they contain.