Sujet : Re: Code guidelines
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.cDate : 03. Sep 2024, 15:12:24
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vb75g9$3bntp$1@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
On 03/09/2024 10:33, Thiago Adams wrote:
...
For instance:
The first sample my create confusion (is name optional?)
void f(struct user* user)
{
if (user->name && strcmp(user->name, "john") == 0)
{
//...
}
}
But :
void f(struct user* user)
{
assert(user->name);
if (user->name && strcmp(user->name, "john") == 0)
{
//...
}
}
would show redundancy but making clear the contract still "name should not be null"
Redundant code can either indicate a programmer's mental confusion or serve as a way to address potential contract violations.
I believe the objective is to ensure that runtime checks are not questioning the contract but rather functioning as redundant safeguards.
In other words, the programmer must demonstrate that they understand the contract and are not messing it.
A safeguards for a very low risk situation also may indicate a mental confusion about the risks involved. For instance, assert(2 + 2 == 4);