Liste des Groupes | Revenir à cl c |
On Mon, 24 Jun 2024 00:25:56 +0100, Ben Bacarisse wrote:I disagree. It's much clearer (to me) to separate the cases and deal with them individually.
struct nlist *install(const char *name, const char *defn)Too many different paths in the control flow, though. I think it’s a good
{
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.
idea to minimize that.
Les messages affichés proviennent d'usenet.