Re: Struct vs Class

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




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
improvements in syntax
<snip>

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.

.



Relevant Pages

  • Re: Struct vs Class
    ... Whatever - I'm just going to stick with the idea that all structs should be ... immutable for now in light of the reasons that you have stated. ... many newbies do many, many things wrong. ... then creates problems elsewhere in the language. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Struct vs Class
    ... I agree that structs should be immutable, ... many newbies do many, many things wrong. ... reasoning behind your conclusion. ... I guess I'd prefer immutability in structs just so shrewd programmers don't ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: a new language
    ... there is not that much reason to use it for much...). ... then I tried ripping off ideas from java (making a mostly statically typed ... language, and a vm resembling the jvm). ... my design included 'structs', where structs differed primarily from classes ...
    (comp.lang.misc)
  • Re: C vs. C++ vs. Java
    ... as specified by it's standards. ... topic" for a C language newsgroup. ... We were all newbies once. ... newbie is not OT for a C language newsgroup. ...
    (comp.lang.c)
  • Re: Better is better: improving productivity through programming
    ... >> it to be a pretty hard choice? ... > surprised to hear of hordes of newbies unable to choose between them. ... They're spending time figuring out ... Find a text or tutorial that you like, and use the language ...
    (comp.lang.lisp)