[cfe-dev] Default arguments considered harmful?

Richard legalize at xmission.com
Fri May 2 17:04:12 PDT 2014


In article <CACs=tyJfAHEMvKY2y_e22fnDjVXeHoDC4n5B=9KkrQAeyOU1Zg at mail.gmail.com>,
    Reid Kleckner <rnk at google.com> writes:

> On Thu, May 1, 2014 at 9:26 PM, Richard Smith <richard at metafoo.co.uk> wrote:
> 
> > Yikes! But ASTRecordLayout's constructor beats this function's puny 17
> > parameters -- it has 20.  And DeclaratorChunk::getFunction is the winner in
> > Clang, with 21 parameters (and only one miserable, lonely default
> > argument). In total, LLVM and Clang (and LLDB and whatever else is in my
> > source tree...) has nearly 100 functions in header files with 10 or more
> > parameters.
> >
> 
> Well, record layout is complicated.  :)  Would you rather build a fresh
> record layout and set the fields individually, potentially letting callers
> ignore some fields?

When I have lots of parameters that control creating an object, I tend
to prefer a "builder" interface that gives me readable code,
reasonable defaults and avoids the long parameter smell.

Here's an example I wrote up for describing GPU resource parameters in
the Direct3D11 API.  These GPU resources have lots of parameters (width,
height, bit plane depth) and lots of options, many of which have reasonable
defaults.
<http://legalizeadulthood.wordpress.com/2009/07/12/description-helpers-for-direct3d-10-10-1-and-11/>
-- 
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
     The Computer Graphics Museum <http://ComputerGraphicsMuseum.org>
         The Terminals Wiki <http://terminals.classiccmp.org>
  Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com>



More information about the cfe-dev mailing list