Re: Fixing a sample from K&R book using cake static analyser

Liste des GroupesRevenir à l c 
Sujet : Re: Fixing a sample from K&R book using cake static analyser
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.c
Date : 24. Jun 2024, 11:39:49
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v5bet5$s3j6$1@dont-email.me>
References : 1 2 3 4 5 6 7
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0
On 24/06/2024 02:34, Lawrence D'Oliveiro wrote:
On Mon, 24 Jun 2024 00:25:56 +0100, Ben Bacarisse wrote:
 
struct nlist *install(const char *name, const char *defn)
{
      struct nlist *node = lookup(name);
      if (node) {
           char *new_defn = strdup(defn);
           if (new_defn) {
                free(node->defn);
                node->defn = new_defn;
                return node;
           }
           else return NULL;
      }
      else {
           struct nlist *new_node = malloc(sizeof *new_node);
           char *new_name = strdup(name), *new_defn = strdup(defn);
           if (new_node && new_name && new_defn) {
                unsigned hashval = hash(name);
                new_node->name = new_name;
                new_node->defn = new_defn;
                new_node->next = hashtab[hashval];
                return hashtab[hashval] = new_node;
           }
           else {
                free(new_defn);
                free(new_name);
                free(new_node);
                return NULL;
           }
      }
}
>
We have four cases:
>
   node with the name found
      new definition allocated new definition not allocated
   node with the name not found
      new node, name and definition all allocated not all of new node,
      name and definition allocated.
>
We can very simply reason about all of these situations.
 Too many different paths in the control flow, though. I think it’s a good
idea to minimize that.
I disagree.  It's much clearer (to me) to separate the cases and deal with them individually.
I think the only disadvantage of doing that as a strategy is that you can sometimes end up with duplicated code.  If the duplications are significant, refactor them out as separate static functions.
Ben's code here is the first version I have seen where I have not thought "That code is horrible.  I'd have to think about it to see what it does."
I have two small complaints. I think it is a bad idea to have more than one variable declaration and initialisation stuffed in the same line unless they are /very/ tightly coupled, and I certainly don't like it if pointers are involved.  And I don't like returning (or using) the value of an assignment.  Basically, I prefer one line of code to do one thing at a time.  But while /I/ would split up those lines, I don't find it hard to see what Ben is doing in the code.

Date Sujet#  Auteur
21 Jun 24 * Fixing a sample from K&R book using cake static analyser83Thiago Adams
22 Jun 24 +* Re: Fixing a sample from K&R book using cake static analyser79Lawrence D'Oliveiro
22 Jun 24 i+- Re: Fixing a sample from K&R book using cake static analyser1Tim Rentsch
23 Jun 24 i+* Re: Fixing a sample from K&R book using cake static analyser13Anton Shepelev
23 Jun 24 ii+* Re: Fixing a sample from K&R book using cake static analyser5Lawrence D'Oliveiro
23 Jun 24 iii+* Re: Fixing a sample from K&R book using cake static analyser2bart
24 Jun 24 iiii`- Re: Fixing a sample from K&R book using cake static analyser1Lawrence D'Oliveiro
24 Jun 24 iii`* Re: Fixing a sample from K&R book using cake static analyser2Anton Shepelev
24 Jun 24 iii `- Re: Fixing a sample from K&R book using cake static analyser1Lawrence D'Oliveiro
23 Jun 24 ii`* Re: Fixing a sample from K&R book using cake static analyser7Ben Bacarisse
24 Jun 24 ii `* Re: Fixing a sample from K&R book using cake static analyser6Anton Shepelev
24 Jun 24 ii  `* Re: Fixing a sample from K&R book using cake static analyser5Ben Bacarisse
24 Jun 24 ii   +- Re: Fixing a sample from K&R book using cake static analyser1Lawrence D'Oliveiro
24 Jun 24 ii   +* Re: Fixing a sample from K&R book using cake static analyser2Janis Papanagnou
24 Jun 24 ii   i`- Re: Fixing a sample from K&R book using cake static analyser1Lawrence D'Oliveiro
24 Jun 24 ii   `- Re: Fixing a sample from K&R book using cake static analyser1Tim Rentsch
23 Jun 24 i`* Re: Fixing a sample from K&R book using cake static analyser64Kaz Kylheku
23 Jun 24 i +* Re: Fixing a sample from K&R book using cake static analyser57Ben Bacarisse
24 Jun 24 i i+* Re: Fixing a sample from K&R book using cake static analyser55Anton Shepelev
24 Jun 24 i ii+* Re: Fixing a sample from K&R book using cake static analyser3Lawrence D'Oliveiro
24 Jun 24 i iii`* Re: Fixing a sample from K&R book using cake static analyser2Anton Shepelev
24 Jun 24 i iii `- Re: Fixing a sample from K&R book using cake static analyser1Lawrence D'Oliveiro
24 Jun 24 i ii`* Re: Fixing a sample from K&R book using cake static analyser51Ben Bacarisse
24 Jun 24 i ii +* Re: Fixing a sample from K&R book using cake static analyser45Lawrence D'Oliveiro
24 Jun 24 i ii i+* Re: Fixing a sample from K&R book using cake static analyser4David Brown
25 Jun 24 i ii ii`* Re: Fixing a sample from K&R book using cake static analyser3Lawrence D'Oliveiro
25 Jun 24 i ii ii +- Re: Fixing a sample from K&R book using cake static analyser1David Brown
25 Jun 24 i ii ii `- Re: Fixing a sample from K&R book using cake static analyser1Kaz Kylheku
24 Jun 24 i ii i`* Re: Fixing a sample from K&R book using cake static analyser40Ben Bacarisse
25 Jun 24 i ii i `* Re: Fixing a sample from K&R book using cake static analyser39Lawrence D'Oliveiro
25 Jun 24 i ii i  +- Re: Fixing a sample from K&R book using cake static analyser1Ben Bacarisse
25 Jun 24 i ii i  `* Re: Fixing a sample from K&R book using cake static analyser37Kaz Kylheku
25 Jun 24 i ii i   `* Re: Fixing a sample from K&R book using cake static analyser36Ben Bacarisse
26 Jun 24 i ii i    `* Re: Fixing a sample from K&R book using cake static analyser35Chris M. Thomasson
26 Jun 24 i ii i     `* Re: Fixing a sample from K&R book using cake static analyser34Kaz Kylheku
26 Jun 24 i ii i      `* Re: Fixing a sample from K&R book using cake static analyser33Janis Papanagnou
26 Jun 24 i ii i       `* Re: Fixing a sample from K&R book using cake static analyser32Kaz Kylheku
26 Jun 24 i ii i        +- Re: Fixing a sample from K&R book using cake static analyser1DFS
26 Jun 24 i ii i        +* [OT] Re: Fixing a sample from K&R book using cake static analyser26Janis Papanagnou
26 Jun 24 i ii i        i+* Re: [OT] Re: Fixing a sample from K&R book using cake static analyser4Michael S
26 Jun 24 i ii i        ii`* Re: [OT] Reinheitsgebot and Beer without C3Janis Papanagnou
26 Jun 24 i ii i        ii `* Re: [OT] Reinheitsgebot and Beer without C2Michael S
26 Jun 24 i ii i        ii  `- Re: [OT] Reinheitsgebot and Beer without C1Janis Papanagnou
26 Jun 24 i ii i        i+- Re: [OT] Re: Fixing a sample from K&R book using cake static analyser1Michael S
26 Jun 24 i ii i        i+* Re: [OT] Re: Fixing a sample from K&R book using cake static analyser11Janis Papanagnou
26 Jun 24 i ii i        ii+* Re: [OT] Re: Fixing a sample from K&R book using cake static analyser9Chris M. Thomasson
26 Jun 24 i ii i        iii`* Re: [OT] Re: Fixing a sample from K&R book using cake static analyser8Janis Papanagnou
27 Jun 24 i ii i        iii +* Re: [OT] Re: Fixing a sample from K&R book using cake static analyser2Richard Harnden
27 Jun 24 i ii i        iii i`- Re: [OT] Re: Fixing a sample from K&R book using cake static analyser1Janis Papanagnou
29 Jun 24 i ii i        iii `* Re: [OT] Re: Fixing a sample from K&R book using cake static analyser5Chris M. Thomasson
29 Jun 24 i ii i        iii  `* Re: [OT] Re: Fixing a sample from K&R book using cake static analyser4Janis Papanagnou
29 Jun 24 i ii i        iii   +* Re: [OT] Re: Fixing a sample from K&R book using cake static analyser2Kenny McCormack
29 Jun 24 i ii i        iii   i`- Re: [OT] Re: Fixing a sample from K&R book using cake static analyser1Chris M. Thomasson
29 Jun 24 i ii i        iii   `- Re: [OT] Re: Fixing a sample from K&R book using cake static analyser1Michael S
27 Jun 24 i ii i        ii`- Re: [OT] Re: Fixing a sample from K&R book using cake static analyser1Janis Papanagnou
28 Jun 24 i ii i        i`* Re: [OT] Re: Fixing a sample from K&R book using cake static analyser9Phil Carmody
28 Jun 24 i ii i        i `* Re: [OT] Re: Fixing a sample from K&R book using cake static analyser8Janis Papanagnou
28 Jun 24 i ii i        i  `* Re: [OT] Re: Fixing a sample from K&R book using cake static analyser7Michael S
28 Jun 24 i ii i        i   `* Re: [OT] Re: Fixing a sample from K&R book using cake static analyser6Janis Papanagnou
28 Jun 24 i ii i        i    +* Re: [OT] Re: Fixing a sample from K&R book using cake static analyser3Tim Rentsch
28 Jun 24 i ii i        i    i`* Re: [OT] Re: Fixing a sample from K&R book using cake static analyser2Janis Papanagnou
30 Jun 24 i ii i        i    i `- Re: [OT] Re: Fixing a sample from K&R book using cake static analyser1Tim Rentsch
29 Jun 24 i ii i        i    `* Re: [OT] Re: Fixing a sample from K&R book using cake static analyser2Chris M. Thomasson
29 Jun 24 i ii i        i     `- Re: [OT] Re: Fixing a sample from K&R book using cake static analyser1Chris M. Thomasson
26 Jun 24 i ii i        `* Re: Fixing a sample from K&R book using cake static analyser4David Brown
27 Jun 24 i ii i         +- Re: Fixing a sample from K&R book using cake static analyser1Richard Harnden
27 Jun 24 i ii i         `* Re: Fixing a sample from K&R book using cake static analyser2Lawrence D'Oliveiro
27 Jun 24 i ii i          `- Re: Fixing a sample from K&R book using cake static analyser1Janis Papanagnou
24 Jun 24 i ii +- Re: Fixing a sample from K&R book using cake static analyser1Tim Rentsch
25 Jun 24 i ii `* Re: Fixing a sample from K&R book using cake static analyser4Phil Carmody
25 Jun 24 i ii  +- Re: Fixing a sample from K&R book using cake static analyser1Ben Bacarisse
26 Jun 24 i ii  `* Re: Fixing a sample from K&R book using cake static analyser2Lawrence D'Oliveiro
30 Jun 24 i ii   `- Re: Fixing a sample from K&R book using cake static analyser1Phil Carmody
24 Jun 24 i i`- Re: Fixing a sample from K&R book using cake static analyser1Kaz Kylheku
24 Jun 24 i `* Re: Fixing a sample from K&R book using cake static analyser6Anton Shepelev
24 Jun 24 i  `* Re: Fixing a sample from K&R book using cake static analyser5Kaz Kylheku
24 Jun 24 i   +* Re: Fixing a sample from K&R book using cake static analyser2Kaz Kylheku
24 Jun 24 i   i`- Re: Fixing a sample from K&R book using cake static analyser1Anton Shepelev
24 Jun 24 i   `* Re: Fixing a sample from K&R book using cake static analyser2Janis Papanagnou
24 Jun 24 i    `- Re: Fixing a sample from K&R book using cake static analyser1Kaz Kylheku
22 Jun 24 +- Re: Fixing a sample from K&R book using cake static analyser1Thiago Adams
29 Jun 24 `* Re: Fixing a sample from K&R book using cake static analyser2Lawrence D'Oliveiro
29 Jun 24  `- Re: Fixing a sample from K&R book using cake static analyser1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal