473,396 Members | 1,755 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,396 software developers and data experts.

#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 2483
kk_...@yahoo.com 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.com 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.com 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.com 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.patterns, 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
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...
5
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
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
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...
12
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...
1
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? // //...
6
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
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...
7
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
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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...
0
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...
0
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,...

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.