- 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
I don't get how so many of you are making identical coding mistakes here. You're doing code roughly as
- Do a loop to get a value for a variable
- If certain conditions are met then set variable to a particular value
- Return variable
Why do you make it execute the loop?
- If certain conditions are met then return a particular value
- Do a loop to get a value for a variable
- Return variable
captcha is all-knowing as ever : wtf
Admin
I had a teacher like that once. He was the only computer teacher in the building, and was piss poor at what he did. It was me and a few of my friends in a programming club, and I was the only one who knew jack about programming. Eventually, the teacher handed me the marker and offered to let me teach the class. I took the marker and proceeded to show my friends what a method was. I talked to them later, and they said that I taught better than the teacher did. They did say that it was a sincere compliment, not just because I was their friend.
Admin
Admin
It is not so much that as not being able to handle a function being called before it is defined. Without this, you can not have recursive chains (function A which calls function B which calls function A). Some languages allow declaring the parms separately.
Once the compiler has the formal parameter list, it has enough information to handle calls, recursive or not (barring a weird language).
Sincerely,
Gene Wirchenko
Admin
To get off the "recursion is bad/recursion is good" thread for a second, I have seen (horrible) code like this on a project I worked on. It was a C# front-end that had been written by a back-end (SQL Server) kinda guy, who had no idea what he was doing in UI-land. Because the company had a similar amount of cluefulness they made him do it.
Basically the entire application was run in the UI thread from the callback code of each of the toolbar buttons!!! But of course he had to allow the same actions to be performed from menu selections as well. So each top-level function in the program began with some similar "is my parent null, or is my parent's *parent* null?" code to work out what context the function was launched from. THEN, depending on the outcome of that, it would set the new UI window's parent to the appropriate container frame. Then the REAL fun began...
I made a point of grabbing a copy of that app code when I bailed - I should submit it here, there's about a fortnight's worth of code WTF's in there!
Admin
I find it interesting that everyone is making a lot of assumptions about how this is "supposed" to work when they re-write it.
Maybe the tree is not allowed to go more than 60 levels deep. Maybe a circular loop of somekind *is* allowed. If that's the case, then we've got a lot of people writing infinite loops here.
Admin
Perhaps - but the important point is that you can also curse in every language. Not to mention at the language. VB, I'm looking at you....
Admin
We need more Rube Goldberg ones like this:
http://hardware.slashdot.org/comments.pl?sid=187109&threshold=1&commentsort=0&mode=thread&cid=15439517
Admin
Aha! So it is your damn fault that we have to work with all these hopeless monkeys! I had always wondered how some people had managed to get a degree... until now I guess I had assumed they had made some lewd and nasty arrangements with dirty old professors...
Grab your torches and pitchforks... let's get him!! :)
Admin
That won't work in all variants of BASIC. I'm not sure about any others, but GWBasic only stored a single return address, no matter how many GOSUB's you have. Basically, you had functions, but you couldn't call another function from within a function and still be able to get back to where you started. I'd say that qualifies for a WTF.
Admin
<font face="Verdana" size="2">Technically, recursion is a language feature. Just because you have functions doesn't mean you can use them recursively. Support for it has to be built in. There are many languages that still don't support recursion to this day, for a variety of reasons (i.e. the ability to inline all functions, etc).
The more I read comments from the "experts" on this site, the more I think the people who read this site do it so they can see how to improve their coding technique.
Go read about language design issues, or at the very least stop posting from you ass.
</font>
Admin
That's because PIC only has a stack 2 or 3 levels deep. That's what happens when you have well under 1k of RAM. PIC16F84 has what, 68 bytes?
Not if you're programming in Scheme. :)
Admin
Not that I believe you, but if this was the case then you would lose in the long haul because you have to compete with a bunch of retards that have the same qualifications as yourself, thus lowering your potential earnings and job security.
Admin
Well, he could have counted to 17 instead. Its a 7 by 10 rectangle with 1 parent missing. 69. :D
Grats to Xocomil for being the first to spot that its really one if else with a bunch of unreachable space between (assuming they don't have something weird, like where a node changes when accessed, so you can't know its value and its location at the same time (no, I didn't invent that example, I've seen it done, mostly to torment/test a new programmer in python) :).
And I agree with "you bastard". Spork adwadwawda!!
Admin
I bet it does, it was the next feature after 'on error resume next'.
but obviously. VB programmers doesn't support recursion.
Admin
Take other note: I've had plenty of people work with me or for me with CS degrees and masters (that I didn't hire) whose code was just as bad as that. I'll take my chances talking to someone and asking them questions and listening to the answers rather than looking at worthless pieces of paper when hiring.
I know some damned good programmers who wanted to get to work rather than go to uni.
Admin
Maybe I've just had a very different experence than the rest of you, but all we did in my first CS course was recursion. My first-year courses went something like this:
Scheme Lambda Calculus Assembly Java
Admin
I'm not sure what you call functions :-) but I seem to recall that Fortran 77 did
not support recursive function calls. Fortran 90 and later does. From what I now know
about computer science I actually wonder how they built a language which supports
functions but not recursion. It seems you would have to add extra logic to
prevent adding an extra stack frame for the same function.
Admin
Fuck, I didn't know you could get a degree in Counter Strike
A lot of students graduate that don't know the most basic programming principles, but they do have a *lot* of creativity in solving programming problems in the most unimaginable ways. Some of them aren't even really interested in writing software (and they are the ones that get employed at a company, start writing their enterprisy application from crap, eh scratch, and afterwards we have the joy of maintaining it).
Plus the whole load of elderly people that write their super duper software in Access and then try finding a professional to maintain and extend it (by sending us letters reading "Skills needed: MS Access, VB Script, blahblah more stuff that isn't relevant).
Plus all the say.. 28+ year olds that don't even know OO exists, yet they try to mimic it vaguely by using an "ancestor" once in a while.
I'm in hell.
Admin
Hmmm. What are we talking about here?
Ahhh ... I see ... Youngsters talking about stuff that's relevant to their generation!
You haven't figured out that it's all crap after about 1970 yet eh!
YAWN!
:-)
[No harm intended :-)]
Admin
An iterative solution would work just fine here (you get C, because I don't know VB):
Admin
Public Function GetNestingLevel(objNode As Node, maxRecursion as Integer, RecursionLevel as Integer)
If RecursionLevel < maxRecursion Then
RecursionLevel = RecursionLevel +1
<All other Code>
End If
End Function
(I hope the VB-Code is correct, i haven't wrote VB-Code for years)
No problem with that...
Admin
I had one instructor that I put a certain amount of fear into. When I challenged him on deducting marks because I did not validate a name by checking that it was not numeric, he challenged me to come up with a case of a numeric name. I came up with two.
Which were ?
I mean, I certainly can't come up with any.
Admin
<FONT face=Arial>This looks to me like its the nesting level of a node in a VB6 Treeview control. So you should be able to do something like this.</FONT>
<FONT face=Arial>NestingLevel = Ubound(split(objNode.Path,"\")) </FONT>
<FONT face=Arial>There is a property on the Treeview that tells you the path seperator is but I cant remember it.</FONT>
Admin
That wouldn't happen to be the same "days" when you also had to go out and collect your own bits, barefoot, in the snow, leaving bloody trails for the wolves to follow?
Shamelessly copied from Kent Beck, "TDD By Example" ...
Admin
Not true. GW basic works just fine -- just tried the example above and it goes all the way. (copy from the gwbasic window:
load "a"
Ok
list
100 REM RECURSION EXAMPLE
110 LET L=10
120 GOSUB 200
130 PRINT "MOST INEFFICIENT WAY TO SET L TO 0 COMPLETED"
140 END
200 LET L=L-1
210 IF L>0 THEN GOSUB 200
220 RETURN
Ok
run
MOST INEFFICIENT WAY TO SET L TO 0 COMPLETED
Ok
- ------------
The classic basic has no procedures, no locals, no variable stack, and implementations generally had a fixed stack for GOSUBs. And it was specified in dox, that you can go 32/40/whatever levels deep before an error occours. Like this same gw-basic:
list
10 PRINT "*";
20 GOSUB 10
Ok
run
***********************************
Out of memory in 10
Ok
Admin
I call BS! (and that's not Bachelor of Science!)
I've had people writing their thesis and they just UTTERLY DIDN'T KNOW TO CODE THEIR WAY OUT OF A ROOM WITH NO WALLS!
With sites such as Rentacoder.com providing paid homework help for inept Computer Science students, it's just as much of a gamble as hiring someone without a degree.
Yay, Captcha is "genius" :)
Admin
Well, yes.
Of course, you've already made an assumption - that stack frames are used. Think about an alternative mechanism, wherein each function had its own statically allocated 'frame' for its locals. At this point, you could have an arbitrary calling depth, but no function could be in that pile twice.
Back in the days when stacks were much smaller than RAM, that might have made sense.
Admin
I am 32 years old programmer, and I love OOP. I even write elegant javascript code in OOP that is short than the equivalent procedural code!.
--Tei
Admin
<FONT style="BACKGROUND-COLOR: #d3d3d3">dpb wrote the following post at 05-31-2006 5:02 PM: </FONT>
//CAPTCHA = craptastic</FONT>
Right. And the noun would be "recurrence," not "recursion."
Admin
Just use a language in which you can't turn tail-call optimization off in the first place (Scheme, Lisp, most if not all functional programming languages in fact).
Admin
First: Will do wonders to the team spirit when Greg's new subordinates find out that Greg posts their work to dailywtf for the whole internet to look at.
Second: I doubt that evaluating skill levels from smallish pieces of code is very effective. I would rather give them a piece of code and ask them to figure out what's wrong with it.
Admin
Don't see the big WTF here....
Looks like something that evolved through time...
Day X:
We need this function... There are only 4 possible nesting levels - the last 2 depending on the special case objNode.NodeId. There will /never ever/ be more. - so the part till the nested if was built.
Day X+A:
We just made a redesign... Now there is another Level possible - so we are at a max of 5 levels - and there will /never ever/ be more... - so one else was added.
Day X+A+B:
On Level more - just an if added and else modified....
And some alterations later the actual maintainer decided "I won't touch this piece of crap ;-)" - so they just used the good old cut-n-paste-technology - maybe even an emacs/vi-macro an F12 or so in the meantime ;-)
So it's crap for shure... But I wouldn't be too surprised if many code-pieces evolve like this throughout the years. Shurely one could make a handy 5-liner out of this... But why ? It's ugly, braindamaged, ... yes. But it works. So why should someone invest the 20 minutes to rewrite it ???
Admin
Well, if they can't work out what's hideously wrong with their own code, what point is there in giving them someone else's?
Admin
Bah! Back in my day, we recursed deep into 200 levels, in machine code (HEX), both ways, hum, under 3 feet of snow.
-dZ.
Admin
wtf are those guys doin' here?!
Admin
Luckily, there are language nazis with better skills around (and perhaps a dictionary to boot!) to keep you straight.
The noun is "Recursion", and its a term used in mathematics and computer science. There is actually no real verb associated with this concept, other than "to perform recursion". It is not "recur", as the concept of recursion involves more than just repetition.
Some, more liberal, dictionaries (particularly online) have adopted "recurse" as a verb, perhaps because the use of the term is so prevalent nowadays.
-dZ.
-dZ.
Admin
Not that I believe you, but if this was the case then you would lose in the long haul because you have to compete with a bunch of retards that have the same qualifications as yourself, thus lowering your potential earnings and job security.
Why don't you believe him? I did the same thing. None of the people I helped ended up being developers but quite a few went into IT related fields (network admin, etc.). They weren't retards, they just couldn't code. If you really think that a couple of incompetent programmers with a piece of paper hurt your potential earnings and job security perhaps you are in the wrong field as well.
As for recursion, we did that in first year. Unfortunately, due to hangovers beyond my control, I missed the first class where we were introduced to it so the following day when asked to write a tree traversal algorithm I came up with an "interesting" 900-line piece of crap that could be done in 5 lines with recursion.
Admin
I'm glad you're not my colleague.
And, BTW, it's "surely"'.
Admin
I've met people, even in management, who are afraid of recursion.
I once had to implement a "recursive" breadth first using a queue not because I needed breadth first but because management banned recursion.
Admin
actually did what the function said it would, and only had a single case of undocumented and out-of-place mystery business logic
Wait, where's the out-of-place mystery business logic?
Wait... where's the logic? I can see the monkey business... did you mean out-of-place monkey business logic?
Admin
"I'm glad you're not my colleague."
Me too ;-)
If there is time for rewriting ugly but working code - I'm with you. It should be done.
But we really don't know how this piece evolved - maybe there've been much more if/then/else special-clauses in the meantime - and only the one posted survived... Who knows ?
Fact 1: it's working.
Fact 2: Out of purely aesthetic reasons you'd demand to fix it
Fact 3: This /only/ makes sense if you really don't have any points of more importance on your todo-list...
Fact 4: We don't know if this piece of code is very "volatile"... If there are changes to this piece on a daily/weekly basis, I'd still leave it as it is..
Fact 5: If this piece - thrown out of it's context - is the one-and-only-really-big-WTF in the company - then it's a superb one ;-)
P.S. 'And, BTW, it's "surely"' - thanks for the correction... haven't been writing in english for about 17 years - so many things have slipped my memory...
Admin
HAHAHA
Admin
I'll go further, and say those of you who considered not using recursion in this case are WTF-enabled. Iteration and recursion are mathematically equivalent, in a sense I won't try to explain to this group of ignorant twits, but if you want to write good code, you will use the method which reflects the problem at hand. Things like "node level" are defined recursively, so they should be coded recursively. Otherwise, your code will be ugly, and, therefore, wrong.
Admin
You should use the method that best fits the problem at hand. Recursion may seem to be the most natural fit for the problem, however, recursion requires extra resources and incurs a performance problem over a loop. In this case, it would appear that a simple loop gives the best trade-off and is the best fit. Of course, by calling everyone ignorant twits you have demonstrated your immense superiority and have rendered all counter arguments, no matter how sensible, impotent. Well done, sir.
Admin
If you've got it, you've got it. I know people with CS degrees who cant understand a for next loop, also know loads of people with degrees in other subjects who are great coders.
As someone without a CS degree it really shocks me to think lecturers are giving lectures about what an iteration is, surely this should be background reading? If the CS courses are this dumbed down, it would explain the number of highly dynamic IT consultants we see working for big companies who havent got the first clue about IT.
Admin
Admin
rofl. Thats a good one. But also could be autogenerated, saves time when you generate your code ;)
Well, just give the objNode a Variable of integer called parentLevel.
And if you create an objNode, its 0 if parent==0 else its parent->parentLevel+1;
Or use iteration ;)
Admin
Heh. I can't believe how few people seem to have noticed that. However, I think you're still not quite right: I think it should be more like:
cheers,
DaveK
Admin
Because having to manage people like that is grounds for suicide? Keep in mind I see the wtf as that little function being the best of what the subordinates thought was their best code. Fighting that kind of institutionalized stupidity is like trying to fight the tides.