Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > what is the best way

Reply
Thread Tools

what is the best way

 
 
Malcolm McLean
Guest
Posts: n/a
 
      11-28-2012
On Wednesday, November 28, 2012 12:21:24 AM UTC, Les Cargill wrote:
> Jorgen Grahn wrote:
>
> > Yes -- that seems highly unusual and impractical. E.g. anyone who
> > wants to use the interface provided by foo.h will be poisoned by
> > everything needed to implement foo.c!

>
>
> I've never found this to be a significant burden, but I do spend
> some time in trying to make interfaces as clean as possible.
>
>

The problem is that whizzbanggui.c contains the typedef of a struct point.
You're implementing pythagoras, which takes two points. So naturally they
get passed as struct points. But now everyone has to pull in the whole of
whizzbanggui, just to use that trivial definition.
If you define your own struct Point / struct myPoint, then you need adapter
code to convert between two identical structures when calling pythagoras
and whizzbanggui from the same program. So that's not a good solution either.

 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      11-28-2012
On Wednesday, November 28, 2012 2:21:50 AM UTC, Ian Collins wrote:
> Les Cargill wrote:
>
> > I dunno - at least conceptually, it's simpler for me if I hold
> > to a separately compiled module being roughly a "class", where the
> > header is the "interface".

>
>
>
> Ah, I see a different in metaphor here.
>
> I would describe the collection of source files (typically in their own
> directory with a makefile) used to implement the interface declared in a
> header as a module. You appear to be describing an individual source
> file as a module.
>

I try to keep one module per file.

So, for example, lets say I'm writing a string-intensive program that does a
lot of counting of characters. I might have one module that does DNA
sequences, and another that does protein sequences.
So they'll both have

/* count the number of times ch appears in str */
static int countchars(char *str, int ch)

defined in the source file, purely to keep modules as one file, depending
on nothing else.

But if a file becomes too complex, I will reluctantly split it.
--
Visit Malcolm's homepage
http://www.malcolmmclean.site11.com/www

 
Reply With Quote
 
 
 
 
Les Cargill
Guest
Posts: n/a
 
      11-28-2012
Malcolm McLean wrote:
> On Wednesday, November 28, 2012 12:21:24 AM UTC, Les Cargill wrote:
>> Jorgen Grahn wrote:
>>
>>> Yes -- that seems highly unusual and impractical. E.g. anyone who
>>> wants to use the interface provided by foo.h will be poisoned by
>>> everything needed to implement foo.c!

>>
>>
>> I've never found this to be a significant burden, but I do spend
>> some time in trying to make interfaces as clean as possible.
>>
>>

> The problem is that whizzbanggui.c contains the typedef of a struct point.


Uh oh.

> You're implementing pythagoras, which takes two points. So naturally they
> get passed as struct points. But now everyone has to pull in the whole of
> whizzbanggui, just to use that trivial definition.


There's an entire ... discipline? in the art of stacking boxes
on top of other boxes so that everything is covered. Bad as I hate to
admit it, to my ear, a "point" is an object and unless it has
exactly one client/user, it has to be published in a reasonable manner.

Maybe you have a "primiTypes.h". Maybe you have "point.h".

> If you define your own struct Point / struct myPoint, then you need adapter
> code to convert between two identical structures when calling pythagoras
> and whizzbanggui from the same program. So that's not a good solution either.
>


Or you could make "point" either completely independent in the two, or
make "point" a standalone object, or....


--
Les Cargill
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-28-2012
Malcolm McLean wrote:
> On Wednesday, November 28, 2012 2:21:50 AM UTC, Ian Collins wrote:
>> Les Cargill wrote:
>>
>>> I dunno - at least conceptually, it's simpler for me if I hold
>>> to a separately compiled module being roughly a "class", where the
>>> header is the "interface".

>>
>> Ah, I see a different in metaphor here.
>>
>> I would describe the collection of source files (typically in their own
>> directory with a makefile) used to implement the interface declared in a
>> header as a module. You appear to be describing an individual source
>> file as a module.
>>

> I try to keep one module per file.
>
> So, for example, lets say I'm writing a string-intensive program that does a
> lot of counting of characters. I might have one module that does DNA
> sequences, and another that does protein sequences.
> So they'll both have
>
> /* count the number of times ch appears in str */
> static int countchars(char *str, int ch)


You forgot the const

> defined in the source file, purely to keep modules as one file, depending
> on nothing else.


That looks like code duplication for the sake of a doctrine. This
sounds like a text book example of when to use a shared library.

> But if a file becomes too complex, I will reluctantly split it.


That depends on your definition of "too complex". Another good reason
for splitting is "too slow": when a monolithic file takes longer to
compile than the equivalent set of smaller files.

--
Ian Collins
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      11-29-2012
On Wednesday, November 28, 2012 7:30:21 PM UTC, Ian Collins wrote:
> Malcolm McLean wrote:
>
> That looks like code duplication for the sake of a doctrine. This
> sounds like a text book example of when to use a shared library.
>

It's not doctrine for the sake of doctrine. It's so I can take the file
dna.c, and have a list of functions that operate on dna sequences, which I
can use for another program. I can do the same with proteins.c. I can
take both files or one file, and they won't interact with each other.

--
Visit Malcolm's website
http://www.malcolmmclean.site11.com/www
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-29-2012
Malcolm McLean wrote:
> On Wednesday, November 28, 2012 7:30:21 PM UTC, Ian Collins wrote:
>> Malcolm McLean wrote:
>>
>> That looks like code duplication for the sake of a doctrine. This
>> sounds like a text book example of when to use a shared library.
>>

> It's not doctrine for the sake of doctrine. It's so I can take the file
> dna.c, and have a list of functions that operate on dna sequences, which I
> can use for another program. I can do the same with proteins.c. I can
> take both files or one file, and they won't interact with each other.


Or you could simply build a (shared) library.

--
Ian Collins
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      11-29-2012
On Thursday, November 29, 2012 6:37:40 PM UTC, Ian Collins wrote:
> Malcolm McLean wrote:
>
> > It's not doctrine for the sake of doctrine. It's so I can take the file
> > dna.c, and have a list of functions that operate on dna sequences, which I
> > can use for another program. I can do the same with proteins.c. I can
> > take both files or one file, and they won't interact with each other.

>
> Or you could simply build a (shared) library.
>

More trouble than it's worth.
You want to be able to distribute the source. The receiver can then compile
it on his own system, hopefully just by typing cc *.c or an equivalent. He
doesn't want to have to mess about with libraries.

--
Visit Malcolm's website
http://www.malcolmmclean.site11.com/www

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to loop through ArrayList and remove elements on the way? Kevin Java 16 01-30-2008 08:54 PM
way way way OT: MCNGP Announcement Neil MCSE 174 04-17-2006 05:55 PM
Any way/Best way to simulate 3:2 mode on an S2 IS? Joe Digital Photography 0 03-09-2006 05:35 PM
AMD Opteron: 1-way, 2-way, ... Up to 8-way. John John Windows 64bit 12 12-27-2005 08:17 AM
What is the best way to make dual-way xml databinding? Stan ASP .Net 3 05-05-2005 02:07 PM



Advertisments