Re: Coding style point: "const" in function parameter declarations

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Merlin Moncure <mmoncure(at)gmail(dot)com>, Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>, Dan Ports <drkp(at)csail(dot)mit(dot)edu>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Coding style point: "const" in function parameter declarations
Date: 2011-06-22 19:03:56
Message-ID: BANLkTimL6gzpLNd7YnpBJ1cNbOO5szWorQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Jun 22, 2011 at 12:41 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Merlin Moncure <mmoncure(at)gmail(dot)com> writes:
>> On Tue, Jun 21, 2011 at 5:51 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>>> Declarations like "const structtype *param" are fine, because those
>>> create a real, enforced contract on what the function can do to data
>>> that is visible to its caller.  But I don't see any value at all in
>>> const-ifying the parameter itself.
>
>> What about making a separate typedef for that (like ConstRelation)?
>
> Well, any change along this line would require pretty widespread
> changes, as you'd have to const-ify from the bottom up, or else hold
> your nose and cast-away-const in assorted calls (which I think is so
> evil you might as well not have the const in the first place).
>
> If we were thinking of moving in that direction, I would argue that
> we should get rid of typedef'd pointers altogether, ie, change
> "Relation" to be a typedef for the struct and write "Relation *rel"
> not "Relation rel".  Then, attaching a const to the front is easy,
> natural, and does what the naive reader would expect.  But this would
> be a sufficiently sweeping change that I'm not sure the benefits
> would be worth the cost.

I don't really see the point, either. I mean, the Relation is
basically a cache. And so it could happen that we want to start
caching something we don't cache today. Then someone for whom it was
const suddenly needs it to be non-const. Of course I don't think
there's much call for monkeying with rel->rd_id but how likely is that
to be a real source of coding errors?

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Kevin Grittner 2011-06-22 19:12:46 Re: Repeated PredicateLockRelation calls during seqscan
Previous Message Bruce Momjian 2011-06-22 18:31:51 Re: Indication of db-shared tables