- 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
God I HATE the m_ convention! All my code uses the m convention (as in mLastName). This is MUCH more efficient as I don't have to type the damned _ key with the {SHIFT} key every time!
I am JUST LIKE the genious at GM in the 80's that convinced the powers that be that it would save $X millions per year if they would just spell employee with ONE trailing "E". (Employe) Which... hard to believe, but still true, they did CORPORATE WIDE until the mid 90's.
Strange, but true...
Admin
A closely related, yet simpler C++ weirdness example:
class LastName{} lastName(lastName); // Completely undefined behavior
Admin
One underscore followed by any alphanumeric character is also reserved for library writers (which brings up an interesting point that all variables starting with an underscore are illegal except the variable single-underscore '_')
But it's irrelevant here since you are talking about member variables which are in their own class namespace and so are exempted from the restriction as clashes aren't going to happen. Still, just to be safe, I've always seen this practiced as a trailing underscore, not a leading.
Admin
Not knowing C# makes this a real WTF- as in, WhyTF is this a WTF?
[the captcha is 'unreal']
Admin
K... I've done Delphi and VB and I tend to see a lot of f_ and m_. Delphi programmers used it to denote a class's field I'm sure VB's m_ meant "module variable." But, class fields should be easy to spot with just a simple _.
The _ should make you think "aha this variable is really a field in my class with class-level scope." So if you are writing something that is thread-safe, you would take care to protect those _ fields.
Admin
Admin
Sorry, put my comments inside the quote tags.
Look at LastName = value. Do you see it?
Admin
I can't see it. Wait, I can't see anything! Oh god no I'm blind! Mom always said that doing that would make me go blind! Oh mom I'm so sorry! Er wait, I just had my eyes closed.
Hasn't the exact nature of this WTF been explained in excruciating detail? Something tells me we just found the author of this code.
Admin
you are incorrect. MSDN doesn't specify anything for private variables. protected variables should use camel casing, but you're free to name private variables however you like.
Admin
Actually no it isn't. OK, let me try this as a question: Why in the pleiades do people say that? There is a huge difference between the pound symbol: £ and the hash or sharp symbol: # They are not on the same key (different hands in fact, on my UK keyboard) and they don't remotely look alike. So *why* do I keep seeing this "it is a C with the pound symbol etc. crap? Is there character set mapping/mangling going on that's messing with peoples minds????
Admin
In America (where Microsoft resides and their products are primarily developed), we call the # symbol the pound symbol when referring to it on a phone keypad. And since all we ever care about is our own country, no one immediately associates pounds with UK currency. Besides, aren't you guys converted over to Euros yet?
Admin
Because Americans and Canadians in the early days reserved the £ to mean the "pound sign" or "number sign". Perfectly fine to have two meanings for something between countries. Happens in Spanish and Portuguese all the time (even sex tenses on verbs!)
Admin
Correction I mean the # sign.
Admin
Yes I know # means number - but that's a mile from "pound" which is money??? <mutter mutter> I still find it highly strange...
Admin
but OK, having looked at the wikipedia page, I see what you mean. 8-)
Admin
WOW! I only wanted that sig until someone noticed. I thought for sure that it would take longer than 17 minutes!
Sincerely,
<er, no. that's been done too.>
Admin
Haha, I've done this before. The best part is that in ASP.NET, when the stack overflows as it does in this situation, the stack trace is lost and you get no error location info with the error message.
Admin
I bet more than half the readers didn't catch the problem with the code
Admin
Fellas!
The real WTF is not the camel casing or the protected vs public keywords. The real WTF is that the brilliant developer wrote bad code -- as in doesn't work and has unreachable variables. To the clueless, get a clue. To everybody, focus on the fundamentals first.
Admin
Yes, I have been programming in C# for 3 years and I still make that mistake once in a while. The problem isn't doing that mistake. The problem is thinking you are the best coder in the world and thinking that the problem is the language and not you, considering that this is a bug easy to find.
Admin
With the resharper plugin, it would hi-lite unused variables. If the variable were also private, then it would then tool-tip with a nice message saying that the variable never gets assigned.
Admin
I'm sure he gets it, but you didn't look closely enough. The OP only implemented a set property, not get. Your principle of least surprise is still a good point though.
Admin
I don't consider it trite. I consider it useful. In fact, this is the _useful_ form of hungarian notation, as opposed to the (IMHO) much less useful lpszName type hungarian notation.
Admin
I didn't catch it until it was explained in the first few posts. Why? Because I was not expecting a function call when the assignment operator is used. How silly of me to expect an operator to perform the operation that it is supposed to do. Seriously, what kind of fucktard designs a language where a primitive operator can implicitly be a function call that may do anything it feels like (including recursing infinitely or NOT SETTING THE VARIABLE)? Yes, I'm looking at you, Bjarne Stroustrup.
<font color="Red"> Something didn't quite work out ...
- CAPTCHA Validation Incorrect </font>
Admin
Incorrect. Only identifiers beginning with an underscore and either an uppercase letter or another underscore are reserved everywhere. Identifiers beginning with an underscore and a lowercase letter or a number are only reserved as symbols with external linkage. This does not include class members.
(I'm talking about C, but this comes from C originally. replace "class members" with "struct members" - i don't think they're reserved as struct tags either, but it'd be reasonable for them to be reserved as class names in c++)
Admin
C# was not designed by Bjarne Stroustrup, but by Anders Hejlsberg. As for complaining about property setters potentially recursing: Get used to it, it's a trivial mistake thats trivially easy to spot, fix and avoid. IMO property based syntax adds an effective extra layer of syntactic encapsulation, allowing you to (for example) swap a public data member with an encapsulated getter/setter in a way which other languages doesn't. In this regard it is very very useful IMO and is a Good and Usefule Thing (tm) IMNSHO. YMMV.
(ffs, why does the CAPTCHA fail first time round??? does it time out or something???)
Admin
I cant believe no one has bitched about the fact that this doesn't do encapsulation well. Why would you make your member variable protected?
It should really be
private string lastName;
not
protected string lastName;
Admin
The syntatic arsenic otherwise known as operator overloading has been around much longer than Hejlsberg's D-flat. The purple dinosaur also known as Bjarne seems to be responsible for at least the wide spread use if not actual invention of this perversion.
Admin
What a dumbass...
Admin
Depends. If you want subclasses to be able to access/override the member directly, then it should be protected. If not, then private.
Admin
Yeah, you could have a naming convention to avoid this sort of problem. Or you could just make the data public (since it effectively is anyway), and have a policy like "no nulls across method boundaries" so that the conversion from null to "" isn't needed. (If you're in the habit of using the Null Object pattern, well, "" is the natural Null Object for strings, so you'd probably never have a problem or notice.) Or you could, you know, actually try to practice some form of encapsulation. Instead of testing out a spiffy new language feature intended to replace a pair of methods (without really reducing typing much anyway) which were lying by pretending to encapsulate stuff in the first place. Seriously. How often does anyone ever actually go back and add checks and balances to these kinds of methods?
But. Supposing one wants to keep the insulation from change. Dare I propose a language where it worked like this:
But that's all just off the top of my head.
Admin
Admin
lol, this is such a newb.
I've been codin in C# for like 10 yrs and never done such dumbass thing lol. [H] net really sux cause it has so many bugs. Sun should have fixed them all already, I found them. rofl. This would never happen in ruby cause it's not that dumb. Googd languages dont' allow you to do dumb thingies. I don't like c# anymoore because of this crap.
Admin
Darn! For literally DAYS I've been wanting to use the following sig, but now the fun is already over!
___________________
</er>This is a semi-automated signature. I copy and paste it from Manni's sig every time, then change most of the words.
Admin
Gah, I hate to agree with the guy, but thats what you are asking for when you can't tell a function from a property.
Sure it is a nice shortcut, but I think a language should show you the difference between a simple variable set, which will run in constant (and short too!) time and a function call which could be... say a RMI method to another state!
In any programmer's mind A = B; should not have any side effects beyond the value of A. It allows you, when debugging, to simplify things in your mind. Looking for a stack problem? X should not be suspect, F() is.
Admin
What's wrong with the code: in the setter, LastName should have been lastName.
Moreover, why would he put the null check in the getter? Why not put it in the setter instead? Don't getters tend to get called a lot more than setters?
Admin
That is simply your preference.
I like the potential for the internal representation to be different from the external representation. I can see the need for calculations if the value of B is not kept handy but is computed as needed.
Like any other programming language flexibility, this is subject to abuse, but that does not mean that using this is always abuse.
Sincerely,
Gene Wirchenko
Admin
here is a valid use of recursion in a property:
Admin
For those who hate properties, indexers, and overloading...
http://www.geocities.com/csharpfaq/test2.html
To be honest, I've grown kind of fond of properties and indexers - maybe i'm going soft in the head. I still loathe operator overloading.
Admin
*cough* Troll. *cough*
I like the idea of assignments automatically filtering through your property get/sets... to some extent.
I use Python, where property's are strictly optional. Are they optional in C#?
Admin
public sealed class User
{
private string _lastName;
public string LastName
{
get { return this._lastName == null ? string.Empty : this._lastName; }
set { this.lastName = value; }
}
}
unless a class is inheritable, i always as a rule seal the class. The compiler provides some optimisation for classes that are sealed and every ounce of optimisation will surely lead one day to a pound-ful of robust performance.
I understand from a colleague of mine that the underscore is used within VB.NET and as such, the m however trite it may well be, is the better option
Admin
Me too. Properties aren't too bad because with the use of casing you can diferentiate them, and I almost always use indexers for classes that implement ICollection so that doesn't bother me neither since I know what to expect from them. Plus the function tend to be simple and most complex thing I do is generating an on change event or validation in properties and indexers. However operator overloading and implicit conversion are just evil.(I haven't notice many of those used in the .net libraries by the way).
Admin
And who do you think you are, we ALL know its C-HASH. # == HASH. i like hash.
chill man.
Admin
Sadly, no, as I said C# properties don't create virtual members, they're completely independant from regular members and can't always be transparently switched with "real" members.
Even less so because the C# coding guidelines ask you to CamelCase properties and methods and camelCase members (which clearly identify properties as properties)...
In the end, C#'s properties are more or less equivalent to Java's getters/setters plague, just slightly less ugly.
Another "issue" with C#'s properties is that you can define properties in an interface, but not regular members, and you can't switch between them (defining a member's signature in the interface and being able to implement either a real member or a virtual member via a property hasn't been deemed acceptable by the creators of C#)
Admin
That's a textbook example of why Best Practices call for prefixing member variables with an underscore. Still, it's a pretty common mistake. It wouldn't surprise me to see the compiler generate warnings for these types of errors at some point in the future.
Admin
<font style="font-family: Arial;" size="2">Yeah so the guy messed up a capital.. geeze leave him alone.
But the real WTF for me is here:</font>
Admin
Nothing wrong with the ternary operator.
Mdollarsign u r cl3v3r.
Admin
Stupid D-flat programming language - it actually does what the program was written to do ! Recurse till the stack falls over. Tisk - tisk : the stupid compiler should have guessed what the clever C++ programmer wanted to do :D
Admin
The setter is clearly tail-recursive, so it should never overflow the Stack. Ok, so it will never halt either, but hey...
The real WTF is that IL has a TailCall opcode, but it doesn't seem to get emitted by any of the compilers!
Admin
props! [pi]