git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Initialise hash variable to prevent compiler warnings

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 14, 2014, 16:41 UTC
Message-ID
<CAPc5daXFuuxW=PSB_wKw3CcUGiM1sBPZXQ4UJoqHd1ZOPh7F8w@mail.gmail.com>
In-Reply-To
<CANxchRQ=kR4N5s5S8eSJy2de__BydWDgowCNb6QQW6JZyvQAMw@mail.gmail.com>
On Tue, Oct 14, 2014 at 4:44 AM, Felipe Franciosi <felipe@paradoxo.org> wrote:
Show 10 quoted lines
> On Tue, Oct 14, 2014 at 2:13 AM, Junio C Hamano <gitster@pobox.com> wrote:
>>
>> If I really had to choose between the two, adding a useless initialization
>> would be the less harmful choice. Adding a meaningless "default:" robs
>> ...
> Being a bit defensive in that
> sense and initialising local variables is what I would do. On top of
> that (and putting the compiler flaw aside for a moment), having it
> sensibly initialised is another way of protecting the code against
> errors introduced in the future.

That is a false sense of safety. You will not know if the new method introduced in the future would behave sensibly if the variable is left in a state the blanket initialization created, so setting it to 0 upfront is not really being defensive; you would rob compilers a chance to notice something is amiss in the future code with the initialization, just like a "default:" would. We need to accept that both are not about being defensive but are ways to work around stupid compilers from reporting false positives.

I am not saying that we should not do a work around. I am only saying that it is wrong to try selling such a work around as a defensive good practice, which is not.

> What do you think?

Again, if I really had to choose between the two, adding a useless initialization would be the less harmful choice, as the other one has an extra downside.

Previous: Felipe Franciosi
Message 6 of 6 in “Initialise hash variable to prevent compiler warnings”
  1. Initialise hash variable to prevent compiler warningsFelipe Franciosi, Oct 13, 2014
  2. Junio C HamanoOct 13, 2014
  3. Felipe FranciosiOct 13, 2014
  4. Junio C HamanoOct 14, 2014
  5. Felipe FranciosiOct 14, 2014
  6. Junio C HamanoOct 14, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.