Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #103761

Re: Private and public code

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.lang.c
Subject Re: Private and public code
Date 2017-02-12 20:58 +0000
Organization Fix this later
Message-ID <o7qi67$ded$1@dont-email.me> (permalink)
References <20170212200353.005eb833d8a8d4e8e6d8f3de@gmail.com> <egbq9cFbmb2U3@mid.individual.net> <20170212213845.24b28c8a15ee4e41cfe2e07f@gmail.com>

Show all headers | View raw


On 12/02/17 20:38, GOTHIER Nathan wrote:
> On Mon, 13 Feb 2017 08:10:36 +1300 Ian Collins <ian-news@hotmail.com>
> wrote:
>
>> On 02/13/17 08:03 AM, GOTHIER Nathan wrote:
>>
>> Please limit your line length!
>>
>>> Hello everybody,
>>>
>>> After reading many headers in different C projects, I realized
>>> the fact that there are many private code (macros, typedefs,
>>> ...) included in headers that makes difficult to distinguish what
>>> is useful for the end user.
>>>
>>> Since I consider headers as some kind of minimal documentation
>>> for C programmers, I put every private code in source files and
>>> only public code in headers. Although it could lead sometimes to
>>> put all unrelated private code in one common source file with an
>>> include guard.
>>>
>>> What do you think about this practice?
>>
>> Why don't you just use a private header?
>>
>> -- Ian
>
> Do you compile private headers? For me appending "private" to
> "header" looks weird... since we usually make public access to
> private code with headers. ;-)

I use a private header whenever I wish to share information between two 
(or more) source files in a library but don't wish to share that same 
information with the user of the library. I don't particularly consider 
"private header" to look weird. Perhaps it's because you have accustomed 
yourself to thinking of headers as the "public interface" of a source 
file? That's understandable, but there are various degrees of "public".

Proof by analogy is fraud, but /illustration/ by analogy is valid. So 
here's an analogy for you. Think of a header as being a little like an 
HTML document. We normally think of HTML documents as being part of the 
World Wide Web, and so they seem to us to be "public" information. But 
many companies nowadays have internal Web sites on their intranets, and 
they use those sites to share information amongst their various 
departments, but they don't share them with the wider world.

Similarly, headers can be shared amongst source files in a module, 
without necessarily being published as part of the binary distribution.

This is particularly useful when "opaque types" are being used. An 
opaque type can be created by publishing a struct type declaration in a 
header without publishing the struct itself, often (but not necessarily) 
using a typedef, such as: typedef struct Bignum_ Bignum;

This isn't enough information for a source to define a Bignum object, 
but it /is/ enough information to allow the source to define a pointer 
to such an object. It can then receive such a pointer from a call to a 
function that creates these objects, and it can pass it to functions 
that can receive such pointers, without ever being aware of what it's 
actually pointing to.

Of course, the type must be defined /somewhere/. And if the module that 
manages the type can all be implemented in one source file, that's fine, 
but maybe for reasons of code organisation it is considered better to 
split that source into several files. In this case, they will all need 
to see the type definition, without revealing it to "user" code. The 
solution is to use a private header - one that is #included into all the 
sources of that module, but is not available to "user" source files.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Private and public code GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-12 20:03 +0100
  Re: Private and public code Ian Collins <ian-news@hotmail.com> - 2017-02-13 08:10 +1300
    Re: Private and public code GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-12 21:38 +0100
      Re: Private and public code Richard Heathfield <rjh@cpax.org.uk> - 2017-02-12 20:58 +0000
        Re: Private and public code GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-12 23:06 +0100
          Re: Private and public code Richard Heathfield <rjh@cpax.org.uk> - 2017-02-13 06:00 +0000
            Re: Private and public code GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-13 08:31 +0100
      Re: Private and public code Ian Collins <ian-news@hotmail.com> - 2017-02-13 19:07 +1300
        Re: Private and public code GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-13 09:14 +0100

csiph-web