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

Re: Numeric constants as strings

From
APAndy Parkins <andyparkins@gmail.com>
Date
Feb 19, 2007, 11:00 UTC
Message-ID
<200702191100.42953.andyparkins@gmail.com>
In-Reply-To
<7vbqjq8ojr.fsf@assigned-by-dhcp.cox.net>
On Monday 2007 February 19 09:49, Junio C Hamano wrote:
> I do not want to risk discouraging public discussion on this
> topic, but I am not sure if this is really worth it.
It's not primarily for that purpose, but rather to improve readability.
For example, this little bit of patch:
-       if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
+       if (get_sha1_hex(buffer+5, tree_sha1) || buffer[HASH_WIDTH_ASCII+5] != '\n')
                return objerror(&commit->object, "invalid 'tree' line format - bad sha1");
-       buffer += 46;
+       buffer += HASH_WIDTH_ASCII+6;
        while (!memcmp(buffer, "parent ", 7)) {
-               if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
+               if (get_sha1_hex(buffer+7, sha1) || buffer[HASH_WIDTH_ASCII+7] != '\n')
                        return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
-               buffer += 48;
+               buffer += HASH_WIDTH_ASCII+8;

Using HASH_WIDTH_ASCII+8 is much clearer for a reader to be able to understand intent than 48. Especially when the variables in question are called "buffer" it's harder to track what is being stored in the buffer without a named constant.

There are also a few other places where 20 is used and HASH_WIDTH_ASCII is not intended.

Andy
-- 
Dr Andy Parkins, M Eng (hons), MIEE
andyparkins@gmail.com
Previous: Junio C HamanoNext: Mark Wooding
Message 8 of 11 in “Numeric constants as strings”
  1. Andy ParkinsFeb 19, 2007
  2. Junio C HamanoFeb 19, 2007
  3. Shawn O. PearceFeb 19, 2007
  4. Matthieu MoyFeb 19, 2007
  5. Andy ParkinsFeb 19, 2007
  6. Andy ParkinsFeb 19, 2007
  7. Junio C HamanoFeb 19, 2007
  8. Andy ParkinsFeb 19, 2007
  9. Mark WoodingFeb 19, 2007
  10. Andy ParkinsFeb 19, 2007
  11. Jakub NarebskiFeb 20, 2007

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.