- 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
<font size="2">Sorry, but I do maintain more or less such code. so having someone defending it makes me thinly weiledly passivly agressivly sarcastic or however you say it in English. and as far as I am concerned - sometimes only a good knockout might bring someone to the path of rightneousness. ;) </font>
Admin
Amen, Brother!!
Admin
<font size="2">Is anyone as much interested as I am where does Xarium take his wisdom from? Xarium please share. I am really anxious to know.
</font>
Admin
If he needed those constants, then why didn't he put them directly in the functions?
Admin
oops
inline double Meters2Inches( const double &m ) { return m*( 1.0 / 0.0254 ); }
Admin
I really need to throw in my $0.02... The code is funny - but it must be pretty fast.
1. "inline double CM2MM( const double &cm )" is a pass by reference which is C++ only (that little &). So this can't be C.
2. All the constants are divisions, so comments like "he didn't even use the constants for 2.54" are just silly.
3. This is obviously built for speed... Has anyone wondered that this code really smells like Find & replace or a tiny shell script
If he started off with a file full of lines like: mm -> inches 1.0/25.4 0.0393700787401575 it takes about 4 lines of linux shell code to convert it to the double declarations, replacing "1.0/" by "One", "." by "Pt", adding "th = " at end, does most of the work.
Doing it like this is a lot faster to code than using descriptive names for everything.
Admin
<font size="2">At least he... um... formatted it nicely? (Where's a code reviewer when you need one?)
</font>
Admin
Yeah, this is C++, not C.
And not entirely bad. My guess is that all the things that are intended to be exposed -- the conversion functions -- have reasonable names while the things that are not intended to be exposed -- the constants -- have tortuous names that are unlikely to collide with anything a programmer using what this provides might dream up.
The practice of making constants manifest as some sort of symbol makes sense -- when you use the values more than once. I can't see that this is the case here, but it's a common practice.
Limiting the scope of the constants to the file in which they're needed might be considered -- unless one wants to protect against another value of, for instance, Pi fr,p being defined. (Although there's not much chance of another One2Pt20462262185th showing up at the party, is there?)
Admin
I can tell you exactly how this happened. It's required by the coding standards imposed by a misguided IT manager. "There will be no hard-coded values anywhere in the code." You have to create constants for every number and string. The rationale is that you need to be able to change the variable's value globally.
If your code was evaluated with rules-based code review software including this rule, and your coding quality was assessed based on the number of points racked up for breaking the inflexible rules, you'd end up with this madness.
Admin
[|-)]
<font size="2">
But I'm still waiting for your rebuttal. Abuse doesn't count.
</font>
Admin
that's not quite the answer to the question I asked. I do not even have to remind it. it's quoted above.
I do not think I have to rebut anything. I said what I wanted to say. and maybe if you try to read it you will get it. I really appreciate your knowledge on C/C++ - I always admired people who could remember all detail about how it works were in what conditions, I am simply disagreeing (whatever you spell it) with your defence over stupid constants names.
get over me... and tell me in what 'real world' they call libraries deterministic and non-deterministic. where do they name constant like that. where do they let you write such code. I just simply do not want to get somewhere I won't belong in the future.
Admin
Hey, it's all good. I'm still in school (CS major), and have not made it to the IT ranks just yet. Needless to say, I don't have to maintain anybody else's mess. Maybe the code brought up flashbacks of a past traumatic experience, pushing you to the brink of openin' up a good ole can of whoop'ass.
Or maybe I've just spent too much time studying the finer points of Psychology and overanalyzed the situation. [:P]
Admin
Nice. I think you've come the closest to explaining what was really going on through the coder's mind. Let's think about this for a second: Nobody in their right mind would use constant names that are actually HARDER to remember and retype unless they had an ulterior motive. It's not like he was just lazy and named his constants A, B, C and so on.
It is possible that this code, within its context, is not a terrible WTF. While one could argue that every single post here in the Daily WTF is taken out of context, this one is just too bizarre to be a result of sheer ignorance.
While we are only allowed to see one tree, a forest can be inferred.
Admin
<font size="2">yeah... once upon a time I, too, was at the univ and I thought the programmers community couldn't do as much crap as we are doing (I, myslef, can also be ashamed of a few of my inventions) but... unfortunatelly... yup... I am traumatized... and this code- </font><font size="2">esspecially the </font><font size="2">constants- remind me of this trauma.... ;)</font>
Admin
This code is simply bad. I mean very-very ugly and probably totally useless. CPU time, memory savings .. etc can't really justifies these constant "names", not even that example a few comments before mine. The division-multiplication debate makes a difference, but we're not in '90, when assembly was very popular.. even "embedded systems" are smarter than this. And this is a wtf because nowadays any compiler would change the pi/2 into 1.570796.. as long as it can fit into the varible of that type. And someone before me pointed out that this code must be C++, and there are a lots of good compilers for C++, and that's not just an urband legend ;]
and happy new year :P
Admin
There are 63360 inches in a mile. The names are so horrible that it is difficult to make the connection. I tried factorising and got suspicious when I saw 88 as the number remaining to factor. If I had not factored out the threes, I would not have caught it.
Sincerely,
Gene Wirchenko
Admin
Yes, I was the retard who tried to say this was C when it's "obviously" (lol) C++, prompting every other post to point that out. I need to be taken out back and shot. My B. :-/
Admin
How did this get by:
Admin
I've read your posts, yet you obviously haven't read mine. Where do I defend the constant names (please quote me specifically defending the constant names)?
Yep, I agree, they are a little strange, but that's it. No big deal.
You're making out like this is the worst code in the world - and that tells me you haven't seen any real world code.
You are ignorant, you were noticed and you cannot admit it.
By your own admission you write only Java, therefore, you've had no experience with implementing real world math libraries - your world is very small, don't assume everybody's is.
Admin
"Defending this code with numbers of how many CPU cycles are spent on different things on an Intel CPU? Does anyone know for sure it is meant to run on an Intel CPU?"
It doesn't really matter if it is an Intel or not (btw, Intel make lots of other CPUs, not only x86), I've yet to see a CPU that does division faster than multiplication. I'm sure someone can dig up there pet Über-divider-CPU, but it is a fair assumption that you gain speed by removing unnecessary divisions.
"Does anyone know if the code will run millions of times per second so that it actually makes a difference?"
If it does, the code makes sense. Why assume everybody are idiots just because it is usually true?
"Did anyone benchmark this code and see that it will actually be faster?"
We need the complete program for a sensible benchmark, but it will be faster. If it is a noticeable and important difference is a completely different question.
"Premature optimization is a favourite passtime for a lot of people who have once learned a little bit more about how a CPU works. It seldom does what they expect, or at least not as much."
How do you know it is premature? Perhaps it is a response to performance problems?
"Still, they gladly make the most bizarre code just because "it's faster" or "saves bytes"."
Try programming for embedded control systems and you'll see exactly how important 'faster' and 'save bytes' can be...
"Then again, if seeing this code gets you thinking about CPU cycles instead of the horrible constant names, then maybe there's no point in arguing..."
I agree about the naming, it could be better. Also, if these constants are needed in a tight loop, I would prefer to define them just before the loop to keep things together.
Admin
<font size="2">
I am ignorant! I admit it!
In my small world 'deterministic' means the opposite of stochastic. and those libraries of which you are talking are called 'dedicated'. I assume that one needs those 'dedicated/deterministic' libraries, not only for the reasons of the speed, but also the acurracy of the calculations. I have actually implemented a library to numeric calculations and it was done in C, but... yeah... that was not a real world. it was my own small world where we- only Java experienced- assume that people could make use of our code. that's why we like to give names that mean something. that's why if we create a constant we use it afterwards. but hey... that's just my small world. and </font><font size="2">I am ignorant! I admit it!
does this make you feel better? or maybe you want to insult me some more because I deffinitely don't like some parts this code? maybe you have a problem of low self-esteem, if you felt so offended by what I wrote? or maybe... maybe it's because I am a woman?...
whatever it is </font><font size="2">I am ignorant! I admit it!
happy?</font>
Admin
ooooh look. A cat fight...
Admin
<font size="2">
How about you both chill down a bit and drop the provocative attitude?
The thing about the "deterministic libraries" makes perfect sense to me, even the name (guaranteed running times = deterministic) though I doubt the claim that banks care about guaranteed running times except in a few very specialized applications, namely stock brokering.
But it really doesn't have anything to do with the criticism of this code. which is mainly for the really, really bad variable names.
</font>
Admin
Not entirely. A constant named after its value is still a LOT better than using that value everywhere. If you mistype the name, you'll get a compiler error rather than a wrong value at runtime.
Admin
It's like half a wheel.
Admin
That's "You must be new here, you moron", you idiot!
Admin
Floating-point math includes exception handling and state bits that indicate the result of prior operations. For example, look at the INEXACT flag in most processors.
As a result of the need to keep this meta state 100% correct for some numerical computations, compilers often do not fold floating-point computations at compile-time. If they do, they often provide a flag to prevent them from doing so.
Consider another well-known example: a != a. Surely a is always equal to itself, right? Not so with IEEE-754 floating point numbers. a may be a NaN (Not a Number) value, which for the purposes of comparison is not equal to itself. Older versions (if not current -- I haven't regressed this) of Microsoft Visual C++ would always incorrectly eliminate this comparison.
I suggest everybody here hunt down and read Goldberg's "What Every Computer Scientist Should Know About Floating-Point Arithmetic."
Admin
Think someone should break it to the guy that 1900 was not a leap year?
http://en.wikipedia.org/wiki/Leap_years
Admin
More like "You must be new to the internet"
Admin
Cool! Is it GPL? Can I use this?
Admin
Apologies if someone already said this...
but shouldn't the various items reference one another? He makes a good start by defining and then using pi, but isn't OneThirtysixth equivalent to OneTwelfth / 3.0? Or OneTwelfth * OneThird? What if he needed to change one of the basic factors, like define OneThird as 1/4? He'd have to edit every line item...
Admin
This exactly shows the author's lazyness. I sensed there must be some reasons for:
<FONT color=#000099>const</FONT> <FONT color=#000099>double</FONT> One2Pt20462262185th = 1.0/2.20462262185;
whilst:
<FONT color=#000099>const</FONT> <FONT color=#000099>double</FONT> One4Pt44822th = 1.0/4.44822161525;
My guess would be, the application asks for higher accuracy in weight than in force.
Admin
I imagine it's something very much like that. I went on a SQL Server development course a year or so ago where the trainer related a story about a company he'd worked at where this kind of thing went on.
Admin
double Round(double val, int precision)
{
double v[] = { 1, 10, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8 }; // mgl. verlängern
return floor(val* v[precision] + 0.5) / v[precision];
}
Admin
prednisone without prescription medication: http://prednisone1st.store/# buying prednisone mexico