473,569 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#ifdef vs OO patterns

Hi. In our domain, we have to vary a lot of code based on a radar type
(of which there are 3--for now). The legacy code acheives this by
heavy use of #ifdef macros sprinkled throughout the code. We now have
an opportunity to redesign a lot of this code. I am recommending that
instead of the macro approach, we use polymorphism-based design
options, such as the strategy, template method and abstract factory
patterns. To me, this just seems intuitively like a better approach.
However, I want to be able to articulate why this a better approach, so
I am prepared to respond to those who will recommend continuing with
the macro approach.

Could someone articulate some reasons or cite articles/URLs that
support the polymorphism/pattern approach over the macro approach?

Thanks in advance!

Ken

Dec 16 '05 #1
8 2493
kk_...@yahoo.co m wrote:
Hi. In our domain, we have to vary a lot of code based on a radar type
(of which there are 3--for now). The legacy code acheives this by
heavy use of #ifdef macros sprinkled throughout the code. We now have
an opportunity to redesign a lot of this code. I am recommending that
instead of the macro approach, we use polymorphism-based design
options, such as the strategy, template method and abstract factory
patterns. To me, this just seems intuitively like a better approach.
However, I want to be able to articulate why this a better approach, so
I am prepared to respond to those who will recommend continuing with
the macro approach.

Could someone articulate some reasons or cite articles/URLs that
support the polymorphism/pattern approach over the macro approach?


in my experience maintaining code that is crawling with #ifdefs is
painful
in the extreme. How many places would you have to change the code in
if you added a 4th radar type? It could run into hundreds or thousands.

The preprocessor is generally bad news.

This might be interesting:-
http://www.objectmentor.com/resources/articles/ocp.pdf

You could also try asking your question comp.object

Be careful your current code may be quite efficeint in memory usage
(only
code needed is included in a particuar build). Naive application of
polymorphism may cause your executable to get much larger.
--
Nick Keighley

Dec 16 '05 #2
kk****@yahoo.co m wrote:
Hi. In our domain, we have to vary a lot of code based on a radar type
(of which there are 3--for now). The legacy code acheives this by
heavy use of #ifdef macros sprinkled throughout the code. We now have
an opportunity to redesign a lot of this code. I am recommending that
instead of the macro approach, we use polymorphism-based design
options, such as the strategy, template method and abstract factory
patterns. To me, this just seems intuitively like a better approach.
However, I want to be able to articulate why this a better approach, so
I am prepared to respond to those who will recommend continuing with
the macro approach.

Could someone articulate some reasons or cite articles/URLs that
support the polymorphism/pattern approach over the macro approach?

Thanks in advance!

Ken


Your approach may be a great idea when starting with a new project, or
a project that is in it's early stages.
However, it's very problematic to try to convert an existing well
season project to a different format.
You usaully end up with many bugs, and problems that are hard to
forcast.

Dec 16 '05 #3
kk_oop wrote:
Hi. In our domain, we have to vary a lot of code based on a radar type
(of which there are 3--for now). The legacy code acheives this by
heavy use of #ifdef macros sprinkled throughout the code. We now have
an opportunity to redesign a lot of this code. I am recommending that
instead of the macro approach, we use polymorphism-based design
options, such as the strategy, template method and abstract factory
patterns. To me, this just seems intuitively like a better approach.
However, I want to be able to articulate why this a better approach, so
I am prepared to respond to those who will recommend continuing with
the macro approach.

Could someone articulate some reasons or cite articles/URLs that
support the polymorphism/pattern approach over the macro approach?


We don't need to cite anything - preprocessor abuse is always wrong. All the
C++ tutorials will tell you that.

If you had lots of unit tests, you could change from one design to the other
incrementally, one ifdef at a time, while improving other design aspects and
improving the tests.

Read /Working Effectively with Legacy Code/ by Mike Feathers.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Dec 16 '05 #4
Axter wrote:
kk****@yahoo.co m wrote:
Hi. In our domain, we have to vary a lot of code based on a radar type
(of which there are 3--for now). The legacy code acheives this by
heavy use of #ifdef macros sprinkled throughout the code. We now have
an opportunity to redesign a lot of this code. I am recommending that
instead of the macro approach, we use polymorphism-based design
options, such as the strategy, template method and abstract factory
patterns. To me, this just seems intuitively like a better approach.
However, I want to be able to articulate why this a better approach, so
I am prepared to respond to those who will recommend continuing with
the macro approach.

Could someone articulate some reasons or cite articles/URLs that
support the polymorphism/pattern approach over the macro approach?

Thanks in advance!

Ken


Your approach may be a great idea when starting with a new project, or
a project that is in it's early stages.
However, it's very problematic to try to convert an existing well
season project to a different format.
You usaully end up with many bugs, and problems that are hard to
forcast.


But, if the existing product "works" but is difficult to maintain and
extend, then refactoring to patterns may actually reduce the overall
bug rate. The OP might be interested in _Refactoring to Patterns_ by
Joshua Kerievsky.

Cheers! --M

Dec 16 '05 #5
kk****@yahoo.co m wrote:
Hi. In our domain, we have to vary a lot of code based on a radar type
(of which there are 3--for now). The legacy code acheives this by
heavy use of #ifdef macros sprinkled throughout the code. We now have
an opportunity to redesign a lot of this code. I am recommending that
instead of the macro approach, we use polymorphism-based design
options, such as the strategy, template method and abstract factory
patterns. To me, this just seems intuitively like a better approach.
However, I want to be able to articulate why this a better approach, so
I am prepared to respond to those who will recommend continuing with
the macro approach.

Could someone articulate some reasons or cite articles/URLs that
support the polymorphism/pattern approach over the macro approach?

Thanks in advance!

Ken


Patterns can be very handy, but if the programming team is not
proficient with C++ and an OO-based design, they can actually impede
productivity. (Of course training can help with those things.) As the
FAQ argues, the choice of language (and we could add design
methodology) depends more on business concerns than it does on what is
"best":

http://www.parashift.com/c++-faq-lit...e.html#faq-6.5

That being said, you might want to ask this question on comp.object,
comp.software.p atterns, and comp.software-eng for some additional
insight since this forum is more for discussions of the C++ language
proper rather than general design.

As for the preprocessor, our official position is that it is evil and
should be avoided:

http://www.parashift.com/c++-faq-lit....html#faq-29.8

It sounds like your existing software follows bad programming practice
and is thus likely fragile and hard to maintain and extend, but whether
you move to design patterns or not, I would strongly recommend ridding
your code of #ifdefs (except in the few instances where they are
necessary, such as header file include guards).

Cheers! --M

Dec 16 '05 #6
Nick Keighley wrote:
[snip]
Be careful your current code may be quite efficeint in memory usage
(only
code needed is included in a particuar build). Naive application of
polymorphism may cause your executable to get much larger.


Agreed, but since the currently #ifdef'ed parts aren't needed anyway,
if properly factored into patterns, they could be excluded at the
project/makefile level to keep about the same size. As you note,
however, such an implementation is not usually the simplest to achieve.

Cheers! --M

Dec 16 '05 #7

Phlip wrote:
kk_oop wrote:

Read /Working Effectively with Legacy Code/ by Mike Feathers.


Great book. Have it, love it, use it frequently. I didn't see where
that addresses the #ifdef issue, though.

- Ken

Dec 16 '05 #8
kk_oop wrote:
Read /Working Effectively with Legacy Code/ by Mike Feathers.


Great book. Have it, love it, use it frequently. I didn't see where
that addresses the #ifdef issue, though.


You guys have a latent OO pattern of a common Radar abstraction and
three concrete Radar implementations .

However, you don't use OO to run the pattern. You use conditional
compilation to produce three separate programs.

This is so wrong you won't find a citation for how wrong it is.
Someone, early in this code's history, decided that preprocessor abuse
would somehow be cleaner, or more elegant, or more efficient, than
writing one program that handles three kinds of radar.

The concept you generally need is called "Software Product Lines".
Google for that, and you will find some advice about conditional
compilation, and much advice about OO abstractions.

To get where you need to go, you must either refactor (via unit tests),
or rewrite (also via unit tests). So you need Feather's book.

If you hope to allay your team's fears about refactoring, you can't do
it by citing anything saying OO is superior to preprocessor abuse. You
should do it by coaching code cleanup and refactoring. Then you should
introduce just a few abstractions that allow just a few #ifdefs to go
away. That will turn the tide against the rest.

Dec 16 '05 #9

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

Similar topics

2
2554
by: Design Pattern Catalog | last post by:
Thank you for your interest in "Design Patterns: Elements of Reusable Object-Oriented Design", by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. This message answers several frequently asked questions. If you thought you were asking for the source code, you must have made a mistake. Please try again! The "pattern home...
5
5766
by: lovecreatesbeauty | last post by:
Do #ifdef or #ifndef have some defects? I ever heard that some people use #if defined() or #if !defined() instead of using #ifdef or #ifndef in header file.
6
74322
by: Michael B Allen | last post by:
Which is the preferred method for preprocessor tests and why? #ifdef XYZ or #if XYZ or #if defined(XYZ) and
13
6535
by: John Salerno | last post by:
Here are a few I'm considering: Design Patterns Explained : A New Perspective on Object-Oriented Design (2nd Edition) (Software Patterns Series) by Alan Shalloway Design Patterns C# by Steven John Metsker Design Patterns by Erich Gamma Head First Design Patterns by Elisabeth Freeman
12
1889
by: Jeff | last post by:
I'm just getting up to speed on OOP patterns (e.g, MVC) and I'm wondering how closely they are followed out in the real world. Do those of you who use them try to follow them as closely as possible and deviate only as necessary? Or do you only generally follow them; mix-n-match as necessary? Just wondering what I should be looking to...
1
3205
by: Michael Sgier | last post by:
Hi I get the error: No case-independent string comparison (stricmp, strcasecmp) with the code below. Why...where should stricmp be defined? And how do i get rid of the error on Linux? // // Function portability // #ifndef HAVE_STRICMP
6
27785
by: anirbid.banerjee | last post by:
Hi, I need to write a macro which would have a lot many conditional #ifdef ... #endif blocks in it. #define _xx_macro (x) { ... \ ... \ /* some code (); */ #ifdef _SOME_STMT \ ... \ ... \
10
3517
by: David W | last post by:
Hello, In a C++ MFC application I need to conditionally #include one of two additional resource files in my main resource file because different forms of the application have different names. I would also like to edit the main resource file with the IDE's resource editor and save it without losing the conditional #includes of the...
7
3097
by: =?Utf-8?B?bWF2cmlja18xMDE=?= | last post by:
Hi, I would like to know more about design patterns and specifically using C#. Can any one recommend a good book? Thanks
0
7695
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...
0
7612
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...
0
7922
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. ...
0
7964
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...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
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...

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.