Solved

Alternative to #ifdef ?

Posted on 2000-03-12
3
1,242 Views
Last Modified: 2009-12-16
I work on a program which has a lot of files made by other programmers. Thi program is used for few different types of Siemens terminals and that's why
someone thought to use #ifdef statements inside the sources and now the sources looks very ugly and hard to be followed. Probably these statements were overused. My question is if the statemets can be reordered in blocks or something in order to make the code easier for debugging? Is there another alternative ?
0
Comment
Question by:jorj
3 Comments
 
LVL 3

Accepted Solution

by:
ufolk123 earned 200 total points
ID: 2611353
Hi jori,
This is the common way for writing a multi-platform code.It is not always possible to group the code at macro level as it will create more redundancy.

e.g if a code has following lines
function f()
{
//processing...
/*here comes the system call which has diff sementics on diff systems
*/
#ifdef _UNIX
 Code ( Unix specific)
#else
 other platform
#endif

Now one option is to write the function f as
f()
{
#ifdef _UNIX

code for Unix

#else

code for other platforms

#endif

This way code is more readable but the redundancy is very high.
That is main reason of putting only the statement or code chunk level macro usage.

There is one more option if repetative group of statements are encountered in the code.

#define UNIX 0
#define WINDOWS 1

#define CHANGE_SOCK(x,other param)
     {
     switch(x)
     {
     case UNIX: ioctl(other param)
     case WINDOWS : ioctlsocket(other param)
  ....
}

These #defines can be in one .h file and you can use it to separate the platform dependent coding.But this is useful only
1) Difference in code is basically system call or logically grouped fixed code.
2) coding style is same i.e code flow is overall same for every platform.


Please get back for more clarifcation if required.

ufolk123

0
 

Expert Comment

by:jpjpjp
ID: 2611392
I dont really understand what you question is regarding the reordering,  but, if you would like to remove the #ifdefs, you could put your code under a source control system, and tag different version of it (one tag to see one version of the code, another one to see another version, etc..). I am only familiar with Clearcase as a source control system, and with Clearcase, you can set a view to see a certain version of the source. You could have your common code under the main branch (ie all view would see that code), and have the   code that is different in a branch, that way you dont have to maintain too many different version of the same code.

It you are stuck with all the #ifdefs, and you want to make sure that the defines are ok when u compile one version, you can use the #error prep directive, that will display an error and stop the compilation if you dont have the right defines.

If your program is not that critical to be real fast, you could rearrange the #ifdefs into conditional statements.
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 2613214
If C++ is an option for you you could use namespaces...
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now