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

Re: [PATCH 1/5] hashmap: add enum for hashmap free_entries option

From
Karsten Blees <karsten.blees@gmail.com>
Date
Jun 11, 2014, 09:12 UTC
Message-ID
<53981D6A.3090604@gmail.com>
In-Reply-To
<20140610101744.GA23370@t2784.greatnet.de>
Am 10.06.2014 12:17, schrieb Heiko Voigt:
Show 40 quoted lines
> On Fri, Jun 06, 2014 at 07:52:03PM +0200, Karsten Blees wrote:
>> Am 05.06.2014 08:06, schrieb Heiko Voigt:
>>> This allows a reader to immediately know which options can be used and
>>> what this parameter is about.
>>>
>> [...]
>>> -void hashmap_free(struct hashmap *map, int free_entries)
>>> +void hashmap_free(struct hashmap *map, enum hashmap_free_options free_entries)
>> [...]
>>>  
>>> +enum hashmap_free_options {
>>> +	HASHMAP_NO_FREE_ENTRIES = 0,
>>> +	HASHMAP_FREE_ENTRIES = 1,
>>> +};
>>
>> This was meant as a boolean parameter. Would it make sense to have
>>
>> enum boolean {
>> 	false,
>> 	true
>> };
>>
>> or similar in some central place?
> 
> The intention of Jonathans critique here[1] was that you do not see what
> this parameter does on the callsite. I.e.:
> 
> 	hashmap_free(&map, 1);
> 
> compared to
> 
> 	hashmap_free(&map, HASHMAP_FREE_ENTRIES);
> 
> A boolean basically transfers the same information and would not help
> the reader here.
> 
> Cheers Heiko
> 
> [1] http://article.gmane.org/gmane.comp.version-control.git/243917
> 
There are languages where you can have e.g. 'hashmap_free(..., free_entries: true)'. In C, however, you do not see what a parameter does at the call site. This is a general language feature, reducing redundancy and keeping it short and concise. IMO there's no reason to treat boolean parameters differently.
Using an enum suggests that there is more to the parameter than a simple yes/no decision, underpinned by naming it '...options' (plural). I find this rather confusing.
Finally, enums share a global namespace, which means long identifiers, provoking additional line breaks and thus reducing readability. Not a problem with hashmap_free per se, but if you do the same for e.g. 'free_util' in string-list.[ch] or 'icase' in name-hash.c, I suspect it'll get pretty ugly.
So please lets not spoil the global namespace with a thousand different names for 0/1. Using enums for >= tristate values and bit flags is fine, but inventing enums for every simple boolean in the system is bound to end in chaos.

Just my 2c Karsten

Previous: Heiko VoigtNext: Junio C Hamano
Message 5 of 26 in “submodule config lookup API”
  1. 0/5 submodule config lookup APIHeiko Voigt, Jun 5, 2014
  2. 1/5 hashmap: add enum for hashmap free_entries optionHeiko Voigt, Jun 5, 2014
  3. Karsten BleesJun 6, 2014
  4. Heiko VoigtJun 10, 2014
  5. Karsten BleesJun 11, 2014
  6. Junio C HamanoJun 12, 2014
  7. Karsten BleesJun 17, 2014
  8. Heiko VoigtJun 17, 2014
  9. Junio C HamanoJun 17, 2014
  10. 2/5 implement submodule config cache for lookup of submodule namesHeiko Voigt, Jun 5, 2014
  11. W. Trevor KingJun 5, 2014
  12. Heiko VoigtJun 6, 2014
  13. Eric SunshineJun 8, 2014
  14. Heiko VoigtJun 10, 2014
  15. Junio C HamanoJun 12, 2014
  16. Heiko VoigtJun 13, 2014
  17. 3/5 extract functions for submodule config set and lookupHeiko Voigt, Jun 5, 2014
  18. 4/5 use new config API for worktree configurations of submodulesHeiko Voigt, Jun 5, 2014
  19. 5/5 do not die on error of parsing fetchrecursesubmodules optionHeiko Voigt, Jun 5, 2014
  20. Junio C HamanoJun 12, 2014
  21. Heiko VoigtJun 13, 2014
  22. Junio C HamanoJun 16, 2014
  23. Heiko VoigtJun 17, 2014
  24. Junio C HamanoJun 12, 2014
  25. Jens LehmannJun 13, 2014
  26. Junio C HamanoJun 13, 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.