- 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
A bit of redundancy there: aren't all "best" coders self-proclaimed.
I wish I were half as good as these self-proclaimed best coders.
Sincerely,
Gene Wirchenko
Admin
For once, we can't blame the "stupid Americans"! Bloody Limeys!
USA! USA! USA!
Admin
What would never happen? Writing crappy code? I've inherited plenty of C++ code in the past that would cause me to beg to differ.
Admin
This would never happen in a case-insensitive language like VB.
</sarcasm>
Admin
I think that it would be difficult to proclaim a status of "BEST" if one's experience is still being measured in semesters. :)
.jc
Admin
Maybe this guy should have tried debugging his work before opening his mouth. (It is better to keep your mouth closed and let people think you are a fool than to open it and remove all doubt. -- Mark Twain)
Admin
AFAIK at least VB.NET /is/ case-sensitive ;-)
Oh and did this man ever hear of the cool & _really_ handy debugging tools that are built into any later IDE?...
Admin
Bahhh Damn you L!! how dare you grow in size!
Admin
Waiting for someone to comment on what's wrong with the code.
Admin
No, its not.
Admin
LOL. Copy it, set the value of LastName property, and wait for it to finish. Come back and let us know when its done.
Admin
VB.Net is most definitely NOT case sensitive, which is why you can't have CLRCompliant attributes on c# assemblies that have differently-cased methods or classes w/ the same name.
Admin
Oh I get it. Recursion. Case-sensitive.
Admin
So am I. I don't know .Net or C-Pound.
... ugh, I'm falling into the depths of those who scream "Brillant" and the related.
Admin
HOW FUNNY!!!
OMG I couldn't stop giggling.
Admin
it should have been written:
...
set
{
lastName = value;
}
with the uppercase L as written it is a recursive call to the setter.
/praying the formatting of this post is okay.
Admin
A typical coding style rule for C# and C++ is to give the identifiers which are fields to your class the prefix of an underscore ("_") so that when you are writing property code you are astute to what you are returning and setting to avoid stupid mistakes like this one.
Admin
This would have worked:
Admin
For starters, it's written in C#.
What happens is, the line "Lastname = value" recurses infinitely. Why? Because the "preprocessor" in C# translates:
Lastname = value
into
void setLastName( const char *value )
{
setLastName( value );
}
rather than doing what any reasonable person would expect (that is, set a variable when one appears on the left-hand side of an = sign). And the original coder is right. This wouldn't happen in an even remotely thought-out programming language.
Admin
I get it, because User doesn't inherit from Person right?
Admin
"Yeah! When I make a case error in C++, it just blissfully returns unexpected values and I never have to worry about run-time errors."
Admin
Just out of curiousity would it ever be considered a "good thing" for a property to be recursive in the first place? Is it possible that the compiler not catching a recursive call is actually the big bug?
Admin
<font size="3">HA,
Thats almost as stupid as the time that I did exactly the same thing, I say almost because... well I figured it out almost straight away - and kept quiet about it so no one would know...
</font>
Admin
I beg to differ. This *is* well thought out. It's clearly obvious just from the source he recursing ad infinitum, it's actually painfully obvious (unlike following compound pointer derefs and typecasting is C++).
The programmer should have followed some typical coding style that makes field identifiers clearer, the most-popular of which is to preface the identifier with an underscore, so that you know two things off the bat:
Admin
I don't see why it should be prohibited. Would you want to add a similar feature for functions as well?
Admin
You don't want the compiler catching recursion, anyway; it would have to inject code around the method body, making it slower and harder to inline, among other things.
Admin
yes, it is a c with the pound symbol but it is C-SHARP .... <cue>charlie brown scream as he tries to kick the american football</cue>
Admin
The funny thing is the code conforms to Microsoft's design guidelines for naming classes/members/properties (which I personally find to be quite lame):
<FONT face=Arial size=2><FONT title=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconnetframeworkdesignguidelines.asp face="Times New Roman" size=3>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconnetframeworkdesignguidelines.asp</FONT></FONT>
<FONT face=Arial size=2><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></FONT>
Admin
I have to agree ... surely the world's best coder should also be the world's best variable declarer. Also, why does someone need to boast about how good they are, when in the industry you have to show/do what you are good at.
OH YEAH, when they talk more than they do, they just don't have the goods....compensating for what they lack.
Admin
There are some checks the compiler can do for recursion without injecting code simply by checking for unconditional recursion before an exit point (I think VS might do this already).
Admin
common sense is not for everyone
Admin
Thief! I'm calling the signature police! I'm pressing charges! The death penalty for you!
Admin
If it does conform to their guidelines, their guidelines suck. If you name your method and the variable for your method the same thing with a very slight change in capitalization, you're going to make this mistake.
Besides, as someone else pointed out above, wouldn't it have been _lastName for the variable, if the programmer had been following MS's policy?
Admin
I'm going to be honest. I've done this several times. Of course, the mistake itself is not in this case the big WTF. :)
.NET does have an interesting set of WTFs, but hell, I would take it over C++ for almost any purpose..
Admin
That's somewhat old. I prefer the Philips guide plus some enhancements I have made.
Admin
Yes. It's probably not common, but I can think of certain cases where you would do that.
public Foo Bar
{
set
{
try {
process(value);
moreProcess(value);
evenMoreProcess(value);
}
catch
{
Bar = value.playSafe; // Recursion
}
}
}
Admin
Clearly the better alternative is to have two methods (getFoo() and setFoo()) for every property your class wants to expose.
Oh wait, this does the same exact thing, just in the IL.
Admin
You're right. You can technically place logic in property declarations (although if it's at all complex it should be frowned upon). While for the life of me I can't think of why anyone would want to, I guess one could feasibly have recursive logic in a setter or getter. And make it work without crashing the software at that.
Admin
That gets close to violating the principle of least surprise. What do I mean by that? You usually don't expect an exception to be raised when you read a property... maybe when you set one, but not when just reading it.
If something bad happens in the property read you would want to return a null value (say you implement a Parent property and when you look for the parent object it isn't there) or whatever you establish as baseless for a value type (0/-1) to avoid surprising the client.
If you must return an exception, then consider using a method instead and return an explicit exception (ParentNotFound), etc.
Admin
Yes, C++ is so much more thought out. Witness the following example:
Admin
Agreed. It's bad.
Property reads are good places to place calculated values or complex object lookup returns. For example if you have a data object with properties to retrieve objects higher up in the food chain, a property read is a good place to do that. Say this (C# pseudo):
Of course, the natural counter-argument is "What if the database lookup fails for for Department.Employees? Returning 0 employees would be bad because that is wrong!".
Well, that depends on what you are doing. 0 and NULL are different. If you return a null value, you could react to that. If it's serious, then by all means generate an exception. But... are you going to be reacting appropriately to the exception if/when it occurs for EVERY property read? Probably not... so a method call would be better.
Admin
Uh... did you read the code above? Where would the code above raise an exception on get?
Admin
This property cannot be read. It's an artificial example anyway.
Admin
Admin
Such as Ruby you mean?
And in C#'s case, what was extremely badly thought of was considering properties as nothing more than syntactic sugar for Java-style accessor functions instead of virtual members generators (which is their role in Python or Ruby)
Admin
Guess that was before your time C-Pound
Just in case the freakin' formatting doesn't work ... http://www.thedailywtf.com/forums/34804/ShowPost.aspx
Admin
Actually, they are a continuation of the VB4 style property let/set/get syntax. Not sure why you think Java was involved.
Admin
You mean with TWO leading underscores. I agree, you don't do THAT. But one is OK.
Admin
Yes, I did. I didn't say it generated an exception, it's going to be a SURPRISE when you theoretically run it and it locks up your process. Get it, principle of least surprise?
Admin
For non .NET folks (like myself :)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
{<o:p></o:p>
LastName = value;<o:p></o:p>
}<o:p></o:p>
<FONT size=2> {<o:p></o:p></FONT>
<FONT size=2> setLastName(value);<o:p></o:p></FONT>
<FONT size=2> }<o:p></o:p></FONT>