473,394 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

assertion in header

I firmly believe that it is always a bad idea to put code in a header
file. Nothing pisses me off more than seeing function definitions in a
..h, and I recently was truly blessed :) to witness code that actually
contained #include "foo.c" in a header. However, I am eliciting
responses to the thought of putting assertions in a header file. I am
dealing with a really ugly, really poorly done code base, and I find
myself often thinking "wtf? Does this mean what I think it does?" In
such cases, I usually like to trow in an assertion to confirm things.
My aesthetic sense tells to clean out all of the header files, my
realistic sense tells me to dump the whole thing and start over, but my
practical sense tells me that either of those solutions will take
months. So I'm putting assertions into header files, and it makes me
feel dirty. Am I compounding an already horrible situation? When
possible, I use:
#if FOO != BAZ
#error
#endif
but in a case where the header already has run-time code, I need to use
an assertion. Believe me, I would love to simply pull the code out of
the headers, but the build environment is so phenomenally convoluted
that changing the file structure is a major headache. I suppose that
in this situation, aesthetics and good style are totally pointless
concerns, but I still have some dignity.

So, what do you think. Is it acceptable to put an assertion into a
header file?

Nov 15 '05 #1
3 1743
bill wrote:
I firmly believe that it is always a bad idea to put code in a header
file.
Depends what you mean by 'code'. Empty headers are pretty pointless.

[If you're entering thousands of enum identifiers by hand, and you're
paid per line of code, and enums aren't considered code, ... then you
should get another job. ;-]
Nothing pisses me off more than seeing function definitions in a
.h, and I recently was truly blessed :) to witness code that actually
contained #include "foo.c" in a header.
How is this 'better' than having the code directly in the header?
However, I am eliciting responses to the thought of putting assertions
in a header file. ... I usually like to trow in an assertion to confirm
things.
My aesthetic sense tells to clean out all of the header files, my
realistic sense tells me to dump the whole thing and start over, but
my practical sense tells me that either of those solutions will take
months. So I'm putting assertions into header files, and it makes me
feel dirty. Am I compounding an already horrible situation? When
possible, I use:
#if FOO != BAZ
#error
#endif
How does this relate to function definitions in headers?
but in a case where the header already has run-time code, I need to
use an assertion.
How? If you make inclusions of those headers errors, then you're
just screwing your entire build.

Are you suggesting that (true) source (.c) files have something like...

#define INCLUDE_HEADER_SOURCE 1

....and your header functions are wrapped with #if/endif?
Believe me, I would love to simply pull the code out of the headers,
but the build environment is so phenomenally convoluted that changing
the file structure is a major headache.
I don't really see why you can't change the function definitions into
declarations only, and move the source into a single extra translation
unit, however, if the headers have header guards, just change the
functions to static scope. [That will magnify your executable size,
but it's the laziest option available.]
I suppose that in this situation, aesthetics and good style are
totally pointless concerns, but I still have some dignity.
Depends what price you value your dignity. If it's your name on the
code, and you're asking other people to continue the maintenance, then
not doing it right is just false lazyness.
So, what do you think. Is it acceptable to put an assertion into a
header file?


Perfectly, but you haven't really explained how you will apply your
'assertions'. Perhaps you could supply an shortened sample of what
you are really facing.

--
Peter

Nov 15 '05 #2
Pigpen wrote:
I firmly believe that
it is always a bad idea to put code in a header file.
Nothing pisses me off more than seeing function definitions in a .h
and I recently was truly blessed :) to witness code
that actually contained #include "foo.c" in a header.
You are confused.
All that is meant by the term "header file" is that
it is meant to be included somewhere near the top (head)
of a source file or another header file.

Header files are commonly used to introduce a *module interface*
into a translation unit. An interface has no substance.
This means that it should *not* contain any definitions
that, by themselves, cause the compiler to emit code or reserve memory.
Any such definitions should be sequestered in a translation unit
which can be compiled separately and linked into the program.

Good C programmers often *do* use the C preprocessor to include
files that contain external function and variable definitions
where they are required in the translation unit.
They probably should *not* call them header files
because other C programmers may think that
they contain a module interface.
However, I am eliciting responses
to the thought of putting assertions in a header file.
I am dealing with a really ugly, really poorly done code base
and I find myself often thinking,
"Wtf? Does this mean what I think it does?"
In such cases, I usually like to throw in an assertion to confirm things.
My aesthetic sense tells to clean out all of the header files.
My realistic sense tells me to dump the whole thing and start over.
But my practical sense tells me that
either of those solutions will take months.
Does this code belong to you now?
Are you responsible for maintaining and extending the code?
If it belongs to someone else,
you should try to get them to fix and maintain it.
If it belongs to you,
you need to make a plan to fix it no matter how long it takes.
So I'm putting assertions into header files and it makes me feel dirty.
A cold shower might help.
Am I compounding an already horrible situation?
When possible, I use:

#if FOO != BAZ
#error
#endif

but, in a case where the header already has run-time code,
I need to use an assertion.
Believe me, I would love to simply pull the code out of the headers
but the build environment is so phenomenally convoluted
that changing the file structure is a major headache.
I suppose that, in this situation,
aesthetics and good style are totally pointless concerns.
but I still have some dignity.

So, what do you think.
Is it acceptable to put an assertion into a header file?


I can only assume that you mean the assert macro.
Whether they appear in a header file or not is irrelevant.
Any assert macros must appear in the scope of a function definition.
They are used to trap programming errors at run-time while testing.
They disappear when you define the NDEBUG macro and recompile
just before you deploy your code.

Nov 15 '05 #3
bill <bi**********@gmail.com> a écrit*:
but in a case where the header already has run-time code, I need to use
an assertion.


I do not understand what you mean with "run-time code in header".
Could you please gives an example ?

Marc Boyer
Nov 15 '05 #4

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

Similar topics

3
by: Todd Miller | last post by:
Hi, I recently discovered an assertion failure in the Python garbage collection system when scripts using our C extension (numarray) exit. The assertion is activated for Pythons configured using...
4
by: A_StClaire_ | last post by:
I read a section of my text on smart "counting" pointers and found it confusing so I decided to get hands-on. however I'm getting "Debug Assertion Failed... Expression:...
4
by: ellieong | last post by:
When i try to run my program, i get this error "debug assertion failed!" and it's caused by this file called "afxtempl.h" at line 262. isn't this header file provided by c++? so how can i get rid...
1
by: Timur Safin | last post by:
Hi All, Sorry if it is offtopic here, I wasn't able to find any more relevant group... I'm slowly approaching AMD64 build for our product (as my personal fun project). And after I ran that...
5
by: Ron Louzon | last post by:
I have some C++ code that uses the CSingleLock( CCriticalSection *) constructor. In visual C++ 6.0, this code compiles and runs fine in both Debug and release modes. However, in Visual Studio...
2
by: karspy | last post by:
In my COM Server i have added Dialog Box. i have registered OCX file and added that Activex Control to my Project also. It has created 1 header file and 1 source file. After that i have created...
0
by: Rameez Sajwani | last post by:
I made a web service and have implemented usernameForCertificateSecurity assertion in it. If I put both the webservice and client on same PC , then its running absolutely fine. But when i tried to...
6
by: Eric | last post by:
is it possible to catch an assertion error? that is, could the following block of code be made to run? assert (false); catch (...) {}
0
Airslash
by: Airslash | last post by:
Hi, currently working with this component to build a quick testapplication for a network camera and motion detection. The only problem I currently encounter, is that when I call : mTcpClient...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.