Hi all,
Summary of my problem first:
----------------------------------------------------------------------------
We now have 4 ways to test scoped_ptr for not being NULL.
1. if (foo)
2. if (foo != NULL)
3. if (foo.get())
4. if (foo.get() != NULL)
Neither of them seems to be preferred by the style guide, apart of the (IMO little vague) consistency requirement.
Also, motivation for consistency is easy pattern-matching and readability, which is not necessarily optimised with the most frequently used version of the pointer test.
What’s the rule of thumb here during disputes in reviews? Follow the guidance of code OWNERS?
----------------------------------------------------------------------------
If you are still reading, here is the appendix:
Couple of times a reviewer asked me to use option 3., based on its prevalence (I did not actually check the numbers, though) and thus maintaining consistency.
The style guide says, that consistency is important because “(...) we can more easily use "pattern-matching" to infer what various symbols are (...)” and “(...) patterns makes code much easier to understand.” So if we try to preserve consistency, we do it also for easy pattern-matching and readability.
Now, for pattern matching options 2 and 4 are actually much better than 1 or 3. In fact, when I was following the reviewer’s request to replace all 4s with 3s, I just did a simple search for “.get() != NULL” in my patch.
As for readability (and this is heavily subjective), this is how the code reads to me:
1. If foo...
2. If foo is not NULL...
3. If getting something from foo is successful...
4. If what we get from foo is not NULL...
The reading of 1. is confusing, and 3. sounds a little different from what it actually is.
To be fair, what often helps with reading is brevity, in which sense 1 is the best, and 4 the worst.
But that’s exactly the point, there is hardly the single best one of the four options. Currently the only argument to consider, going by the style guide, seems to be consistency, not readability or pattern-matching. Still, if two NULL-tests are in the same file, does it necessarily mean that both have the same ideal way of being written?
Forcing global consistency would imply using only one of the 4 options, clearly not the goal here. So I guess the easiest way to go is by asking a person from an appropriate OWNERS file, and stick to what they wish the code to be consistent with.
I know this is rather unimportant, but as I said, it leads to discussions in reviews, and so I would like to know what chromium-dev people think of it.
Thanks,
Vaclav