[webkit-dev] When to use "auto"? (I usually consider it harmful)

Maciej Stachowiak mjs at apple.com
Thu Jan 2 19:37:39 PST 2014


On Jan 2, 2014, at 2:48 PM, Filip Pizlo <fpizlo at apple.com> wrote:

> I think that this article is one of many great examples of arguments in favor of less explicit typing.  These lines of reasoning are often used by proponents of both dynamic languages (like Ruby) and type-inferred languages (like ML or Haskell).  C++'s auto is almost exactly like what ML and Haskell have and it's not surprising to me to see bloggers rehash these decades-old arguments.
> 
> But what I like about how we use C++ in WebKit is that we avoid writing English comments, and try to document our code using types, function names, and variable names.  Types are a particularly powerful form of documentation because it is checked by the compiler.  I think that the main source of my distaste for using auto everywhere is that it takes away our compiler-checked documentation.
> 
> The article seems to suggest that we should say:
> 
>     auto x = type { expression };
> 
> when we want documentation.  But this is more noisy than saying:
> 
>     type x = expression;
> 
> I don't think that the auto-based type expression suggested by the article has any merits over the traditional variable type.

It does have one merit, namely once you've typed auto you can't accidentally forget to intialize the variable. Other than that, it seems like a stretch.

I can see some advantages to mostly type-free coding, but I think it's too big a change in style to our code base to make all at once. I'd rather start with something like Geoff's guidelines, where we use 'auto' only in cases where the type is too ugly to spell out, or already effectively stated by the line of code.

I'm not sure I like 'auto x = 42ul' over 'unsigned long x = 42' though. It effectively still has a type declaration but it's just using a short and slightly mysterious synonym for it. I don't think that is helpful on th whole.

Regards,
Maciej



More information about the webkit-dev mailing list