Comment On Ancestors

It’s important to have an understanding of genealogy; it can give you a connection to history. Even in code, we find a need to connect with our parents and their ancestors. [expand full text]
« PrevPage 1Next »

Re: Ancestors

2012-12-17 06:32 • by ParkinT
Would you call this style of coding, Childish?

Re: Ancestors

2012-12-17 08:02 • by Gaza Rullz (unregistered)
Coding : You're doing it wrong.

Re: Ancestors

2012-12-17 08:07 • by macmac (unregistered)
Im guilty of having done similar stuff in Javascript. Namely in custom userjs files for opera (greasemonkey for the firefox users), meaning you don't have control over the layout of the original html file you are customizing.
You make a userJS file that adds a button that should hide a certain element that has no id and no class defined. The best you can do is find another nearby element that DOES have an ID and do the parent/children thing to get to the right one.

Re: Ancestors

2012-12-17 08:08 • by Warren (unregistered)
OK, I see the WTFs. The first one doesn't do anything with the result obtained, and the second one could be optimised to one line rather than using the variable thechkbox.

Re: Ancestors

2012-12-17 08:15 • by Honnza (unregistered)
if(hasrating){
$(currRating[0])
.closest("li")
.find("input[type=checkbox]")
.attr("disabled",false)
}

Re: Ancestors

2012-12-17 08:21 • by The Daily WTF (unregistered)
Is it really a WTF? I'm not so sure, and I'm The Daily WTF.

Re: Ancestors

2012-12-17 08:28 • by macmac (unregistered)
397318 in reply to 397316
Honnza:
if(hasrating){
$(currRating[0])
.closest("li")
.find("input[type=checkbox]")
.attr("disabled",false)
}


And now without jquery ;)

Re: Ancestors

2012-12-17 08:33 • by Bruce W (unregistered)
It takes a unique type of inbreeding to code like that.

Re: Ancestors

2012-12-17 08:45 • by imgx64 (unregistered)
Looks like the elements in the second snippet are second cousins thrice removed.

Re: Ancestors

2012-12-17 08:58 • by Techpaul (unregistered)
thetxtbox = comment[10].parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.children(Frist).children(Frist0).children(second)

Re: Ancestors

2012-12-17 09:03 • by jamiec
397323 in reply to 397316
Honnza:
if(hasrating){
$(currRating[0])
.closest("li")
.find("input[type=checkbox]")
.attr("disabled",false)
}


Needs more jQuery!

Re: Ancestors

2012-12-17 09:09 • by ObiWayneKenobi
I've done something like this only once and it was for a weird bit of JavaScript in a pre-4.0 ASP.NET WebForms (so the weird-ass control IDs) where using a jQuery selector, for whatever reason, just wouldn't find the element. The only choice was do control.parent().parent().parent().something()

Re: Ancestors

2012-12-17 09:12 • by LoremIpsumDolorSitAmet

Re: Ancestors

2012-12-17 09:13 • by Xarthaneon the Unclear (unregistered)
397326 in reply to 397319
Bruce W:
It takes a unique type of inbreeding to code like that.


Appalachian Application Development - the next big breakthrough in computer science!

It's when genetic algorithms go wrong.

Re: Ancestors

2012-12-17 10:02 • by $$ERR:get_name_fail (unregistered)
I also wrote code like that a few times when writing bookmarklets.

When you are operating on HTML code by someone else and perform operations on it the original author never intended, you often have no other choice.

Re: Ancestors

2012-12-17 10:11 • by TheSHEEEP (unregistered)
What language is the first one?

First I thought it was ActionScript, but that one doesn't have the MouseEventArgs type and "object" type is capitalized.
Java afaik also has no MouseEventArgs and Object instead of object.
.NET has MouseEventArgs, but also Object.

Seriously, what is this?

Re: Ancestors

2012-12-17 10:17 • by Remy Porter
You always have another choice.

x = some.really.long.query.string()
is always bad.

Even if you're not using jQuery, you are doing queries against the DOM. You should encapsulate those queries, even not for reusability, but for readability.

Re: Ancestors

2012-12-17 10:36 • by Ben Jammin (unregistered)
Several people are saying that when one does not have control over the html, that this approach is sometimes necessary. Wouldn't you want something a little more robust; Especially, if you did not have control of the html? All they have to do is mess with their design slightly, and they probably wouldn't even think to notify you that they added pretty boxes around their pictures, and the code is broken.

Re: Ancestors

2012-12-17 11:02 • by Nick (unregistered)
397332 in reply to 397329
"What language is the first one?"

Probably C# (.NET), it uses object as the default when creating an event handler.

Most of the common types have lower case keywords you can use instead for convenience.

e.g.
System.Int32 -> int
System.Int64 -> long
System.Boolean -> bool
System.Object -> object
System.String -> string

Re: Ancestors

2012-12-17 11:04 • by jamiec
397333 in reply to 397329
TheSHEEEP:
What language is the first one?


c#.

It has syntactical sugar for common .NET types

object = Object
int = Int32
string = String

etc.

Re: Ancestors

2012-12-17 11:21 • by Omego2K (unregistered)
(sender as PictureBox).Parent.Parent.Parent.Parent.Parent.GetType();

Lets ignore the fact that the code is bad. What is this line supposed to accomplish?

Re: Ancestors

2012-12-17 11:28 • by WPFWTF (unregistered)
You have to do this if you're animating subproperties on a part of a template in WPF.

Of course, it's all right there together, and if you alter the template you'll have to alter the binding anyway.

but it's more like.

control.template =
some stuff
-> new textbox() { text = bind to control's date's yadda yadda field }

Re: Ancestors

2012-12-17 11:48 • by Brent (unregistered)
Sadly, I cam empathize with this type of code more than other examples of bad code on this site. The best way I know to find a specific parent on a form like that is with recursion: http://www.kruegerwebdesign.com/blog/find-first-parent-control-of-a-specific-type-using-recursion

Re: Ancestors

2012-12-17 11:58 • by jamiec
397340 in reply to 397334
Omego2K:
(sender as PictureBox).Parent.Parent.Parent.Parent.Parent.GetType();

Lets ignore the fact that the code is bad. What is this line supposed to accomplish?


Allows us to take the piss out of it for one thing.

It does nothing, the result of GetType() is thrown away.

Re: Ancestors

2012-12-17 12:04 • by Jamie (unregistered)
397341 in reply to 397340
jamiec:

Allows us to take the piss out of it for one thing.

It does nothing, the result of GetType() is thrown away.


They also shouldn't be doing (sender as PictureBox) either. If sender is always a PicutureBox, do a direct cast. If it might not be a PictureBox, you've got a situation in which(sender as PictureBox) returns null.

Re: Ancestors

2012-12-17 12:31 • by PiisAWheeL
397344 in reply to 397341
I think that code might be its own grandpa!

Re: Ancestors

2012-12-17 12:41 • by the beholder (unregistered)
397345 in reply to 397340
jamiec:
Omego2K:
(sender as PictureBox).Parent.Parent.Parent.Parent.Parent.GetType();

Lets ignore the fact that the code is bad. What is this line supposed to accomplish?


Allows us to take the piss out of it for one thing.

It does nothing, the result of GetType() is thrown away.
...unless GetType() has been overridden to do something else other than returning the object's type.

Naturally that would fall into another category of WTF, probably an even worse one. But hey, at least it would mean that line is not useless.

Re: Ancestors

2012-12-17 13:23 • by Gurth
That reminds me of AppleScript Studio. At least with these you can be glad you don't have to specify the parent elements yourself — all the way up the hierarchy
set isChecked to value of checkbox "foo" of box "baz" of tab "bar" of tab view "foobar" of window "whatever"

Re: Ancestors

2012-12-17 13:33 • by jay (unregistered)
Need it be stated: What's wrong with this code is not that it chases up and down the parent/child tree, but that it builds in the assumption that it is exactly this number of generations. Yes, I've had times when I've had to do something like this. But what I generally do is put a loop that takes parents until it finds a node of a certain type or that has a certain class, rather than hard-coding the number of generations. Then, (a) if someone changes the HTML to add or remove a level of tags, my code doesn't mysteriously break, and (b) someone reading it can see, "Oh, he's getting the enclosing li tag" or whatever, rather than, "Eight levels up? What's eight levels up?" Especially if something went wrong after another programmer added or removed a level of tags, and know someone is trying to figure out where it's supposed to go.

Re: Ancestors

2012-12-17 13:58 • by Daniel (unregistered)
There's nothing wrong with this code. Nothing at all. It's a perfectly valid form of adressing a branch within a known inner scope tree encapsulated in an unknown outer scope.

In fact, it's the right way to do just that. It's called reusability. This was most likely a plugin of sorts.

The fact that so many of you are dissing it just shows how many chimpanzee wannabe programmers are reading tdwtf.


Re: Ancestors

2012-12-17 14:00 • by RichP
It's the (not so) famous third-cousin design pattern!

Re: Ancestors

2012-12-17 15:22 • by dahvyd (unregistered)
397354 in reply to 397345
Can't override Object.GetType() as it's sealed.

Re: Ancestors

2012-12-17 16:58 • by Fox Mulder (unregistered)
397358 in reply to 397344
PiisAWheeL:
I think that code might be its own grandpa!
And if your aunt was your sister, you'd be your own grandpa.....

Re: Ancestors

2012-12-17 18:45 • by Robert Heinlein (unregistered)
That code knows where it came from. But all you zombies, where did you come from?

Re: Ancestors

2012-12-17 23:14 • by Coyne
This is positively scary. Talk about your fragile code: "I refactored routine someDumbThing() into two parts, one of which calls the other, and now nothing works. WTF!"

The only thing I can think of that would be worse would be enhanced reflection along this line:


((Victim) caller().caller().caller().getObjectVariable("victimVar")).setStatus("whatever");


Oh, BTW: I vote "Ancestors" as WTF of the year.

Re: Ancestors

2012-12-18 04:06 • by QJo (unregistered)
397368 in reply to 397349
jay:
Need it be stated: What's wrong with this code is not that it chases up and down the parent/child tree, but that it builds in the assumption that it is exactly this number of generations. Yes, I've had times when I've had to do something like this. But what I generally do is put a loop that takes parents until it finds a node of a certain type or that has a certain class, rather than hard-coding the number of generations. Then, (a) if someone changes the HTML to add or remove a level of tags, my code doesn't mysteriously break, and (b) someone reading it can see, "Oh, he's getting the enclosing li tag" or whatever, rather than, "Eight levels up? What's eight levels up?" Especially if something went wrong after another programmer added or removed a level of tags, and know someone is trying to figure out where it's supposed to go.

+1 FTW

Re: Ancestors

2012-12-18 05:30 • by Meep (unregistered)
397369 in reply to 397350
Daniel:
There's nothing wrong with this code. Nothing at all. It's a perfectly valid form of adressing a branch within a known inner scope tree encapsulated in an unknown outer scope.

In fact, it's the right way to do just that. It's called reusability. This was most likely a plugin of sorts.

The fact that so many of you are dissing it just shows how many chimpanzee wannabe programmers are reading tdwtf.


Except that it's not at all reusable. The reusable version would be something like:


var obj = sender;
while(!(obj instanceof Form)) {
obj = obj.parent;
}


Or looking it up using DOM or using a closure when constructing the handler or any of a dozen techniques to pass the information cleanly and reliably.

If you alter your GUI slightly and have to wade through a slew of bug reports, you're doing it wrong.

Re: Ancestors

2012-12-18 06:37 • by jamiec
397372 in reply to 397350
Daniel:
There's nothing wrong with this code. Nothing at all. It's a perfectly valid form of adressing a branch within a known inner scope tree encapsulated in an unknown outer scope.

In fact, it's the right way to do just that. It's called reusability. This was most likely a plugin of sorts.

The fact that so many of you are dissing it just shows how many chimpanzee wannabe programmers are reading tdwtf.




Awful troll, must try harder. See me after class.

Re: Ancestors

2012-12-18 07:27 • by faoileag
397373 in reply to 397350
Daniel:
There's nothing wrong with this code. Nothing at all.[...] The fact that so many of you are dissing it just shows how many chimpanzee wannabe programmers are reading tdwtf.

What I like about this posting is the fact that it was posted directly after jay's good post on how to solve the problem in a better way :-)

Re: Ancestors

2012-12-18 11:31 • by Herr Otto Flick (unregistered)
397428 in reply to 397349
jay:
Need it be stated: What's wrong with this code is not that it chases up and down the parent/child tree, but that it builds in the assumption that it is exactly this number of generations. Yes, I've had times when I've had to do something like this. But what I generally do is put a loop that takes parents until it finds a node of a certain type or that has a certain class, rather than hard-coding the number of generations. Then, (a) if someone changes the HTML to add or remove a level of tags, my code doesn't mysteriously break, and (b) someone reading it can see, "Oh, he's getting the enclosing li tag" or whatever, rather than, "Eight levels up? What's eight levels up?" Especially if something went wrong after another programmer added or removed a level of tags, and know someone is trying to figure out where it's supposed to go.


Or I could just wait for the offending twatknuckle to check in their breaking change, wait for Jenkins to fuck them up the arse, and then force them to unbreak my code.

"I know, lets make it much slower just in case someone does something stupid in the future"

Re: Ancestors

2012-12-18 15:22 • by Worf (unregistered)
397466 in reply to 397347
Gurth:
That reminds me of AppleScript Studio. At least with these you can be glad you don't have to specify the parent elements yourself — all the way up the hierarchy
set isChecked to value of checkbox "foo" of box "baz" of tab "bar" of tab view "foobar" of window "whatever"


Though, if you specify up the hierarchy, it's a lot easier to catch when someone movies something around - you'll get an error. If someone happened to move the control around, the unspecified ones can still work in very odd ways leading to very hard to troubleshoot bugs.

Of course, it also means the article version can have controls moved around without (apparently) breaking the code.

Re: Ancestors

2012-12-20 11:59 • by B. D. Johnson (unregistered)
397735 in reply to 397326
So recursive genetic algorithms?

Re: Ancestors

2012-12-24 23:40 • by panzi (unregistered)
I don't see how that JavaScript code ever worked. children is not a function but an array (like object - a HTMLCollection) so the parentheses should be replaces with square brackets.

Re: Ancestors

2012-12-27 10:20 • by toshir0
398029 in reply to 397937
panzi:
I don't see how that JavaScript code ever worked. children is not a function but an array (like object - a HTMLCollection) so the parentheses should be replaces with square brackets.
Oh. Must be a scheme, so.
« PrevPage 1Next »

Add Comment