git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jonathan Nieder <jrnieder@gmail.com>
To: Stefan Beller <sbeller@google.com>
Cc: git@vger.kernel.org, git@jeffhostetler.com
Subject: Re: [PATCH 1/3] Documentation/CodingGuidelines: explain why assert is bad
Date: Wed, 22 Nov 2017 14:59:13 -0800	[thread overview]
Message-ID: <20171122225913.GF11671@aiede.mtv.corp.google.com> (raw)
In-Reply-To: <20171122223827.26773-2-sbeller@google.com>

Hi,

Stefan Beller wrote:

> --- a/Documentation/CodingGuidelines
> +++ b/Documentation/CodingGuidelines
> @@ -386,6 +386,9 @@ For C programs:
>   - Use Git's gettext wrappers to make the user interface
>     translatable. See "Marking strings for translation" in po/README.
>  
> + - Prefer the BUG() macro over asserts, as asserts requires that the
> +   NDEBUG flag is unset on compilation to work.

nit: is there some logical place in the list of C guidelines this
should go at, instead of the last item?  Maybe near the top, since
this is one of those straightforward cases and we're just saying that
this is the startegy for asserting invariants that this project
prefers.

Separately: I am not sure we currently universally prefer BUG_ON()
over assert().  In theory, assert() is fine wherever you don't care
whether the assertion is checked at run-time --- in other words, it is
a fancy form of comment.  BUG_ON() is useful for defensive checks that
you *expect* to never trip but want to rely on afterwards.

In a certain ideal world, the preference would be reversed: you'd want
to use assert() wherever you can and require the compiler to check
that all assert()s are verifiable at compile time.  A check that a
static analyzer can verify is more valuable than a run-time check.
When a compile-time check is not possible, you'd have to fall back to
BUG_ON().

But we don't live in that ideal world.  I'm not aware of any widely
available tools for checking assert()s.  So maybe it makes sense to
forbid assert() in our codebase entirely.

That could be enforced using something like check-non-portable-shell.pl,
except

- run on C files instead of sh files
- run on Git's source code instead of t/

What do you think?

Thanks,
Jonathan

  reply	other threads:[~2017-11-22 22:59 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-22 22:38 [PATCH 0/3] Introduce BUG_ON(cond, msg) MACRO Stefan Beller
2017-11-22 22:38 ` [PATCH 1/3] Documentation/CodingGuidelines: explain why assert is bad Stefan Beller
2017-11-22 22:59   ` Jonathan Nieder [this message]
2017-11-22 23:08     ` Stefan Beller
2017-11-22 23:54       ` Jonathan Nieder
2017-11-22 22:38 ` [PATCH 2/3] git-compat: introduce BUG_ON(condition, fmt, ...) macro Stefan Beller
2017-11-22 23:02   ` Jonathan Nieder
2017-11-22 23:37     ` Jeff King
2017-11-22 22:38 ` [PATCH 3/3] contrib/coccinelle: convert all conditional bugs to bug_on Stefan Beller
2017-11-22 23:24 ` [PATCH 0/3] Introduce BUG_ON(cond, msg) MACRO Jeff King
2017-11-22 23:28   ` Jonathan Nieder
2017-11-22 23:39     ` Jeff King
2017-11-22 23:45       ` Jonathan Nieder
2017-11-22 23:58         ` Jeff King
2017-11-23  0:08           ` Jonathan Nieder
2017-11-23  0:10             ` Jeff King
2017-11-23  1:38             ` Junio C Hamano
2017-11-23  5:00               ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171122225913.GF11671@aiede.mtv.corp.google.com \
    --to=jrnieder@gmail.com \
    --cc=git@jeffhostetler.com \
    --cc=git@vger.kernel.org \
    --cc=sbeller@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).