Comment On A Bit of Closure

"After our UI developer left," Danny writes, "I was tasked with figuring out why one of our admin pages would get stuck in the middle of the loading screen." [expand full text]
« PrevPage 1 | Page 2Next »

Re: A Bit of Closure

2008-07-28 08:08 • by Qvasi (unregistered)
What The Failure does that code do?

Oh and second!!!1|!!!11 ;P (Bugger could'v been fR1st) ehehe

Re: A Bit of Closure

2008-07-28 08:09 • by java.lang.Chris;
No, it wasn't your UI developer who left, it was some incompetent twit that had been let loose on your UI code who left.

Re: A Bit of Closure

2008-07-28 08:10 • by Anonymous Cow-Herd (unregistered)
postComment( postComment( postComment( postComment( postComment("WTF",0),0),FILE_NOT_FOUND),1),0);

First, since I deny the existence of the above comments.

Re: A Bit of Closure

2008-07-28 08:24 • by Volmarias
Oh god, I'm getting flashbacks of LISP...

Re: A Bit of Closure

2008-07-28 08:27 • by fruey
Whenever you see code like that, it screams out for some kind of loop.

Maybe he optimized with -funrolloops

Re: A Bit of Closure

2008-07-28 08:41 • by Spellman (unregistered)
208621 in reply to 208618
Volmarias:
Oh god, I'm getting flashbacks of LISP...

So you're writing a macro and casting your spells then?

Re: A Bit of Closure

2008-07-28 08:47 • by Azeroth (unregistered)
Thank god for anonymous functions - imagine if one would have to think names for god knows how many functions to achieve the same functionality.
But since I'm not familiar with Scheme, I have no idea what the wtf is.

Re: A Bit of Closure

2008-07-28 08:53 • by Zecc
208623 in reply to 208622
Azeroth:
Thank god for anonymous functions - imagine if one would have to think names for god knows how many functions to achieve the same functionality.
loadingStage1, loadingStage2, ...
It would look way better to have the code spread out in a sequence than to have it nested this way.
So, I'm assuming you're being ironic.

Re: A Bit of Closure

2008-07-28 08:54 • by ex-Pizza delivery man (unregistered)
I not that familiar with the language but this code actually seems to execute backwards starting from the bottom and working upwards ...WTF?

Re: A Bit of Closure

2008-07-28 09:15 • by amischiefr
208634 in reply to 208618
Volmarias:
Oh god, I'm getting flashbacks of LISP...


If you wrote LISP code like this.. you're the WTF. The beauty of LISP and SCHEME is that while what is going on behind the scenes may look like this, your code doesn't have to.

Re: A Bit of Closure

2008-07-28 09:25 • by Lisper (unregistered)
208635 in reply to 208618
but this would be the code generated by macroexpand.

Re: A Bit of Closure

2008-07-28 09:27 • by Lisper (unregistered)
(forgot) and we would NEVER put a closing parenthesis on its own line.

Re: A Bit of Closure

2008-07-28 09:28 • by Waffle (unregistered)
208638 in reply to 208635





Looks

perfectly

fine

to

me

Re: A Bit of Closure

2008-07-28 09:32 • by dkf
Twenty-two levels of nesting (plus the usual noise around this). That's about twenty too many for this code to be easily understandable. And a net effect of the code "executing in reverse" too. At least we know why the author left: he was faced with maintaining this monstrosity.

Re: A Bit of Closure

2008-07-28 09:44 • by durnurd
208641 in reply to 208638
In this case,





me

to

fine

perfectly

Looks

Re: A Bit of Closure

2008-07-28 10:39 • by danixdefcon5
I think I get it ... this code was made in the USSR!

Because in Soviet Russia, code executes YOU!!!!

Re: A Bit of Closure

2008-07-28 11:26 • by I walked the dinosaur (unregistered)
208666 in reply to 208655
danixdefcon5:
I think I get it ... this code was made in the USSR!

Because in Soviet Russia, code executes YOU!!!!


...That's what she said.....

Re: A Bit of Closure

2008-07-28 11:37 • by GetAway (unregistered)
208668 in reply to 208638
Waffle:





Looks

perfectly

fine

to

me
I agree! Lol

Re: A Bit of Closure

2008-07-28 11:55 • by pfm (unregistered)
208676 in reply to 208639
Reverse in code executing wrong with what is?

Re: A Bit of Closure

2008-07-28 12:01 • by Anonymous Cow-Herd (unregistered)
208678 in reply to 208676
pfm:
Reverse in code executing wrong with what is?


outwards." and the starts the wrong "It's only when code in middle executes

Re: A Bit of Closure

2008-07-28 12:06 • by danixdefcon5
208680 in reply to 208676
pfm:
Reverse in code executing wrong with what is?
Yoda programming what it is!
Too much have I said, my young padawan!

Re: A Bit of Closure

2008-07-28 12:26 • by SomeCoder (unregistered)
208686 in reply to 208680
danixdefcon5:
pfm:
Reverse in code executing wrong with what is?
Yoda programming what it is!
Too much have I said, my young padawan!


I want to write a YODA programming language compiler. Somebody come up with what YODA can stand for for the language :P

Re: A Bit of Closure

2008-07-28 12:35 • by sn (unregistered)

PopupDisablePanel.setPopupText(Loading Config Data...");
^ syntax error ;-P

Re: A Bit of Closure

2008-07-28 12:37 • by fruey
208689 in reply to 208686
SomeCoder:

I want to write a YODA programming language compiler. Somebody come up with what YODA can stand for for the language :P


Yet time, waste Of Development Another

Re: A Bit of Closure

2008-07-28 13:07 • by jgreen
Ye Olde Development Anachronism
YODA Original Diagnostic Application
Your Object Developing Appliance
Yearly Optically Designing Applications

Re: A Bit of Closure

2008-07-28 13:35 • by oncogenesis (unregistered)
208701 in reply to 208618
As the cliche says, you can write bad code in any language. Also, any sufficiently complicated C or Fortran (or C# per this example) program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp.

Re: A Bit of Closure

2008-07-28 13:36 • by Madman1969 (unregistered)
208702 in reply to 208639
Kill it with fire !

Re: A Bit of Closure

2008-07-28 14:24 • by Vlad Patryshev (unregistered)
A neat way to define a state machine.
What we are having here is not a closure, but a continuation.
I am sure it can be done easier using, say, some kind of combinator.

Re: A Bit of Closure

2008-07-28 14:28 • by Vlad Patryshev (unregistered)
208708 in reply to 208689
Forth called is language the

3 2 + .

Re: A Bit of Closure

2008-07-28 14:40 • by AdT (unregistered)

eventManager.addDelegate(ARTICLE_COMPLETE,
d1 = new FunctionDelegate(
function() {
if (article.type == MFD)
mfd = 1, eventManager.addDelegate(ARTICLE_COMPLETE,
d2 = new FunctionDelegate(
function() {
if (article.type == SOD)
{
say("Finally, a CodeSOD again after " + mfd
+ " MFDs.");
eventManager.addDelegate(ARTICLE_COMPLETE, d1);
}
else
article.type == MFD && ++mfd,
eventManager.addDelegate(ARTICLE_COMPLETE, d2);
}
)
);
else
eventManager.addDelegate(ARTICLE_COMPLETE, d1);
}
)
);

Re: A Bit of Closure

2008-07-28 15:45 • by zoips (unregistered)
208715 in reply to 208701
oncogenesis:
As the cliche says, you can write bad code in any language. Also, any sufficiently complicated C or Fortran (or C# per this example) program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp.


Looks more like Javascript to me, seeing as C# does not have a function keyword.

Re: A Bit of Closure

2008-07-28 16:28 • by pfm (unregistered)
YODA:

Another Development Of YODA

Re: A Bit of Closure

2008-07-28 16:38 • by wesley0042 (unregistered)

This code makes me sad };
};
};
};
};

Re: A Bit of Closure

2008-07-28 17:34 • by PileOfMush
I have some 7 year old failcode that looks just like that, but in VB. Ah, the memories.


If Not PageIsSepSheet And Not PageIsBlank Then
If Not PageIsMissingDVF Then
DvfIsGood = ReadDVF(TMID)
If DvfIsGood Then
If Not PageIsMissingTIF Then
If Not PageIsRemoved Then
If PageIsGood Then
'WRITE LOG, IPT, & DOC.LST '''''
WriteToLists ' <- You waded thru all that to get to this
''''''''''''''''''''''''''''''''
If Verify(1) = "49" Then 'WRITE WARNING TO ERROR LOG
ErrorMsg = ErrorMsg & "Page with status 49 ('SCANNED OK') imported." & vbNewLine
AppendLog ErrorLogPath, ErrorLogName, ErrorMsg
End If
Else 'WRITE TO ERROR LOG / PageIsGood = False
ErrorMsg = ErrorMsg & "Page status " & Verify(1) & " not processed." & vbNewLine
AppendLog ErrorLogPath, ErrorLogName, ErrorMsg
End If
Else 'WRITE TO ERROR LOG / PageIsRemoved = True
ErrorMsg = ErrorMsg & "Page marked as REMOVED." & vbNewLine
AppendLog ErrorLogPath, ErrorLogName, ErrorMsg
End If
Else 'WRITE TO ERROR LOG / PageIsMissingTIF = True
ErrorMsg = ErrorMsg & "Page was missing TIF file." & vbNewLine
AppendLog ErrorLogPath, ErrorLogName, ErrorMsg
End If
Else 'WRITE TO ERROR LOG / DVFIsGood = False
ErrorMsg = ErrorMsg & "Could not interpret DVF file." & vbNewLine
AppendLog ErrorLogPath, ErrorLogName, ErrorMsg
End If
Else 'WRITE TO ERROR LOG / PageIsMissingDVF = True
If PageIsRemoved Then
ErrorMsg = ErrorMsg & "Page marked as REMOVED." & vbNewLine
Else
ErrorMsg = ErrorMsg & "Could not find DVF file." & vbNewLine
End If
AppendLog ErrorLogPath, ErrorLogName, ErrorMsg
End If
Else 'Do not log SEPSHEETs
End If



*EDIT* BTW, I've been told it's still running. =-)

Re: A Bit of Closure

2008-07-28 17:51 • by dkf
208728 in reply to 208723
PileOfMush:
I have some 7 year old failcode that looks just like that, but in VB.
Not quite. The code you wrote is just one of the horrible things you get when you insist on not exiting a function early. That sort of thing is entirely possible in many languages. The article's problem seems to be that it's nesting closures inside closures inside closures inside... That's far messier, since it's got lots of added scope management thrown in for fun, making the code just that bit less performant or clear. At a technical level, it's a true WTF!? because it's not even wrong; the code's correct but still makes the person who encounters it in the wild want to take up a rusty spoon and do some damage to the perpetrator.

The only thing missing really is the idiotic catch phrase for us to refer to this WTF by. Clbuttic indeedy.

Re: A Bit of Closure

2008-07-28 17:53 • by Alex Papadimoluppoganeouficamulolulis (unregistered)
208729 in reply to 208723
PileOfMush is hereby banned from The Daily WTF. I'm sorry for that, everyone.

I like rust.

2008-07-28 17:56 • by Salad Fingers (unregistered)
208730 in reply to 208728
dkf:
but still makes the person who encounters it in the wild want to take up a rusty spoon and do some damage to the perpetrator.
I'm here to enquire about your spoons...

Re: A Bit of Closure

2008-07-28 18:22 • by PileOfMush
208732 in reply to 208729
Alex Papadimoluppoganeouficamulolulis:
PileOfMush is hereby banned from The Daily WTF. I'm sorry for that, everyone.


Oh man! And I didn't even include the Do Loop and the broader If that all of this crap was nested in.

Re: A Bit of Closure

2008-07-28 21:25 • by Anonymouse (unregistered)
If LOAD_COMPLETE is a boolean value, it seems that it's assigned to FILE_NOT_FOUND.

Re: I like rust.

2008-07-28 23:46 • by Mr.'; Drop Database -- (unregistered)
208748 in reply to 208730
Salad Fingers:
dkf:
but still makes the person who encounters it in the wild want to take up a rusty spoon and do some damage to the perpetrator.
I'm here to enquire about your spoons...
Unfortunately, it's too big.

Re: A Bit of Closure

2008-07-29 00:34 • by Tourist (unregistered)
If you print that out and hang it on a wall it looks like a babe.. seriously!

Just needs a defmacro

2008-07-29 01:02 • by chkno (unregistered)
(defmacro delegate-chain (event actions)

(when (consp actions)
`(add-delegate ,event (lambda ()
(delegate-chain ,event ,(rest actions))
,(first actions)))))

(delegate-chain 'load-complete
((load-existing-user-mac-data)
(load-status-group-data)
...))

Re: I like rust.

2008-07-29 01:48 • by GermanGirl (unregistered)
208759 in reply to 208730
Salad Fingers:
dkf:
but still makes the person who encounters it in the wild want to take up a rusty spoon and do some damage to the perpetrator.
I'm here to enquire about your spoons...


There is no spoon...

Re: A Bit of Closure

2008-07-29 02:25 • by Anonymous (unregistered)
I count 6 levels of nesting, not 22. The code isn't bad after some pretty-printing is applied. It looks like someone who has but one or two tendons left to chew before escaping from the mental trap that is Java.

Re: A Bit of Closure

2008-07-29 04:46 • by Mouse (unregistered)
208766 in reply to 208761
Anonymous:
I count 6 levels of nesting, not 22. The code isn't bad after some pretty-printing is applied. It looks like someone who has but one or two tendons left to chew before escaping from the mental trap that is Java.



The casing and the use of "function()" doesn't suggest that - your post suggests you dislike java and don't quite understand it.

Re: I like rust.

2008-07-29 06:50 • by unknown (unregistered)
208769 in reply to 208759
GermanGirl:
Salad Fingers:
dkf:
but still makes the person who encounters it in the wild want to take up a rusty spoon and do some damage to the perpetrator.
I'm here to enquire about your spoons...


There is no spoon...


How about a fork?

Anyway, at least he did the indentation...

Re: A Bit of Closure

2008-07-29 09:09 • by leppie (unregistered)
208783 in reply to 208639
dkf:
Twenty-two levels of nesting (plus the usual noise around this). That's about twenty too many for this code to be easily understandable. And a net effect of the code "executing in reverse" too. At least we know why the author left: he was faced with maintaining this monstrosity.


Pity you can only count to 6, if you knew more, you would have realized 7 comes after 6 and not 22. DOH!

Re: A Bit of Closure

2008-07-29 10:42 • by 35% Genius (unregistered)
208821 in reply to 208783
leppie:
dkf:
Twenty-two levels of nesting (plus the usual noise around this). That's about twenty too many for this code to be easily understandable. And a net effect of the code "executing in reverse" too. At least we know why the author left: he was faced with maintaining this monstrosity.


Pity you can only count to 6, if you knew more, you would have realized 7 comes after 6 and not 22. DOH!


The code until PopupDisablePanel.hidePopup() has 28 opening parenthesis and braces and 6 closing parenthesis. That is 22 levels of nesting. However, you did prove that there are people who can't count.

Re: A Bit of Closure

2008-07-29 11:22 • by Asd (unregistered)
There is no need to reach for your macro hammer just for this case.

function addLoadCompleteDelegates(eventManager, delegates) {
eventManager.addDelegate(LOAD_COMPLETE,
new FunctionDelegate(
function() {
addLoadCompleteDelegates(eventManager, tail(delegates));
head(delegates)();
}
}

addLoadCompleteDelegates(eventManager,
[
loadExistingUserMacData,
loadStatusGroupData,
function () { if (macAddress.length > 0) {
PopupDisablePanel.setPopupText(Loading Config Data...");
loadConfigData();
}
else {
checkCreateConfigValid();
PopupDisablePanel.setPopupText("Loading User Data...");
loadUserData();
}
},

...
]);


Assuming I am understanding this right of course.

Re: A Bit of Closure

2008-07-29 11:44 • by James (unregistered)
Wow, I can't believe it sat this long and nobody actually *got* what the code does. The nesting is a function that defines a function that defines a function... etc., not a for-loop or anything else like that. The whole point is that it's defining event handlers for asynchronous (probably UI) code. It's not readable, so that's a WTF, but it's a reasonably elegant way to handwave out some stuff you don't want to have to break into ~6-8 separate named functions. If you tried to write it procedurally, because of the multithreading issues it would have to be something like

Log("Doing A");

start(DoA());
waitfor(DoAComplete);

Log("Doing B");
start(DoB());
waitfor(DoBComplete);

...


which would have to run in a thread separate from the UI thread (which could have run the messy-but-functional code from the original post just fine).

While the above is more compact and readable, the "Best Practice" is to use delegates instead, so that the OS message pump can handle firing off code instead of leaving the. It's kind-of-mostly more elegant to simply register event handlers than to do polling/monitoring manually. They just didn't structure the event handler definitions as well as they might have.
« PrevPage 1 | Page 2Next »

Add Comment