Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do C developer prevent code duplication?

41 views
Skip to first unread message

Carfield Yim

unread,
Mar 17, 2010, 1:35:23 PM3/17/10
to
Hi all, I am coming from a Java/C++ background, in those so call OO
language, I can use polymorphism or inheritance and design pattern to
prevent code duplication. Now I start joining a new project which is
using C to develop, and feel I need to duplicate the code for same
case.

One example is when I need to implement the template pattern, like
http://en.wikipedia.org/wiki/Template_method_pattern

I guess there is some way for C developer to prevent that kind of code
duplication, is that right? May be code generation?? Any sample I can
find?
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Seebs

unread,
Mar 17, 2010, 1:44:58 PM3/17/10
to
On 2010-03-17, Carfield Yim <carf...@gmail.com> wrote:
> Hi all, I am coming from a Java/C++ background, in those so call OO
> language, I can use polymorphism or inheritance and design pattern to
> prevent code duplication. Now I start joining a new project which is
> using C to develop, and feel I need to duplicate the code for same
> case.

So don't.

> I guess there is some way for C developer to prevent that kind of code
> duplication, is that right? May be code generation?? Any sample I can
> find?

There's some cases where you really can't avoid duplicating code; after
all, templates are duplicated code, they're just hidden duplicated code.

I usually avoid code duplication by writing a function to perform any
common task. If I would find myself writing several versions of the same
function, that's usually a sign that I've done something wrong -- I'm
making the wrong function, perhaps, and I should define it differently.

It's hard to be more specific without concrete examples.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Ersek, Laszlo

unread,
Mar 17, 2010, 6:05:21 PM3/17/10
to
From: Carfield Yim <carf...@gmail.com>
Date: Wed, 17 Mar 2010 12:35:23 -0500 (CDT)
Message-ID: <clcm-2010...@plethora.net>

> Hi all, I am coming from a Java/C++ background, in those so call OO
> language, I can use polymorphism or inheritance and design pattern to
> prevent code duplication. Now I start joining a new project which is
> using C to develop, and feel I need to duplicate the code for same
> case.
>

> One example is when I need to implement the template pattern, like
> http://en.wikipedia.org/wiki/Template_method_pattern

You can do this in C with function pointers. Proceeding with the example
in Wikipedia:


---- game.h ----

/* "Base class" for game states. */
struct game
{
unsigned players_count;
};


/* This structure collects the "abstract methods", ie. game rules. */
struct game_methods
{
void (*init)(struct game *game);
void (*make_play)(struct game *game, unsigned player);
int (*eog)(const struct game *game);
void (*print_winner)(const struct game *game);
};


/* Declare "template method". */
void
play_one_game(struct game *game, unsigned players_count,
const struct game_methods *methods);


---- game.c ----

#include "game.h" /* bring struct defs into scope */

/* Implement "template method". */
void
play_one_game(struct game *game, unsigned players_count,
const struct game_methods *methods)
{
unsigned player;

game->players_count = players_count;
(*methods->init)(game);
player = 0u;
while (!(*methods->eog)(game)) {
(*methods->make_play)(game, player);
player = (player + 1u) % players_count;
}
(*methods->print_winner)(game);
}


---- monopoly.h ----

#include "game.h" /* bring "base class" into scope */

/* Game state for Monopoly. */
struct monopoly
{
struct game base; /* must be first member */
/* ... additional members for Monopoly game state */
};


/* Declare list of methods for Monopoly. */
extern const struct game_methods monopoly_methods;


---- monopoly.c ----

#include "monopoly.h"

/* Implement individual game methods for Monopoly. */

static void
init(struct game *game)
{
struct monopoly *m = (struct monopoly *)game;

/* access game state like "m->..." */
}

static void
make_play(struct game *game, unsigned player)
{
/* same here */
}


static int
eog(const struct game *game)
{
/* same here */
int finished = 0;
/* ... */
return finished;
}

static void
print_winner(const struct game *game)
{
/* same here */
}


/* Define method table for Monopoly. */
const struct game_methods monopoly_methods = {
&init, &make_play, &eog, &print_winner
};


---- main.c ----

#include "monopoly.h" /* we'll play Monopoly */

int
main(void)
{
struct monopoly monopoly; /* game state */

/*
call template method with the allocated game state and the according
game rules
*/
play_one_game(&monopoly.base, 5u, &monopoly_methods);

return 0;
}


--------

Usually, one would have a pointer embedded in the base class (here "struct
game") that points to the function table. This pointer would be set to the
correct structure of function pointers with static storage duration by
"constructors", so that it wouldn't be necessary to specify the "methods"
each time a "template method" is called.

See also

X-News: ludens comp.lang.c:815763
From: la...@ludens.elte.hu (Ersek, Laszlo)
Subject: Re: Access to common field in structs
Date: 27 Feb 2010 00:55:11 +0100
Message-ID: <TQDJXwuFBlNP@ludens>

or

http://groups.google.com/group/comp.lang.c/browse_thread/thread/41a1aee54949480d/55bbc21905c53e99#55bbc21905c53e99


HTH,
lacos

Marco

unread,
Mar 19, 2010, 1:11:02 PM3/19/10
to
On Mar 17, 10:35 am, Carfield Yim <carfi...@gmail.com> wrote:
> Hi all, I am coming from a Java/C++ background, in those so call OO
> language, I can use polymorphism or inheritance and design pattern to
> prevent code duplication. Now I start joining a new project which is
> using C to develop, and feel I need to duplicate the code for same
> case.
>
> One example is when I need to implement the template pattern, likehttp://en.wikipedia.org/wiki/Template_method_pattern

>
> I guess there is some way for C developer to prevent that kind of code
> duplication, is that right? May be code generation?? Any sample I can
> find?

you could use macros to "generate" code
not really my cup of tea

spinoza1111

unread,
Apr 2, 2010, 2:35:40 AM4/2/10
to
On Mar 18, 1:35 am, Carfield Yim <carfi...@gmail.com> wrote:
> Hi all, I am coming from a Java/C++ background, in those so call OO
> language, I can use polymorphism or inheritance and design pattern to
> prevent code duplication. Now I start joining a new project which is
> using C to develop, and feel I need to duplicate the code for same
> case.

There are only three methods in C to avoid code duplication:

* Use the preprocessor
* Use the inline directive
* Create new functions

But I suggest that you try to persuade management to use Java unless
you are writing OS code.

>
> One example is when I need to implement the template pattern, likehttp://en.wikipedia.org/wiki/Template_method_pattern


>
> I guess there is some way for C developer to prevent that kind of code
> duplication, is that right? May be code generation?? Any sample I can
> find?
> --

> comp.lang.c.moderated - moderation address: c...@plethora.net -- you must

spinoza1111

unread,
Apr 2, 2010, 2:35:55 AM4/2/10
to
On Mar 18, 6:05 am, "Ersek, Laszlo" <la...@caesar.elte.hu> wrote:
> From: Carfield Yim <carfi...@gmail.com>
> http://groups.google.com/group/comp.lang.c/browse_thread/thread/41a1a...
>
> HTH,
> lacos
> --
> comp.lang.c.moderated - moderation address: c...@plethora.net -- you must

> have an appropriate newsgroups line in your header for your mail to be seen,
> or the newsgroup name in square brackets in the subject line.  Sorry.

I should have mentioned function pointers. OK, four ways.

spinoza1111

unread,
Apr 2, 2010, 12:03:08 PM4/2/10
to
On Mar 20, 1:11 am, Marco <prenom_no...@yahoo.com> wrote:
> On Mar 17, 10:35 am, Carfield Yim <carfi...@gmail.com> wrote:
>
> > Hi all, I am coming from a Java/C++ background, in those so call OO
> > language, I can use polymorphism or inheritance and design pattern to
> > prevent code duplication. Now I start joining a new project which is
> > using C to develop, and feel I need to duplicate the code for same
> > case.
>
> > One example is when I need to implement the template pattern, likehttp://en.wikipedia.org/wiki/Template_method_pattern
>
> > I guess there is some way for C developer to prevent that kind of code
> > duplication, is that right? May be code generation?? Any sample I can
> > find?
>
>  you could use macros to "generate" code
>  not really my cup of tea

Must be done in a disciplined fashion. The formal parameters in the
text of the macro should always be in parentheses as in this example:

#define A(B,C) (C)=(B)*2

because suppose this example is used instead:

#define A(B,C) C=B*2

and A(b+8, c) is the macro call. Then you get

C=b+8*2

which isn't what you want.

More generally, go back to OO. The preprocessor tried to address many
of the issues that were being already better handled in Simula owing
to the fact that Danish labor unions were entitled to control
management's foolish installation of industrial systems, but there are
so many ways of expressing an "object" in non-OO code that using the
preprocessor is trying to put out a fire by pissing on it.

> --
> comp.lang.c.moderated - moderation address: c...@plethora.net -- you must

Ralf Damaschke

unread,
Apr 3, 2010, 1:17:42 PM4/3/10
to
spinoza1111 wrote:

> On Mar 20, 1:11�am, Marco <prenom_no...@yahoo.com> wrote:
>> �you could use macros to "generate" code
>> �not really my cup of tea


>
> Must be done in a disciplined fashion. The formal parameters in the
> text of the macro should always be in parentheses as in this example:
>
> #define A(B,C) (C)=(B)*2

That's not enough discipline.
Consider the code fragment "if (A(b+8, c) > 0) {...}".

-- Ralf

Seebs

unread,
Apr 3, 2010, 3:46:56 PM4/3/10
to
On 2010-04-03, Ralf Damaschke <rws...@gmx.de> wrote:

> spinoza1111 wrote:
>> Must be done in a disciplined fashion. The formal parameters in the
>> text of the macro should always be in parentheses as in this example:

>> #define A(B,C) (C)=(B)*2

> That's not enough discipline.
> Consider the code fragment "if (A(b+8, c) > 0) {...}".

#define SIX 1 + 5
#define NINE 8 + 1

printf("What do you get if you multiply %d by %d? %d.\n",
SIX, NINE, SIX * NINE);

(Not original with me, by any means.)

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

0 new messages