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

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

From
Felipe Franciosi <felipe@paradoxo.org>
Date
Oct 14, 2014, 11:44 UTC
Message-ID
<CANxchRQ=kR4N5s5S8eSJy2de__BydWDgowCNb6QQW6JZyvQAMw@mail.gmail.com>
In-Reply-To
<CAPc5daWmHe-EbgO-wjy9UsodTFERa1vpz-SEDF-UM_eLm7CuCw@mail.gmail.com>
On Tue, Oct 14, 2014 at 2:13 AM, Junio C Hamano <gitster@pobox.com> wrote:
Show 26 quoted lines
> On Mon, Oct 13, 2014 at 2:53 PM, Felipe Franciosi <felipe@paradoxo.org> wrote:
>>
>> On Mon, Oct 13, 2014 at 9:12 PM, Junio C Hamano <gitster@pobox.com> wrote:
>>>
>>> FNV/I/IDIV10/0 covers all the possibilities of (method & 3), I would
>>> have to say that the compiler needs to be fixed.
>>>
>>> Or insert "default:" just before "case HASH_METHOD_0:" line?
>>>
>>> I dunno.
>>
>> Hmm... The "default:" would work, but is it really that bad to initialise a
>> local variable in this case?
>>
>> In any case, the compilation warning is annoying. Do you prefer the default
>> or the initialisation?
>
> If I really had to choose between the two, adding a useless initialization
> would be the less harmful choice. Adding a meaningless "default:" robs
> another chance from the compilers to diagnose a future breakage we
> might add (namely, we may extend methods and forget to write a
> corresponding case arm for the new method value, which a smart
> compiler can and do diagnose as a switch that does not handle
> all the possible values.
>
> Thanks.

I see your point; the code is correct today because it covers all cases. Nevertheless, some versions of gcc (the one I used was 4.1.2 from CentOS 5.10 -- haven't tested others) might generate an annoying warning.

Noting that, I also like my code to compile as cleanly as possible in all environments that it might be used. 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.

What do you think?

Cheers, F.

Previous: Junio C HamanoNext: Junio C Hamano
Message 5 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.