- 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
I was thinking that may be a possibility and that the code may just be overly verbose, but the comment that I quoted above shows that this is not the case. And even if it was, the code would still count as a WTF, just not a potential source of issues.
Admin
Bonus WTF from the same app:
Instead of something like:
Admin
Admin
I have to disagree on the last one.
It should be a hanging offence.
Admin
Are you one of those people who declares every member public and accesses them directly without using getters or setters? Excuse me, I've just been sick in your mouth.
Admin
No, it has to look it up, not use the saved result.
Have you never noted the time a procedure started and then at the end computed the elapsed time as Datetime.Now - the start time? Use the saved copy and you would always get zero.
Admin
Admin
"public fields are bad; public mutable fields are very bad; public mutable fields in a struct should be a firing offense."
You're clearly someone who has never had to write high performance C#. Classes have overhead. Properties have overhead. Readonly fields have overhead. Public mutable fields in a struct are common when performance is critical.
Admin
Unless you care about performance.
Admin
struct: A warehouse of data that needs no special manipulation, sanity checking, or side effects. It just is, and should be treated as much as possible like a useful relic of C.
class: Everything else.
Built-in initializer and cleanup functions are handy, but when all it does is hold a bunch of data that other things use, there's no point in not being public. Especially if you're making simple getter/setters equivalent to a public member, just to pretend to be pure.
In any modern language you can transparently convert a public member to a property when you need sanity checks or side effects, anyway. (C++ really shows its age here.) If you are writing an external ABI I'd hope you'd figure out what you need to do ahead of time.
Admin
public struct Rectangle { public int Left; public int Right; public int Top; public int Bottom;
public int Width { [...] }
[...] }
Yes it has public fields. So what? What's the "additional worth" of properties here?
AFAIK there are two differences:
Admin
The Real WTF is that it could have simply been implemented like this:
Admin
FTFY (It's always fun to debug strange errors because of slight discrepancies in equality/inequality checks or other compare operators)
Admin
Admin
I know: I posted my comment. Looked at it once more and found that one too. But was already too late (and too lazy to write a second one).
Admin
There are certainly times when you want to make an object immutable. There are times when you want to have a "key" value that is immutable while other elements of the object state are not. But it is not at all clear that all objects should always be immutable. Yes, if you're going to add them to a hash table, they'd better have some sort of key that is immutable. But if that's not how the object is used, why impose such a constraint on it? I've built lots of objects that aren't immutable, and where that's a very good thing because it would be a major pain to have to recreate the object every time something changes.
Admin
The same thing happens in the old IBM PC. The upper and lower halves of the time are kept in separate places, so if you read one the other has a chance of rolling over on you. Time going backwards does really bad things to task schedulers! I forget how we fixed this, it might have been as kludgy as seeing if the lower half was just before or after flipping over, we reread everything until we're sure we got a good reading.
Admin
Of course! Because if you write
That's terrible, because outside objects can be changing the value of foo at any time.
But
Whew, now that solved the problem. This is a completely different way of making classes interact.
In Visual Basic they've added a nice little feature where you don't even have to write simple getters and setters. If you declare something to be a "property", the getters and setters are generated automatically. So in VB:
That would be horrible and evil and non-enterprisy. But
Now all those problems go away, the grass turns green and the unicorns come out to frolic in the sunshine.
Admin
Or about not cluttering up your code with getters and setters that give exactly the same results as a simple assignment. Or wasting your life writing such functions.
Here we are in a thread ridiculing programmers who took five lines of code to do what they could have done in one, and someone posts why he thinks it's a great idea to use five lines of code to do what you could have done in one.
Please explain to me what problem is created by making a variable public that is solved by making it private with a getter and setter. Not, "Because somebody wrote a rule in a book that we shouldn't make variables public and this lets me do what I need to do without breaking the rule." I mean, tell me what actually bug or maintenance problem or whatever is fixed by using get/set instead of a public variable.
Now if you have a getter or setter that does validity checks or has side effects, that's a different story. I'm saying, compare "public int x" to "public void setX(int value) {x=value;} public int getX() {return x;}".
Admin
I was thinking that I wonder if order of evaluation of parameters is defined in the language spec. Of course just because C# does -- I assume you are correct in saying that -- doesn't necessarily mean that every language does.
In any case, I would be very reluctant to write code like:
Does that get called with 1 and 2 or 2 and 1? Or something else? Even if it is, in fact, strictly defined in the language spec, I think many programmers would not be sure of such a technicality, and it would be potentially confusing. It's like putting parentheses in complex expressions: I often put in parentheses that are not strictly necessary because the default evaluation order would be right anyway, but just as a help to future programmers who may not remember whether xor comes before or after multiply and that sort of thing. (Hey, I just got burned on this when I wrote "index and 1 = parity". I was thinking "(index and 1) = parity" but the compiler interpreted it as "index and (1 = parity)".)
Admin
Don't run this code (nor the rest of any program that contains it) on a DeathStation 9000, unless you want the crispy-fried blood of millions on your hands.
Admin
What do you mean by sequence point? In C, each of those semicolons would be a sequence point.
However you phrase it, it's not an optimization so much as it's a patch for buggy programs. It's very much a special case; I can't imagine another volatile function where doing that wouldn't be a horrible bug in the compiler.
Admin
C++ -> The compiler is free to reorder the calls as necessary.
And no: Writing code like "i++ * i * ++i" is never a good idea. No matter if the order is defined or not.
Admin
Admin
Admin
Admin
Admin
I can't figure out what language Paul Newman's code is. If it's based on C or C++, the output is a perfectly fine possibility because undefined behaviour can have any result. Paul Newman's boss will be happy because the program will produce desired results in QA, but customers will be unhappy because the program will format their hard drives.
Admin
Admin
Admin
She'll tell you in a second
Admin
Partially correct. HashCode and Equals is an entire can of worms. If an object's equality does not change, the hashCode should also not change. However, two objects with the same hashCode may not also be equal, as was previously pointed out.
Work with hibernate on a large scale and you'll encounter this a bit. Associations ( Colony hasMany Ants ) are stored in hashTables. If you add a new Ant to a Colony you can end up overwriting (deleting and replacing) an existing Ant, or duplicating ants if you were to re-add an existing one if you don't setup hashCode correctly. That's why you need to be careful about updating hashCodes for every little object change.
The best approach is to setup hashCode and equals to match business logic equals, not programmer logic equals. The identity, for example, should be excluded from hashCode and only be used in equals if both objects have it set.
http://t.co/GLocCCg0k0
@EqualsAndHashCode in Groovy is your friend :-)
Admin
I should add that hashCodes are used to generate ETags for REST APIs in some projects as well. One thing I like about this approach is it helps the programmer really think about what "change" is in an object.
Admin
Admin
I got to agree with you on that. Plus: You successfully abstracted away the inards of your class, why do you want to go on and expose them with loads of getters and setters?
Admin
Admin
Norman's code assumes that the clock can only tick at most once in the time it takes to execute your function:
Admin
What happens when you realize you need to add validity checks and/or side effects, and you have thousands of direct references to the field?
C# handles this quite well by generating the private backing field and the default getters & setters, so you don't have to clutter your code or waste time writing boilerplate.
Admin
If you add instead, the hash for {a,b} will be 2*hash(a) for every a==b. That's a bit more reasonable.
Admin
Do this:
It is highly unlikely to be 0.