Sujet : Re: Fixing a sample from K&R book using cake static analyser
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 24. Jun 2024, 10:31:13
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240624022114.728@kylheku.com>
References : 1 2 3 4 5 6
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-06-24, Janis Papanagnou <janis_papanagnou+
ng@hotmail.com> wrote:
On 24.06.2024 01:04, Kaz Kylheku wrote:
On 2024-06-23, Anton Shepelev <anton.txt@gmail.moc> wrote:
Kaz Kylheku:
>
What scatter-brained drivel. Watch and learn:
>
struct nlist *install(char *name, char *defn)
{
struct nlist *existing = lookup(name);
>
if (existing) {
return existing;
} else {
>
When the if-branch ends with a return, the else-branch is
redundant, and its body shall be written bare, removing one
(useless) level of nesting and indentation.
>
I was tempted to post the same comment before I noticed that:
>
I did that because there are declarations in the else case.
>
So I at least understand where Kaz was coming from and
abstained from a comment; I think it's a valid view.
>
I avoid mising declarations and statements, because I consider
that a language misfeature.
In a block structured language, declarations should come first,
then statements.
>
This is an age old concept, some programming languages even
require that (e.g. Simula, Pascal, just to name a few). But
starting with C++ I preferred the initialized declarations;
introduce objects where you use them, so that you have sort
of a "local scope"[*].
But in C++ you can do this:
{
Obj foo("init", 42);
obj.barf();
}
and there are advantages to doing so, compared to leaving
the braces out.
1. you can legally goto/switch around this:
goto label; // valid C++
{
Obj foo("init", 42);
obj.barf();
}
label: ;
2. You know that the declared identifier foo's scope
ends at the curly brace:
{
Obj foo("init", 42);
obj.barf();
}
// foo is not known here; even if your editor finds a foo below
// this line, it is not *that* foo, so don't bother.
3. Because of (2) you can cleanly reuse the same name:
{
Obj foo("init", 42);
obj.barf();
}
// Copy, paste, adjust, no problem:
{
Obj foo("init", 43);
obj.barf();
}
You would never want to generate a self-contained block of code
by a macro without braces!
It is valuable to know that no references to anything called
foo outside of those braces have anything to do with that foo,
and to be able to skip around that whole thing.
Braces are encapsulation; encapsulation is often good.
Braces are a lambda that is immediately invoked!
(let ((foo 42)) <---> ((lambda (foo)
(barf foo)) (barf foo)) 42)
Variables should be initialized at the top because they are
de facto parameters of a function.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca