C
Christopher
If you were on a team of developers that had little C++ experience,
how would you go about explaining the reasoning as to why the
following rules should be followed? (Or why they shouldn't)
1) Class defintions go in their own .h (or .hpp for templates) file
named after the class
1a) Every class should have its own .h (or .hpp for templates) file
1b) A .h file should never contain class implementation (except for
inline functions or methods)
2) Class implementations go in their own .cpp file (except for
templates)
2a) A .cpp should never contain a class definition
3) One should not define multiple classes in a single .h file
3a) Nested class defintions should be avoided without good reason
(what's good reason?)
6) Variables should never be defined in a .h file
7) Globals should not be defined in a .h file
8) Every .cpp file should compile into a .obj file without error or
warning on its own.
i.e In Visual Studio, you should be able to right click a .cpp in
the source tree and choose compile, without error or warning in the
output.
----
Sadly, I find myself having to justify these things. (of course
someone might point out I am wrong or exceptions to the above), but
these are things that I've always had drilled into my head throughout
the years and have rarely come acorss other developers that didn't do
the same. I am finding it frustrating to work on a project where 30
classes defintions reside in the .h same .h file, a single .cpp file,
or worse a precompiled header. There is discussion, but I have to
explain reasoning.
So far, I've come up with the testability argument:
When unit testing one frequently wants to test a single class. A proxy
class may need to be created to test its protected or privare methods.
Proxies may have to be created for its input and outputs. I should be
able to include a single .h file, link in the appropriate .obj files
and compile a unit test without the added clutter of satisfying
includes or linkage needed by unrelated classes.
I don't know what else to say aside from, "just do it, its the thing
to do." and noone is going to buy that.
how would you go about explaining the reasoning as to why the
following rules should be followed? (Or why they shouldn't)
1) Class defintions go in their own .h (or .hpp for templates) file
named after the class
1a) Every class should have its own .h (or .hpp for templates) file
1b) A .h file should never contain class implementation (except for
inline functions or methods)
2) Class implementations go in their own .cpp file (except for
templates)
2a) A .cpp should never contain a class definition
3) One should not define multiple classes in a single .h file
3a) Nested class defintions should be avoided without good reason
(what's good reason?)
6) Variables should never be defined in a .h file
7) Globals should not be defined in a .h file
8) Every .cpp file should compile into a .obj file without error or
warning on its own.
i.e In Visual Studio, you should be able to right click a .cpp in
the source tree and choose compile, without error or warning in the
output.
----
Sadly, I find myself having to justify these things. (of course
someone might point out I am wrong or exceptions to the above), but
these are things that I've always had drilled into my head throughout
the years and have rarely come acorss other developers that didn't do
the same. I am finding it frustrating to work on a project where 30
classes defintions reside in the .h same .h file, a single .cpp file,
or worse a precompiled header. There is discussion, but I have to
explain reasoning.
So far, I've come up with the testability argument:
When unit testing one frequently wants to test a single class. A proxy
class may need to be created to test its protected or privare methods.
Proxies may have to be created for its input and outputs. I should be
able to include a single .h file, link in the appropriate .obj files
and compile a unit test without the added clutter of satisfying
includes or linkage needed by unrelated classes.
I don't know what else to say aside from, "just do it, its the thing
to do." and noone is going to buy that.