- 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
To be blunt, if somebody can't understand this piece of code, they shouldn't be pretending to be a C coder. If they ARE pretending to be a C coder then that demonstrates a lack of self awareness, humility, and intelligence.
oooo! OO! OO does not remove the need for algorithmic coding. And that's all that this !WTF was. Which bleeding edge, commercial, OO language could you be proficient in without seeing for loops, mod and div? Sure you might not see the scope rules or pointer arithmetic in that language, but if you don't understand those you shouldn't be working in C.
And would you seriously criticise someone elses code in a language that you didn't understand?
Admin
I think what the author meant to paste was:
Admin
jspenguin: Your tour the force is impressive, but by calling it _format_num (with leading underscore) you impinge on the reserved namespace. No banana!
Admin
The fact that you need to make such an aggressive response to it suggests that maybe I touched a nerve? Or are you just another whingy internet snot trying to prove that you're better than everyone else? Or do you actually think I'm dumb because I didn't understand the code instantly (in which case I will have to dislike you, obviously...)?
Admin
Admin
Cool thing about jspenguin's implementation is that now it takes longer to read for a C programmer but a non-C programmer may read the code... which is useful because?
Admin
Speak for yourself. I graduated just a few short months ago and I had no trouble reading this code. There is literally nothing in that code that I didn't learn in college. Including pointers.
Admin
http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numbergroupsizes.aspx
BTW, Japanese uses the same format as English.
Admin
Why has this comment elicited so many aggressively defensive replies? What did I say that was so offensive?!?
Admin
Don't you mean "real programmer?" (http://www.pbm.com/~lindahl/real.programmers.html)
Admin
I've been asked more than once to stick my head in applications written in languages I have absolutely no knowledge of, and the occasional relief of a pertinent comment is like nectar, believe me ;^) (Ok, jspenguin's may be a little verbose on the comments, but in this situation I'd always prefer too many comments rather than too few!)
Admin
03 NICE-NUM PICTURE --,---,---,--9,ZZ.
Admin
Yes, I did bother to read your monster comment.
Hmm. From what I gather, that wasn't implied at all. You can sprintf the current value % 1000 just the way the single digits are written into the buffer in the original snippet. The readability would improve somewhat. I fail to see other possible advantages. I agree that the high speed (which was probably intended with this code) would suffer.
Admin
Exactly, more to the point any such person who then passes themself off as such a good C programmer that they can deride other people's work deserves all the lambasting they get.
I.E. you'd use a type conversion. Type conversion isn't a necessary or sufficient feature of an OO language, pretty much any scripting language or shell will do that! Again it's a general rant, there are a lot of people who do criticise code in languages that they don't understand, and to be honest it looked like you were defending them!I don't understand PERL very well, if I have to maintain some then it'll take me a couple of hours to work out what's going on, but that's work against my own lack of knowledge, not work trying to overcome an obtuse predecessor.
Actually it was necessitated by the implication that C is archaic, and that everyone here was some sort of biggot for expecting everyone to get what for loops do.
Let me get this straight, you're posting to a site called "The Daily WTF", a forum set up specifically for people to giggle and sneer at stupid code written by stupid people, and you have a problem with "internet snot"s ?;-)
Admin
Interesting to see so many people arguing over this method, I think it would be a nice feature on dailywtf to challenge people to re-write functions to see who can write the most optimal ;)
Admin
Admin
That could be fun, it'd be nice to see who can think up the biggest WTF code too;-)
Admin
This is standard C Stuff and it is almost beautiful bar the Global variable.
I write in both C and C++ for a living (yes they are very different but in a subtle way) I usually choose straight C for small utility functions like this ( write it as a macro if possible) because if you write straight C code correctly it is the most portable code on the planet (it is basically warmed over pdp 11 asembley).
Java people hate my java code because when I am sentenced to write java code I write it in C. As I say you can write C in any language you just have to find a way around not having pointers which is the only natural datatype there is that being on an unsigned integer value of the size that the operating system/CPU considers being a single work unit.
I could go on about why I like to do my FOR loops backwards but you would just think I a strange or something like that.
Admin
I was simply frustrated at all the comments of the type "Anyone who can't understand this code in seconds is obviously dumb and pathetic". I'll give you that anyone criticising the code without understanding it is a fscktard, but criticising other people just because they don't understand a particular programming language is just as bad. I consider myself a decent coder so to be told that I'm dumb because I don't know C really riles me.
Oh, and FYI I don't have a problem with internet snots in general, just whingy ones who think they're better than everyone else ;^)
Admin
Mostly because it opens up a HUGE can of worms over the great divide between old school programming and new school. You said "I went to school recently (insert snide comment about the uselessness of C) so therefore I don't know how to read this code." Most people take offense to that because we know better then to assume someone CAN'T read code. Anyone who programs for a living and doesn't wind up on this site can read that code. It's just some people (like those that have learned OO languages exclusively) aren't willing to put 5 minutes of critical thinking to use. Not saying this is what you meant or even what you said, that's just how (imo) everyone took it.
Admin
I was simply replying to Programmer (and, by association, a number of other people generally who said things like "anyone who doesn't understand what this code does is an idiot") when s/he said "any college freshman with a programming 101 should be able to grasp <this code>". The point is, I finished my Master's just over 2 years ago. I can read this code no problem, in the sense of following the flow of it. But, having no knowledge of C, I couldn't understand the code. So it's simply not true that anyone with a Programming 101 can understand it. Read it yes, follow the flow yes, but understand it? Not unless the course taught C.
As it is, everyone has now lost sight of the fact that I was making a specific reply to a specific part of a comment, and I predict a long, drawn-out flame war before I get sick of it, amke myself another cup of coffee, and go back to doing some real work. Life? Don't talk to me about life...
Admin
On the other hand, I can also sympathise completely: when I went back to uni it took a term for the software workshop people to get me writing Java with standard namings, as I was coming from a Perl and self-taught Javascript background where all my naming conventions has _ in: so a method that in Java convention would be called getHorizontalPosition I would name get_horiz_pos. They actually started marking my assignments down after the second week because I wouldn't conform ;^(
Admin
Ah, I see where you're coming from now. C doesn't have a "String" data-type per-se. Instead we use arrays of 8bit numbers. (Each eight bit number storing a single ASCII encoded letter) C can widen and narrow data on recasts, but it won't perform algorithmic functions to do that. (C++ however does.) There are some common library functions for converting between raw binary (such as the algebraic functions use) and human-readable strings... but if you use them you have to include an entire library, which is a pain if you're working on an embedded task, or on an OS kernel.
C describes very closely what the CPU actually does, which is why it's still popular in a lot of tasks that need direct access (like device drivers) low overheads (like OSs) and high performance (like multimedia codecs, game engines, etc).
I do have to admit that a lot of C programmers have massive superiority complexes, and that's probably due to being acutely aware of what the CPU's doing, and most of them can't imagine leaving a language to do this kind of thing for them.
With respect if you start off learning on a language which can inject a load of functionality on a "simple" operation, then this does wrong-foot you when you come to learn C, and then the old school C guys start circling like vultures (or like while(1);)
Ah you'll be okay here then, nobody thinks that they're better than everyone else, they all know that they're better than each other;->
Admin
Admin
gah, for(;;); depending on system you are implementing it for and compiler optimization it removes the test for 1 being a true value.
Admin
You don't have to understand it instantantly. But if you can't look at it for a few minutes and at least have an idea what it is trying to do, well, I won't say you are dumb, but maybe programming isn't the best career choice for you.
Admin
A few minor WTFs, already covered. 1. Static buffer--result is overwritten on next call, not threadsafe. 2. If a standard library is available, you can use sprintf() with your own buffer. 3. No comments; at least "// Converts int to string representation with comma separators" would make a 30 second job of reading the code into a 15 second job. Otherwise, this a fair way to do it.
Admin
[quote user="JimM"][quote user="Addison"]Mostly because it opens up a HUGE can of worms over the great divide between old school programming and new school. You said "I went to school recently (insert snide comment about the uselessness of C) so therefore I don't know how to read this code." [/quote]Lets compare that to my original comment, shall we:[quote user="JimM"]Some of us took "programming 101" in the last five year, at colleges / universities that concentrated on modern OO languages (you know, the ones a lot of modern commercial software is written in), and have only a passing familiarity with C[/quote]No, I see no snide comments about C - just that I didn't learn it.[/quote] <snip> [/quote]
You don't think people might have taken offence at the bold comment above?
It reads quite arrogant (to me, and I suspect many others) and sounds very "What would you c people know, use a real language"..... I'm not interested in getting into an OO vs Other debate, but I noticed you were surprised your comments had caused such a ruckus...I would imagine it's comments like the above one that may have attracted all that attention (but hey, I could be wrong....)
Admin
Nice try, but:
Jäg har 96 386-CPUar.
(Excuse me Swedish, I'm backporting from Danish here)
Admin
I'm just going to quote you, so hopefully others will see it too.
Admin
It is worse than that.
I dont write Java that often and if I am going to do some string manipulation I am not going to know about the system.out.strings.stringbuffer object so would just roll my own.
and I will start worrying about if something was allocated on the stack or in the heap and ague that it is the callers job to allocate the object not the called subroutine.
And then a holy war breaks out it would properly be best to let someone with passion for java write the code.
And dont get me started on what is in the language specification vs the class libirary.
Admin
Not a bad function. Yes, it has a few "flaws" but nothing serious. The even more subtle WTF is that the routine won't work (at least in a 2's complement system) at the most negative number. That is because it has no positive equivalent. A word to the wise to those who want to use the program.
Admin
/* differences: goes forwards, directly modifies buffer at dest */
nice_num_fwds(char* dest,long num) {
long i; long j; long denom; long charcount;
/* find number of digits in final string */
i=num; /* i will be used as our number to convert / if(i<0) i=-i; / incorrect for maximum negative / charcount=0; / # of final digits / denom=1; / denom will equal 10 to the power of charcount-1 */
while(i) { denom*=10; i/=10; charcount++; } denom/=10; /* adjust to charcount-1 */
/* convert the number */
i=num; if(i<0) i=-i; /* again incorrect for maximum negative */
/* and the rest explains itself :) */
if (num<0) *dest++='-';
while(charcount) { j=i/denom; *dest++='0'+j; charcount--; if(charcount%3==0) dest++=','; i-=(jdenom); denom/=10; } *dest='/0'; return; }
Untested and dodgy (ie. zero fails) but just did it as a concept of how much more complex the logic gets going forwards. As it turns out, it's not as painful as I thought, but still clearly less efficient. Oh, and the article is not a WTF, but you already knew that.
Admin
Thanks for the info: always nice to broaden my horizons into new languages. Not that my current work has much call for C (fricking web apps - I hate that I have a designers eye... everyone wants web apps! GAH!) but I'm sure it'll come in later (when I refuse to build any more web apps!)...
Admin
Why would you want to divide by 1,000 when you're converting to a string digit by digit?
Admin
I assumed the WTF was the potential for overspill. Otherwise, pretty much everything I've ever seen written in C or C-like C++ is a WTF. Although And C isn't any more about getting stuff done than anything else. C is about getting close to the actual computer behaviour. In many ways, that's counter to accomplishing a goal.
Admin
FIST!
Admin
Yow. I have to agree with the gallery - absolutely no WTF here. Perfectly good code.
jspenguin's heart is in the right place, but commenting to that level actually hurts readability, it doesn't help it. The original is compact, simple to understand to anyone with a passing familiarity with the language, and critically the entire function fits on the screen and is visible without scrolling.
Adding masses of comments so you can't scan/take in the function in its entirity is not an aid to understanding.
About the only changes I'd make would be - A slightly more descriptive name. Use #defines for the separator character and buffer length A single comment at the beginning along the lines of:
Admin
If that's true, I have code to fix.
Admin
I may have missed it, but I don't think anyone has yet posted an elegant method of handling MIN_LONG. This is my 30 year old signed integer to string code (designed for 16 bit processors):
I wrote a custom version of sprintf to handle separator character requirements; doing it in a modified ltoa function just gets ugly.
Admin
Is it me or is this actually a bit harder to comprehend than the original snippet? What really made me spin for a bit was that you don't use modulo math (which isn't goofy). But I like how you elegantly solved the ~0 issue.
Admin
looking at it, i think it would have been slightly faster if done with a modulo operator but then not all architectures have that instruction.
Admin
Admin
*--buffer = '0' + (n % 10); n /= 10;
Mine has one:
n = number / 10; *--ep = (INT_16) (n * 10 - number) + '0';
Divides on older hardware were very slow (~150 clock cycles on 808x) while multiplication was a bit faster, so one divide was used.
Admin
...using apostrophes for plural's;-)
Admin
Admin
Gotcha. All our old compilers did for us was throw in redundant x86 register reloads all over the generated code. :(
Admin
The code is pretty obvious actually. An it doesn't divide by 1000 because a 3-digit decimal number wouldn't be directly convertible to single digit's ASCII code... But yes, the name is silly and an explanatory comment would have been in order.
Admin
I agree with everyone: nice_num() was a very poor choice of name for this function. Something like xtoa() would be much more readable.
Admin
I looked through it briefly. It made sense. I looked through it carefully, looking for WTFs. I couldn't find any. I thought "WTF is this code doing on this site?!?!"
And then it dawned upon me. See that WTF thought in there? That's it! Good job! :)