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

Brent Fulgham bfulgham at apple.com
Thu Jan 2 14:55:56 PST 2014


Hi Filip,

Coming back to your earlier example:

>     auto newSize = optimalSize();
> vs:
>     CGSize newSize = optimalSize();

If I understand your argument, you feel that the explicit CGSize declaration helps the reader because it makes the return value of optimalSize() explicit.

However, that declaration is only stating the type that the author *thinks* optimalSize() returns.  There is nothing to guarantee that optimalSize() returns a CGSize; only that it returns something that the compiler can turn into a CGSize through some set of casts.

The code stating CGSize could have been correct at one point, but the return type of optimalSize might have been changed to some similar type without anyone noticing.

Using ‘auto’ doesn’t seem to make this situation any worse.

In fact, although Sutter’s suggestion of:

	auto newSize = CGSize { optimalSize(); }

looks gross to my eye, it might be a useful approach because it would force the compiler to complain if we were returning something that was not explicitely a CGSize type.

-Brent


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.webkit.org/pipermail/webkit-dev/attachments/20140102/abccccb6/attachment.html>


More information about the webkit-dev mailing list