On Tue, May 26, 2009 at 8:31 PM, Brett Wilson <[email protected]> wrote:
>
> On Tue, May 26, 2009 at 6:22 PM, Aaron Boodman <[email protected]> wrote:
>>
>> I get different stories about this all the time from different people.
>> It would be great to have them as part of the style guide so that I
>> can refer to it.
>>
>> One that you didn't cover is the invariant case. I originally used
>> DCHECKs, but it seems like if you're just going to crash on the next
>> line anyway, a CHECK makes more sense.
>
> Here's what I do:
>
> Generally, use DCHECK().
>
> Use CHECK when the problem indicates a potential security problem.
> I've also added them in cases where there are crash reports and I
> can't find the source of the problem. CHECK()s around the entrypoints
> for various conditions can help narrow this down.
>
> Don't bother doing an assertion when the next line will crash anyway:
>  DCHECK(foo);
>  foo->DoSomething();
> will normally crash pretty obviously dereferencing a NULL pointer
> (even though it will be inside DoSomething). NULL pointer dereferences
> are usually the easiest bugs to see. DCHECK in this situation is
> especially useless since if you're running in a debug build in a
> debugger such that they are enabled, it will be even more obvious.
> However, use assertions when the argument is saved for later use, so
> the offender is obvious (this happens in class constructors most
> often).
>
> Brett

I suggest doing DCHECK anyway in that case if there's a chance foo can be NULL.
foo->DoSomething() will only crash if DoSomething is virtual,
otherwise the code doesn't actually dereference foo. DoSomething
might, or it might as well not, for example forwarding the call to a
member - in which case the 'this' pointer may be small but non-NULL,
and then pass a few checks down the road that it should not. It gets
blurrier and blurrier as you go down the stack.
I personally find DCHECK() a good form of documentation when reading
the code, a form of documentation that is guaranteed to be correct if
the code is exercised :)

Antoine
-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

--~--~---------~--~----~------------~-------~--~----~
Chromium Developers mailing list: [email protected] 
View archives, change email options, or unsubscribe: 
    http://groups.google.com/group/chromium-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to