- 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'm sure we've seen this before? .... although I guess it happens a lot, I've certainly seen lotsandlotsandlotsandlotsandlots of it.
Admin
I've seen enough apps in my time which do something tree-ish and nesting-ish, and the word is that there is a limitation to an arbitrary depth. When asking why that is the case, I get told, because it just is, all right?
So, no actual business requirement, no words in the specification (apart from the post-hoc-propter-hoc weasel-words in the user docs), just a bossy-boots with the emotional development of a 6-year-old telling me it can't be changed Because. And no, we're not going to fix it just because you need it to work to 4 levels deep not 3.
Of course, when you go to inspect the source code, you see exactly the same sort of blethering stupidities like we see here.
Admin
And I suppose that adding meaninful comments as to the intent of the code is also something they are going to start doing...
Although, based on the assertion that their code base is littered with code like this (copy paste), I'm also guessing that doing code reiviews is something they (as in their coworkers) also intend to start doing...
Admin
I think the comment only refers to this blob of code which is copypasta in itself, but of course, this doesn't exclude the possibility there are more of those.
Admin
"Works for now." There are few things more permanent than temporary code.
Admin
I don't know whats worse copy-paste programmers that don't really understand recursion and are afraid to actually try it or copy-paste programmers that don't really understand recursion and run with it anyway.
This is a good example where I suspect a loop (you pick your favorite) structure would be much more understandable and probably at least as efficient to execute and not much longer to express. It also does come with all the potential gotchas like stack exhaustion, will it ever terminate, for the amateur hour developer here.
Admin
So he's formatting a long string by putting
<br/>
tags between words. He could re-write this with string.split:if (website_description.length() > 25) { String part1 = website_description.substring(0, 20); String other = website_description.substring(21); String parts = others.split(" "); websiteWrapped = part1 + "
" +others.join("
") } else { websiteWrapped = website_description; }
Admin
FTFY:
Admin
Oooh my bad that's actually java not javascript. So FTFY should actually be:
Admin
To do: write a meaningful comment, but this works for now.
Admin
If you're going to nest this deeply, the least you could do is use smaller indents.
Admin
Hardly a WTF, the code is correctly indented!
Admin
What's supposed to happen if:
Admin
Pick your poison:
Jake's team doesn't review code changes.
They review changes but, but Eddie is "that guy" who refuses to cooperate in the process.
Someone reviewed and approved this code.
Admin
I'm definitely guilty of this sort of thing. When you know how far you need to go, sometimes it's just a whole lot quicker and easier than doing it the right way.
Admin
How To Achieve Brevity:
Sometimes you just have to recurse Sometimes you just have to Sometimes you just have Sometimes you just Sometimes you Sometimes
Admin
I'd say one of the things I like about recursion is that if someone who doesn't know what they are doing attempts they usually spend so much time rebooting their computer and trying again that the end result never makes it anywhere near production.
I'm sure someone here has good horror stories though.
Admin
This is a poor candidate for recursion in the first place, but besides that the first rule of writing recursive code on the JVM is: Don't
Admin
Actually, I've HAD to do this because of the stupidity of the VBA handling of SQL large text fields. Even though both the VBA and SQL are large text, somewhere in the data flow path it truncates the 2000 character note field. So I had to write the most ghu awful code to look for a space somewhere around character 200, create a d1 field from 1 to that. Then look for a space around 400 and create a d2 from the first space to this one, etc.
I did more than the usual Google searches and MSFT KB and found reports every couple years of this since 2003, never resolved.
So although it's a horrible hack, I felt in good company.
Admin
Knowing nothing about the JVM (I work with .NET right now), I'm sympathetic with that view.
Limited recursion, provided you obey the golden rule of reducing your data set on each call, is fine. Ish. But there's no tail recursion on either platform (neither is there on Python, because Guido famously objected to it). Effectively, this means that unless you use trampolining, you're gonna bust the stack at 256 frames ... and let's be honest, nobody ever thinks very hard about that limit.
What happens in Function Programming ... stays in Functional Programming.
(Mind you, I assume F# has a way round this. No idea what it might be.)
Admin
That's a common phrase used about those temporary prefabricated classrooms you find in a lot of schools, you know the ones, older than most of the kids and yet somehow only supposed to be temporary.
And yet, in the lycée (it's in France ==> it's a lycée) across the street from my appartement, they are (slowly) pulling down a bunch of them. (They've been standing empty and roofless for a week now, although the snowy cold might have something to do with that.=
Admin
The CLR has native support for tail calls, there's a dedicated IR instruction for it. Although AFAIR nothing but the F# compiler emits it these days.
Admin
Technically, the .NET runtime does have a
tail.
opcode, which must proceed a call to method, and allows the current stack frame to be destroyed before calling the method, rather than after. However, C# does not appear to make use of this opcode currently, which is why it would appear that .NET wouldn't support tail calls.In the case of F#, in addition to making use of the
tail.
opcode, the compiler will prefer to rewrite the recursion as a loop if it is able. As an example, if a recursive function is implemented as a pattern match over a DU, where some of the cases recursively call the function as the last operation in the expression, the IL emitted is actuallybr IL_0000
, which is essentially a goto statement pointing to the first instruction of the method, essentially making the entire method awhile(true)
loop.Admin
It's just as well they don't add a comment. They'd just copy/paste the comment along with the code.
Admin
Given their existing approach to co, co, coDING, refactoring is predictable. A recursive call just inside the top level "if" and then ... "turtles all the way down."