Comment On Type Safety Considered Harmful

Submitted anonymously: "I found this in a data access layer for an application testing framework. The object has fields that are obviously meant to contain integers, doubles, and DateTime objects, but they are all strings and this is the only constructor: [expand full text]
« PrevPage 1 | Page 2Next »

Re: Type Safety Considered Harmful

2008-03-19 08:03 • by Anon (unregistered)
The goggles...they do nothing!

Re: Type Safety Considered Harmful

2008-03-19 08:03 • by Chishm (unregistered)
The for-case paradigm has re-appeared, worse than ever

Re: Type Safety Considered Harmful

2008-03-19 08:10 • by captain obvious (unregistered)
How does it get through to someone's mind that this is okay? This is wrong on so many levels, for example:

deduction_breakfast / deduktion_lunch / deduktion_dinner
Note that only once is "deduction" spelt correctly... how the f?

Re: Type Safety Considered Harmful

2008-03-19 08:22 • by Rob (unregistered)
I like the absence of any checks against null - not only the elements, but the array itself as well. Just one occurrance and the entire constructor fails.

Re: Type Safety Considered Harmful

2008-03-19 08:44 • by NaN
I'm guessing the class has a list of all the instances (lineItems) but this list is cleared each time a new LineItem is instantiation, so it contains a list of only the last lineItem:

In the constructor: lineItems = new List(0);

Re: Type Safety Considered Harmful

2008-03-19 08:45 • by McWyrm (unregistered)
184565 in reply to 184554
Chishm:
The for-case paradigm has re-appeared, worse than ever


This strikes me as a particularly elegant implementation of the paradigm, though. Almost as if the programmer's technique has been polished over the course of a lengthy practice.

wtf?!

Re: Type Safety Considered Harmful

2008-03-19 08:45 • by TGV
Why do I get the feeling that the person who wrote this has not idea what a database is? Or what you can do with SQL? Let me guess: all (s)he knows is "SELECT * FROM %s" and that is read into this horrible data structure...

Re: Type Safety Considered Harmful

2008-03-19 08:48 • by Marcel Veldhuizen (unregistered)
Apart from the use of strings and the foreach/switch instead of a normal for-loop, I don't find this code all that strange. Sure, there are other ways to do this, but I don't consider a strongly typed dataset a viable alternative for value objects.

I wonder how the field data ends up in the object array initially though, that's something to be avoided. Straight from DataReader or DataRow to value object would make more sense.

Re: Type Safety Considered Harmful

2008-03-19 08:48 • by NaN
184569 in reply to 184564
As for what and why - I'm guessing these are the expected results from the test, and they compare the objects toString method with these strings, rather than a full object.

Re: Type Safety Considered Harmful

2008-03-19 08:51 • by Codepope (unregistered)
I like the use of .ToString() rather than .toString()...

It's subtle but full aroma code smell... :)

Re: Type Safety Considered Harmful

2008-03-19 08:53 • by NaN
184572 in reply to 184569
For-case VS Unrolled loop VS Normal Loop:

An Unrolled loop would use a few lines less code.

The For-Case only loops while there are objects, so as long as no one added a null object, it would be fine. Also, if there are only 10 objects, it would stop at 10 (well, 9) whereas an unrolled loop would need checking.

A normal loop would be able to loop through everything AND stop at the last one should that happen early.

Normal loop:

String array of all the variables.
Loop, use indexes to assign to array of all variables.

Is it strange I don't think this isn't too bad?

Re: Type Safety Considered Harmful

2008-03-19 08:58 • by Frodo (unregistered)
184573 in reply to 184556
captain obvious:
How does it get through to someone's mind that this is okay? This is wrong on so many levels, for example:

deduction_breakfast / deduktion_lunch / deduktion_dinner
Note that only once is "deduction" spelt correctly... how the f?


Not only that - if the company hires some Hobbits and has to add 'deduction_second_breakfast' to the line item, it will completely break the code.

Re: Type Safety Considered Harmful

2008-03-19 08:59 • by Someone You Know
184574 in reply to 184570
Codepope:
I like the use of .ToString() rather than .toString()...

It's subtle but full aroma code smell... :)


For those of us who don't know C#: what is the difference?

Re: Type Safety Considered Harmful

2008-03-19 09:00 • by benryves (unregistered)
184575 in reply to 184570
Codepope:
I like the use of .ToString() rather than .toString()...

It's subtle but full aroma code smell... :)
This is C#, hence Object.ToString() rather than object.toString().

Re: Type Safety Considered Harmful

2008-03-19 09:01 • by NaN
184576 in reply to 184574
Someone You Know:
Codepope:
I like the use of .ToString() rather than .toString()...

It's subtle but full aroma code smell... :)


For those of us who don't know C#: what is the difference?


I think one works and one doesn't. (Guessing)

Re: Type Safety Considered Harmful

2008-03-19 09:01 • by Patryk Zawadzki
It's simple guys, he must have deduked it was a good idea.

BTW, Deduktion == you are no longer a duke, right?

Re: Type Safety Considered Harmful

2008-03-19 09:02 • by justAnotherCoder (unregistered)
184578 in reply to 184574
Someone You Know:
Codepope:
I like the use of .ToString() rather than .toString()...

It's subtle but full aroma code smell... :)


For those of us who don't know C#: what is the difference?


Methods begin with upper case in C#'s naming convention. So ToString() is the correct one, defined in Object.

Re: Type Safety Considered Harmful

2008-03-19 09:07 • by ath (unregistered)
It's not that WTF:y. I can't really think of any easier way of implementing the same functionality. Sure, implementing similar more logical functionality yes, but the same?

Notice how:
o.LineItem({"Rent", "Cash"});
will only set the parameters category, and payment method which is perfect for the lazy programmer who doesnt like to create new objects.

So:
o.LineItem({"Rent", "Cash", "Today", "Two millions"});
o.LineItem({"Prince of Nigeria help", "Credit Card"});
Will send two millions today to both my landlord and the prince of nigeria, using different payment methods. Excellent!

Re: Type Safety Considered Harmful

2008-03-19 09:18 • by Dave (unregistered)
184580 in reply to 184553
Anon:
The goggles...they do nothing!


Why do people consistently misquote that line?

Re: Type Safety Considered Harmful

2008-03-19 09:20 • by Ben (unregistered)
184581 in reply to 184570
That's not a code-smell. It's just C# where the convention is MethodsLikeThis() (as in Pascal) and not methodsLikeThis() (as in Java).

Re: Type Safety Considered Harmful

2008-03-19 09:20 • by Who Cares (unregistered)
How about using a default case with ToString and only cases IF there is something else? In that case you could strip the switch altogether until somebody really adds something else than a string there.

After all... duplicate code smells

Re: Type Safety Considered Harmful

2008-03-19 09:25 • by Leo Davidson (unregistered)
Looks like a Perl programmer tried to write C#. Yuck.

Re: Type Safety Considered Harmful

2008-03-19 09:35 • by ChipK (unregistered)
Wow, cool, I am going to cut-n-paste this code into the app I am maintaining... it will blend right in!

Re: Type Safety Considered Harmful

2008-03-19 09:49 • by M L (unregistered)
184594 in reply to 184570
Codepope:
I like the use of .ToString() rather than .toString()...

It's subtle but full aroma code smell... :)


That's just the subtle smell of C# and not the Java you thought you were smelling. It is normal C# code style to have capitalized method names. (Also note the foreach loop is not Java's either.)

Re: Type Safety Considered Harmful

2008-03-19 09:59 • by John the incredible (unregistered)
Seems ungraceful. They should have just used nothing but objects for storage and then cast them to strings for output...

Re: Type Safety Considered Harmful

2008-03-19 10:01 • by bbqfrito (unregistered)
184600 in reply to 184592
ChipK:
Wow, cool, I am going to cut-n-paste this code into the app I am maintaining... it will blend right in!


Sorry.

Re: Type Safety Considered Harmful

2008-03-19 10:12 • by Codepope (unregistered)
184608 in reply to 184575
Ah. The just being not Java thing....

Actually, wouldn't it be a good idea for Daily WTF to actually label code segments with what language they are meant to be rather than making the reader infer it from the text.

Re: Type Safety Considered Harmful

2008-03-19 10:16 • by Codepope (unregistered)
184609 in reply to 184594
M L:
(Also note the foreach loop is not Java's either.)


Oh yes; slaps forehead, leaves mark, goes ouch.

Re: Type Safety Considered Harmful

2008-03-19 10:19 • by Pauldy (unregistered)
Not realy much of a WTF esp if this data access layer is meant specifically for display on some type of form only. Of course you would have to know the database layout etc... because if nulls are allowed then I could see this being a wtf.

Re: Type Safety Considered Harmful

2008-03-19 11:03 • by SomeCoder (unregistered)
184635 in reply to 184580
Dave:
Anon:
The goggles...they do nothing!


Why do people consistently misquote that line?



"My eyes... the goggles do nothing!"

Rainier Wolfcastle (The Simpsons)

:)

Re: Type Safety Considered Harmful

2008-03-19 11:07 • by Outlaw Programmer
184638 in reply to 184613
What's the point of using the whole loop-and-switch construct, anyway? All other WTFs aside, couldn't he have done:

category = field[0].ToString();
paymentMethod = field[1].ToString();
...

? (insert check for null if necessary)

Re: Type Safety Considered Harmful

2008-03-19 11:18 • by KenW
184641 in reply to 184580
Dave:
Why do people consistently misquote that line?


Why do pedants who care about stuff like this exist?

Seriously, dude... Get a life! If slight misquoting gets your shorts in a bunch, you need to get laid or something.

Re: Type Safety Considered Harmful

2008-03-19 11:44 • by j0k3r
184654 in reply to 184579
ath:
It's not that WTF:y. I can't really think of any easier way of implementing the same functionality. Sure, implementing similar more logical functionality yes, but the same?

Notice how:
o.LineItem({"Rent", "Cash"});
will only set the parameters category, and payment method which is perfect for the lazy programmer who doesnt like to create new objects.

So:
o.LineItem({"Rent", "Cash", "Today", "Two millions"});
o.LineItem({"Prince of Nigeria help", "Credit Card"});
Will send two millions today to both my landlord and the prince of nigeria, using different payment methods. Excellent!


Oh but it is. If you really want all strings, for whatever reason that may be, you should override your correctly formed dataset.

Re: Type Safety Considered Harmful

2008-03-19 11:58 • by Some Java Guy (unregistered)
So the real WTF is C#.

Is that even a real programming language?

Re: Type Safety Considered Harmful

2008-03-19 12:04 • by NaN
184666 in reply to 184662
Some Java Guy:
So the real WTF is C#.

Is that even a real programming language?


It's A programming language. The REAL WTF is that it's possible you didn't anticipate the small war your going to cause...

Re: Type Safety Considered Harmful

2008-03-19 12:12 • by dkf (unregistered)
184671 in reply to 184638
Outlaw Programmer:
What's the point of using the whole loop-and-switch construct, anyway? All other WTFs aside, couldn't he have done:

category = field[0].ToString();
paymentMethod = field[1].ToString();
It's there to stop the code from charging off the end of the array. Mind you, it's possibly better to do what you're suggesting and wrap it with code to trap a read off the end of the array. Like that it will be much more efficient due to avoiding a counter variable...

Re: Type Safety Considered Harmful

2008-03-19 12:39 • by notJoeKing (unregistered)
184682 in reply to 184662
Some Java Guy:
So the real WTF is C#.

Is that even a real programming language?


Nope, it's just the bastard child of Java and C++... they made it for all the C++ programmers who needed an easier and faster environment to develop in. C# was only created because C++ developers were too Prima Donna to switch to VB.Net... Or maybe they just couldn't learn the syntax ;)

{Putting on flame suit for the prissy C++/C# diciples...)

LOL

Re: Type Safety Considered Harmful

2008-03-19 12:55 • by hikari
184692 in reply to 184666
NaN:

It's A programming language. The REAL WTF is that it's possible you didn't anticipate the small war your going to cause...


Oh I think they knew exactly what they were doing.

Re: Type Safety Considered Harmful

2008-03-19 12:59 • by Al (unregistered)
184696 in reply to 184682
notJoeKing:
C# was only created because C++ developers were too Prima Donna to switch to VB.Net...


VB.Net is the real WTF.

Re: Type Safety Considered Harmful

2008-03-19 13:40 • by Chris M. (unregistered)
184709 in reply to 184682
notJoeKing:
Some Java Guy:
So the real WTF is C#.

Is that even a real programming language?


Nope, it's just the bastard child of Java and C++... they made it for all the C++ programmers who needed an easier and faster environment to develop in. C# was only created because C++ developers were too Prima Donna to switch to VB.Net... Or maybe they just couldn't learn the syntax ;)

{Putting on flame suit for the prissy C++/C# diciples...)

LOL


Well, no. It was actually made for Microsoft managers, who get hives when large numbers of developers or users start using things they can't hold absolute control over, like Java.

Re: Type Safety Considered Harmful

2008-03-19 13:41 • by Roots (unregistered)
184711 in reply to 184564
It's probably an internal list of child lineItems. Nested line items seems plausible I suppose.

Re: Type Safety Considered Harmful

2008-03-19 13:41 • by Chris M. (unregistered)
184712 in reply to 184556
captain obvious:
How does it get through to someone's mind that this is okay? This is wrong on so many levels, for example:

deduction_breakfast / deduktion_lunch / deduktion_dinner
Note that only once is "deduction" spelt correctly... how the f?


Five will get you ten the native language of the original programmer was German.

Re: Type Safety Considered Harmful

2008-03-19 14:12 • by Dan (unregistered)
184723 in reply to 184579
ath:
It's not that WTF:y. I can't really think of any easier way of implementing the same functionality. Sure, implementing similar more logical functionality yes, but the same?


I don't know a lick of C#, but I can't imagine any modern programming language in which the use of magic numbers in a switch-type statement is considered best practices.

Dan.

Re: Type Safety Considered Harmful

2008-03-19 14:26 • by fuzzylollipop (unregistered)
the real WTF is the List(0), which creates the worst case degenerate list behavior possible. It will do memory allocation on almost every add!

Re: Type Safety Considered Harmful

2008-03-19 14:31 • by An apprentice (unregistered)
But it's so intuitive. For example you can write:
deduction_breakfast + deduktion_lunch + deduktion_dinner
and it will compile successfully.

Re: Type Safety Considered Harmful

2008-03-19 14:31 • by Someone You Know
184731 in reply to 184578
justAnotherCoder:
Someone You Know:
Codepope:
I like the use of .ToString() rather than .toString()...

It's subtle but full aroma code smell... :)


For those of us who don't know C#: what is the difference?


Methods begin with upper case in C#'s naming convention. So ToString() is the correct one, defined in Object.


Oh, I see. Codepope made it sound like they both existed, but one of them was a good thing to do and the other not. That would certainly qualify as a WTF to me...

Re: Type Safety Considered Harmful

2008-03-19 14:33 • by nerfer (unregistered)
184733 in reply to 184608
Codepope:
Ah. The just being not Java thing....

Actually, wouldn't it be a good idea for Daily WTF to actually label code segments with what language they are meant to be rather than making the reader infer it from the text.

It's a good litmus test, separating the people who actually know what they're talking about from the others.
I use C/C++, not C# or Java, but there's a couple WTFs that carry over (incremented switch inside a loop being the most obvious one), and the others I would let more knowledgeable people comment on.

But lack of knowledge doesn't stop everybody here from commenting on things, sometimes the best WTFs are in the comments.

Re: Type Safety Considered Harmful

2008-03-19 15:02 • by AT (unregistered)
184740 in reply to 184709
Chris M.:
notJoeKing:
Some Java Guy:
So the real WTF is C#.

Is that even a real programming language?


Nope, it's just the bastard child of Java and C++... they made it for all the C++ programmers who needed an easier and faster environment to develop in. C# was only created because C++ developers were too Prima Donna to switch to VB.Net... Or maybe they just couldn't learn the syntax ;)

{Putting on flame suit for the prissy C++/C# diciples...)

LOL


Well, no. It was actually made for Microsoft managers, who get hives when large numbers of developers or users start using things they can't hold absolute control over, like Java.


Well, no. It was actually made to provide a from-scratch, managed-code environment well-suited to Windows development. MS tried integrating Java with Windows and Sun sued to stop them. You may believe MS did this to protect its OS monopoly, but regardless of that the J/Direct technology created by Sun was much nicer to work with on Windows than JNI.

Re: Type Safety Considered Harmful

2008-03-19 16:15 • by VGR
184759 in reply to 184570
There is a code smell here, but it's not related to the language used. It's the smell of premature optimization. This is clearly superior because the caller might not want to specify all the fields! I'm sure the array is even ordered by importance/dependency.

Re: Type Safety Considered Harmful

2008-03-19 16:23 • by smxlong
Ok, it's a little hokey, but I don't see what the problem is. If a new field is added, this code requires the addition of only a single line of code -- a new case. Each of you, ask yourselves, under your solution (whatever that might be), how many lines would YOU need to add a field? I'm not saying it can't be done some other way, but I really am not seeing a WTF here.
« PrevPage 1 | Page 2Next »

Add Comment