 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
John Thilenius Guest
|
Posted: Thu Aug 21, 2003 10:46 am Post subject: Coding standard rule for using struct's..? |
|
|
If you were tasked with creating a guideline which defines what a struct
can/cannot contain, or should/shouldn't do, what would it look like?
One reason for the guideline might be to prevent struct's from becoming
large nested classes, which can happen over time. Some example
guidelines might look like the following:
Extreme...
1) A struct should not contain any member functions, or objects
containing member functions.
Restrictive...
2) The only methods a struct should contain are those which support
member initialization, assignment & copying of the struct.
Open...
3) Use your own judgment. Typically, use a struct for ...
Assuming you have lots of newbie developers, what would your guideline
look like?
Thanks in advance,
John Thilenius
Boeing Software Engineering
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jerry Feldman Guest
|
Posted: Fri Aug 22, 2003 9:38 am Post subject: Re: Coding standard rule for using struct's..? |
|
|
On 21 Aug 2003 06:46:37 -0400
John Thilenius <john.m.thilenius (AT) boeing (DOT) com> wrote:
| Quote: | If you were tasked with creating a guideline which defines what a
struct can/cannot contain, or should/shouldn't do, what would it look
like? One reason for the guideline might be to prevent struct's from
becoming large nested classes, which can happen over time. Some
example guidelines might look like the following:
Extreme...
1) A struct should not contain any member functions, or objects
containing member functions.
Restrictive...
2) The only methods a struct should contain are those which support
member initialization, assignment & copying of the struct.
Open...
3) Use your own judgment. Typically, use a struct for ...
I would probably go for 2 since they already have default constructors |
and destructors. And, I would add some verbage to suggest places where a
struct might be used more appropriately than a class. Or, an even more
extreme approach is to simply not allow structs.
--
Jerry Feldman <gaf-nospam-at-blu.org>
Boston Linux and Unix user group
http://www.blu.org PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Torsten Robitzki Guest
|
Posted: Fri Aug 22, 2003 9:49 am Post subject: Re: Coding standard rule for using struct's..? |
|
|
Hi,
John Thilenius wrote:
| Quote: | If you were tasked with creating a guideline which defines what a struct
can/cannot contain, or should/shouldn't do, what would it look like?
One reason for the guideline might be to prevent struct's from becoming
large nested classes, which can happen over time. Some example
guidelines might look like the following:
Extreme...
1) A struct should not contain any member functions, or objects
containing member functions.
Restrictive...
2) The only methods a struct should contain are those which support
member initialization, assignment & copying of the struct.
Open...
3) Use your own judgment. Typically, use a struct for ...
Assuming you have lots of newbie developers, what would your guideline
look like?
|
4) Learn the difference between class and struct and judge on knowledge
and rational reasons rather then on guidelines.
I think it's a common misbelieve that one can turn a newbie into a
competent software developer with a guideline.
regards
Torsten
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Fri Aug 22, 2003 4:18 pm Post subject: Re: Coding standard rule for using struct's..? |
|
|
John Thilenius <john.m.thilenius (AT) boeing (DOT) com> writes:
| Quote: | If you were tasked with creating a guideline which defines what a struct
can/cannot contain, or should/shouldn't do, what would it look like?
One reason for the guideline might be to prevent struct's from becoming
large nested classes, which can happen over time. Some example
guidelines might look like the following:
Extreme...
1) A struct should not contain any member functions, or objects
containing member functions.
Restrictive...
2) The only methods a struct should contain are those which support
member initialization, assignment & copying of the struct.
Open...
3) Use your own judgment. Typically, use a struct for ...
Assuming you have lots of newbie developers, what would your guideline
look like?
[snip] |
4) All programmers will be taught what POD means, what operations are
safe for POD, and not safe for non-POD. All programmers will be
taught struct != POD. Code review will analyze each struct for
POD-ness and that it is correctly documented it as POD or
non-POD. There will be a programming education session on POD,
Friday at 10am.
In my expierence, there are two views of struct, which together
dominate nearly all C++ programming: (a) struct is 'just like in
C', and (b) struct is 'just like class but with different default
access' . ( (b) is of course the correct view, but bear with me. )
People of the (a) view will memcpy structs about with impunity. They
will assume that if struct A is a 'prefix' of struct B, A and B
are layout compatible. And so on. In short, they expect struct ==
POD, even if they've never heard the term 'POD'.
My problem with (1), and even more so with (2), is that they allow
non-POD structs, and yet seem to be intended to cater to people
who think (a) - but if a struct has base classes, access
qualifiers, reference members, or non-POD members (and (1) allows
non-POD members! ), it can't be safely treated in the ways those
of the (a) view expect - but since they don't know about POD-ness,
they'll trust the guideline, and write non-POD structs that match
the guideline. The result will all too often be hard-to-reproduce
bugs that defy your best efforts and most effective tools.
If you feel you must have a 'protect the ignorant' guideline for this
situation:
5) All structs must be POD, and all structs be designed, implemented,
and reviewed by people who understand POD-ness.
Note that (3) essentially requires (4), and even (5) requires (4) for
some portion of the development team.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Attila Feher Guest
|
Posted: Sat Aug 23, 2003 3:28 am Post subject: Re: Coding standard rule for using struct's..? |
|
|
Francis Glassborow wrote:
| Quote: | In article <3F438E1E.1449DE87 (AT) boeing (DOT) com>, John Thilenius
[email]john.m.thilenius (AT) boeing (DOT) com[/email]> writes
Assuming you have lots of newbie developers, what would your
guideline look like?
[SNIP]
Now making a nested private type a struct seems reasonable. It would
also seem reasonable to use a struct for any type whose state you
elect to make public and note that as soon as such a type has been
published you are locked to that decision for that type.
|
I would add to this something which is not my tought (as usual ), but
seems to be appropriate. Traits (and other "collection of data static at
compile time) seems to be also a place where the use of a struct is
appropriate. Traits (once published) are such that they only get extended
and they basically have only "public interface", a loads of static members.
Kind of a little namespace. :-)
--
Attila aka WW
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Sat Aug 23, 2003 3:29 am Post subject: Re: Coding standard rule for using struct's..? |
|
|
On 22 Aug 2003 05:49:08 -0400, Torsten Robitzki <Torsten (AT) Robitzki (DOT) de>
wrote:
| Quote: | 4) Learn the difference between class and struct and judge on knowledge
and rational reasons rather then on guidelines.
|
The difference is spelling and nothing else.
class C : B { == struct C : private B { private :
struct S : R { == class S : public R { public :
What rational reasons derive from these observations?
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Andrea Griffini Guest
|
Posted: Sat Aug 23, 2003 2:42 pm Post subject: Re: Coding standard rule for using struct's..? |
|
|
On 22 Aug 2003 23:29:32 -0400, John Potter <jpotter (AT) falcon (DOT) lhup.edu>
wrote:
| Quote: | What rational reasons derive from these observations?
|
That rational reasoning doesn't help much in C++
wonderland. Study helps more.
Andrea
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Torsten Robitzki Guest
|
Posted: Sat Aug 23, 2003 5:48 pm Post subject: Re: Coding standard rule for using struct's..? |
|
|
John Potter wrote:
| Quote: | On 22 Aug 2003 05:49:08 -0400, Torsten Robitzki
wrote:
4) Learn the difference between class and struct and judge on knowledge
and rational reasons rather then on guidelines.
The difference is spelling and nothing else.
class C : B { == struct C : private B { private :
struct S : R { == class S : public R { public :
What rational reasons derive from these observations?
|
Now, as the newbies know the difference they might decide to use a
struct when there is the need for a class with a lot of public members.
But my original point was that writing a coding standard about the usage
of the key words struct and class to prevent newbies from making design
mistakes is pointless. It would be better to educate them and thus give
them the ability to make a reasonable decision on there own.
regards
Torsten
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Peter Schneider Guest
|
Posted: Sat Aug 23, 2003 5:57 pm Post subject: Re: Coding standard rule for using struct's..? |
|
|
Hi,
John Thilenius, Boeing Software Engineering, wrote:
| Quote: | If you were tasked with creating a
guideline which defines what a struct
can/cannot contain, or should/shouldn't
do, what would it look like?
|
It's a design question, rather than a coding question,
not at all C++ specific.
"Though shalt not publish implementation details", needless
to remind a Boeing employee -- after all, some papers
in my Software Engineering textbooks came from Boeing.
The broader the audience of the publishing, the worse.
For oneself, in a limited piece of code, it is a convenience
for a bad typer. In a library interface, it's normally
a major mistake.
| Quote: | Assuming you have lots of newbie developers,
what would your guideline look like?
|
Not differently. Give them as much education
and as much freedom as possible.
They'll experience that their convenient little innocent data
structures frequently need to be rewritten, once all side
effects and special cases are discovered and all extensions
are to be implemented.
If you simply forbid structures, they'll use bundles of arrays
or something worse instead. It's not about rules, it's about
insight here.
Regards,
Peter
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Philippe Mori Guest
|
Posted: Sat Aug 23, 2003 8:07 pm Post subject: Re: Coding standard rule for using struct's..? |
|
|
| Quote: |
Open...
3) Use your own judgment. Typically, use a struct for ...
For enums. Define a struct that has an enum in it. Other classes inherit
from your struct.
|
For enum, I prefer namespace (you can an using in function that uses it
a lot).
I do not recommend classes that inherit from structure and give access
to all structure data members. Better to have the structure as a private
data member and provide accessor in the class.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Dennett Guest
|
Posted: Sun Aug 24, 2003 11:23 pm Post subject: Re: Coding standard rule for using struct's..? |
|
|
John Potter wrote:
| Quote: | On 22 Aug 2003 05:49:08 -0400, Torsten Robitzki
wrote:
4) Learn the difference between class and struct and judge on knowledge
and rational reasons rather then on guidelines.
The difference is spelling and nothing else.
class C : B { == struct C : private B { private :
struct S : R { == class S : public R { public :
What rational reasons derive from these observations?
|
The fact that the different spelling has different connotations
to human programmers. "struct" makes many think in C-like terms,
and "class" pushes them towards thinking more in terms of
interfaces. These non-technical differences are important,
of course: most code is at least as much about communication
between humans as it is about communication between a human
and a computer.
If I'm going to write access qualifiers at all, I tend to
write "class". If everything is to be public, I tend to write
"struct". Unless local guidelines/rules override that, in which
case I do whatever they dictate (unless it's crazy, and then I'd
do what they dicate until/unless I can get them changed).
-- James.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Dennett Guest
|
Posted: Tue Aug 26, 2003 8:39 am Post subject: Re: Coding standard rule for using struct's..? |
|
|
John Potter wrote:
| Quote: | On 24 Aug 2003 19:23:31 -0400, James Dennett <jdennett (AT) acm (DOT) org> wrote:
John Potter wrote:
The difference is spelling and nothing else.
class C : B { == struct C : private B { private :
struct S : R { == class S : public R { public :
What rational reasons derive from these observations?
The fact that the different spelling has different connotations
to human programmers. "struct" makes many think in C-like terms,
and "class" pushes them towards thinking more in terms of
interfaces.
I believe that, but there are other humans.
|
We're not all clones yet, then. Probably a good thing.
| Quote: | I suspect that this has much to do with the age of the human. The
novice is likely not coming from C and has never heard of a struct.
Given a guideline of public before protected before private and most
derivation should be public, it is obvious that struct has the right
defaults and should always be used.
|
Given that struct, unless manually overridden, defaults
to exposing everything, it is obvious that class has the
right default and should always be used ;)
| Quote: | Is this a call for education on the prejudiced connotations of the
ancient like me? Guidelines might be easier and more appropriate.
|
I think it comes down to taste. Any purely logical reasons
will pale into insignificance next to human factors, and the
chance of getting complete agreement among a large group of
people on an issue that is subjective is close to zero.
Probably the thing that does matter is to teach the truth,
that in C++ class and struct (as used to declare class types)
are almost identical, and that any choice for when to use
which is a convention rather than being based on a language
restriction.
-- James.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|