Re: Struct vs Class
- From: "Bruce Wood" <brucewood@xxxxxxxxxx>
- Date: 30 Oct 2006 17:04:15 -0800
Dave Sexton wrote:
Hi Bruce,
<snip>
I'm sure you know about the confusion: non-intuitive behaviour when a
property returns a Point, or when a Point is boxed for storage in an
aggregate structure. Many, many newbies try to use the above p.X = 7
syntax in those situations and then wonder why their point's
coordinates didn't change. Yuck.
I just plain don't indulge in creating mutable structs for questionable<snip>
improvements in syntax
I agree that structs should be immutable, without question. However, many,
many newbies do many, many things wrong. I'm not sure that I agree with the
reasoning behind your conclusion. Newbs seems to always catch all exceptions
too, but we really can't get rid of that functionality either.
I guess I'd prefer immutability in structs just so shrewd programmers don't
make the same mistake on accident. I can't think of a better reason than
that, unfortunately.
LOL... well, I guess appealing to the newbie thing wasn't a good
argument. :-)
What I meant to say is that if Location is a property of type Point,
then this looks as though it ought to work:
myRectangle.Location.X = 7;
but it doesn't. It's perfectly logical that it doesn't work, but the
logic is subtle and requires considerable knowledge of how .NET / C#
works. I dislike things that look as though they should do something
but which, for subtle reasons, do something else (or, in this case,
nothing). Granted, the compiler complains about this specific case, but
there are other cases in which it doesn't. I prefer that my code be
clearly readable to all: newbies and veterans alike, so I prefer to
give up syntactic sugar if it makes my code clearer.
It's so much easier to remember that you can't change a struct's
state--ever--than to remember that you can change it under some
circumstances but not under other circumstances, for perfectly logical
but non-obvious reasons. That just makes the code more difficult to
maintain, IMHO.
One of the clues that this is going on is when newbies (who usually
know other languages, so they're not new to programming, just new to
C#) make the same mistake over and over again. Now, sometimes the
feature is so truly useful that its very utility outweighs the
resulting confusion. Structs, for example, confuse the heck out of
people coming from the C/C++ world, but it's so very useful to be able
to create new objects with value semantics that I wouldn't give it up
to make the language easier to understand. Mere semantic sugar, such as
myPoint.X = 7;
is another thing entirely. I would rather live with more long-winded
code and do away with the confusion than have a handy shorthand that
then creates problems elsewhere in the language.
I've worked in several shops of mixed-language, mixed-skill
programmers, so I prefer code that someone not terribly familiar with
the language can understand. Mutable structs just throw a big wrench
into that, so I avoid them.
.
- Follow-Ups:
- Re: Struct vs Class
- From: Dave Sexton
- Re: Struct vs Class
- References:
- Struct vs Class
- From: JohnGoogle
- Re: Struct vs Class
- From: Dave Sexton
- Re: Struct vs Class
- From: JohnGoogle
- Re: Struct vs Class
- From: Bruce Wood
- Re: Struct vs Class
- From: Martin Z
- Re: Struct vs Class
- From: Bruce Wood
- Re: Struct vs Class
- From: Dave Sexton
- Re: Struct vs Class
- From: Martin Z
- Re: Struct vs Class
- From: Bruce Wood
- Re: Struct vs Class
- From: Dave Sexton
- Struct vs Class
- Prev by Date: Re: Struct vs Class
- Next by Date: Clearing last item from combobox?
- Previous by thread: Re: Struct vs Class
- Next by thread: Re: Struct vs Class
- Index(es):
Relevant Pages
|