473,785 Members | 2,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

work related dig regarding separation of definitions and implementations

Hi.

I've got a fella working for me who is trying to convince me to change our
coding standards to using separate .h and .cc files for definitions and
implementations . I know this is a perfectly valid way of doing things but
in my mind it is kind of quirky. Here's why.

The first reason I'd resist deals with the simplicity of makefiles. When
including most code from headers where classes are protected by #ifdef
blocks the makefile doesn't have to have a separate build rule for each
class.

Next, TTBOMK you need to do your classes in header files if you are going
to make them into generic templates...wel l, putting them into headers
isn't strictly a requirement but the other restrictions for (templatizable
code) would encourage only using headers.

I understand that there a a couple of reasons why you might want separate
definitions and implementation, such as when a class has a static data
member, or if you intend to create a shared object library of class code,
but it seems to me that putting the code into the header files is a bit
more elegant and the other way would be more of a holdover from the old
days before templates.

I'm wondering what comments the upper echelon lurkers might have. Do they
agree with my reasoning, or can they give me reasons why I should
reevaluate my assertions.
Jan 23 '07 #1
4 1455
noone wrote:
I've got a fella working for me who is trying to convince me to
change our coding standards to using separate .h and .cc files for
definitions and implementations . I know this is a perfectly valid
way of doing things but in my mind it is kind of quirky. Here's why.

The first reason I'd resist deals with the simplicity of makefiles.
When including most code from headers where classes are protected by
#ifdef blocks the makefile doesn't have to have a separate build rule
for each class.

Next, TTBOMK you need to do your classes in header files if you are
going to make them into generic templates...wel l, putting them into
headers isn't strictly a requirement but the other restrictions for
(templatizable code) would encourage only using headers.

I understand that there a a couple of reasons why you might want
separate definitions and implementation, such as when a class has a
static data member, or if you intend to create a shared object
library of class code, but it seems to me that putting the code into
the header files is a bit more elegant and the other way would be
more of a holdover from the old days before templates.

I'm wondering what comments the upper echelon lurkers might have. Do
they agree with my reasoning, or can they give me reasons why I should
reevaluate my assertions.
I know of no "upper echelon lurkers", so forgive my barging in, but
I have only one issue to mention: speed of compilation. As soon as
you cross a hundred file barrier (and have a team of more that three
people), you'll probably want to separate your implementation in
a different translation unit for each class so that it could be
independently compiled and not cause recompilation of all other
translation units that include it.

C++ was designed with large scale systems in mind. That's not the
only paradigm it supports, but it's the most important, if you ask me.
If your system is small, no big deal. Keep your headers full of code
and don't bother. We used to do that in Pascal, didn't we? Then
along came units, and suddenly we could split the development into
several relatively independent pieces... You can do it nowadays in
C++ with many modern compilers that support precompiled headers, of
course. But then again, once code changes you need to recompile
most of them again... Kind of PITA.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 23 '07 #2
* noone:
>
I've got a fella working for me who is trying to convince me to change our
coding standards to using separate .h and .cc files for definitions and
implementations . I know this is a perfectly valid way of doing things but
in my mind it is kind of quirky. Here's why.
Rather, having all code in header files is the unusual way.

The first reason I'd resist deals with the simplicity of makefiles. When
including most code from headers where classes are protected by #ifdef
blocks the makefile doesn't have to have a separate build rule for each
class.
Builds nowadays are mostly automated. Anyway, even with hand-crafted
make files you can lay down some general rules.

Next, TTBOMK you need to do your classes in header files if you are going
to make them into generic templates...wel l, putting them into headers
isn't strictly a requirement but the other restrictions for (templatizable
code) would encourage only using headers.
Even template code can benefit from a separation into header and
implementation. For most template classes that means including the
implementation file in the header file. The main advantage is that the
header file can give a very concise overview and defines the logical
interface, whereas with just one big file it's not that clear what's
interface and what's implementation detail. A possible but very
improbable advantage is that with compiler support for 'export' (only
Comeau right now, as far as I know) this can also reduce build times.

I understand that there a a couple of reasons why you might want separate
definitions and implementation, such as when a class has a static data
member
Can be done in header file via template trick.

>, or if you intend to create a shared object library of class code,
AFAICS that is irrelevant except for the headers the client code needs.

but it seems to me that putting the code into the header files is a bit
more elegant and the other way would be more of a holdover from the old
days before templates.

I'm wondering what comments the upper echelon lurkers might have. Do they
agree with my reasoning, or can they give me reasons why I should
reevaluate my assertions.
One reason to separate the two is that with all the code in headers, any
build of the system involves all of the code, which is slow (precompiled
headers have their own problems). And with all code in header files,
you force a remake of dependent modules when only the implementation is
changed, compounding that problem. A third reason to separate is to be
free to use any implementation techniques that suit the problem, instead
of doing very "clever" things and going amok with namespacing.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 23 '07 #3
On Mon, 22 Jan 2007 23:09:57 -0500, Victor Bazarov wrote:
C++ was designed with large scale systems in mind. That's not the only
paradigm it supports, but it's the most important, if you ask me. If your
system is small, no big deal. Keep your headers full of code and don't
bother. We used to do that in Pascal, didn't we? Then along came units,
and suddenly we could split the development into several relatively
independent pieces... You can do it nowadays in C++ with many modern
compilers that support precompiled headers, of course. But then again,
once code changes you need to recompile most of them again... Kind of
PITA.
Thanks for your comments. I do understand your point. Our systems
definitely fall into the category of many small embedded programs with a
well defined message passing API to communicate between the processes.

Thanks for the followup.

Jan 23 '07 #4
On Tue, 23 Jan 2007 05:29:55 +0100, Alf P. Steinbach wrote:

Hi.

I should probably clarify what I mean by "all the code"

I'm talking about utility classes. The individual program logic is of
course still in .cc files. I try to encourage class coding where the
methods are suitable for implicit inline compilation.

And as I mentioned to Victor, the projects are numerous small embedded
software component programs of less than 1000 lines including comments.

The utility classes are things like C++ wrappers around socket networking,
a joystick driver (which does use a separate .cc file only because it uses
signals and requires a static data member to point to the class instance
that is needed to register in the polling timer), message passing
wrappers. interfaces to kernel drivers, etc.

Your points are well taken. I think based on scale it is a tradeoff right
now. The whole unmanned system operating environment compiles in less
than two minutes on a single CPU Xeon machine.
Jan 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
17582
by: CMAR | last post by:
I have the following markup. The problem is that the browser, e.g., IE6, inserts several lines of blank space between the <div> and the following table. Is there a way to minimize that vertical spacing? Thanks, CMA <div class="vlgray">Condition</div> <table cellpadding="0" cellspacing="0">
6
1474
by: johnnieboy | last post by:
I am trying to compile the program hull http://cm.bell-labs.com/netlib/voronoi/hull.html This is quite a complicated program that involves several .c files I get several warnings during compilation and the error: hullmain.c:40: initializer element is not constant This is the offending line: FILE *INFILE, *OUTFILE, *DFILE = stderr, *TFILE;
5
1744
by: sparks | last post by:
I am about to pull my hair out on this one. its a class stack with push and pop but one time its trying to find a } at the end of the code the next it says that I need a } after private man I know I am kinda new but this is driving me up the wall .... Should I start over or what. I keep adding things trying to get it to work but no success.
8
3313
by: yossi.kreinin | last post by:
Hi! When are multiple definitions of global variables with the same name considered legal in C, and how is it different from C++? It appears that in terms of assembly language, some C definitions are translated to "weak" symbols, in which case multiple definitions are merged by the linker, while some become "normal" symbols, triggering linker errors in case they are defined more then once. Is it true that in C++, multiple definitions...
14
1576
by: ToddLMorgan | last post by:
Summary: How should multiple (related) projects be arranged (structured) and configured so that the following is possible: o Sharing common code (one of the projects would be a "common" project referenced by all others and likely the others would share at least the common project and possibly more as times goes on) o Clear separation of "production" code and "test" code (ie to readily ship source and test as separate components. Usually...
36
3859
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same program will result in a linker error complaining about multiple definitions. So this kind of definition should be avoided as much as possible. But as we know, the definition of a class is always in a header file. And we can use "#ifndef" to eliminate...
3
1448
by: sam_cit | last post by:
Hi Everyone, We all know that bit fields are to be used within a structure, like struct sample { int i:4; } However, is there any reason as to why it can only be specified only
10
3507
by: subramanian100in | last post by:
Suppose I declare a global variable int g; in two different files say a.c which has main() and b.c When I compile them to build an executable under gcc in Redhat Linux with the command gcc -std=c99 -pedantic -Wall -Wextra a.c b.c
11
2928
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is this possible.I thought it will throw "Variable redefinition Error". Giving the source code for reference.Please help me out on this... a.h ----- int g; void fun(void);
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10341
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9954
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.