Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #401650
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: if else is not readable |
| Date | 2026-09-05 16:16 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <117h884$1i4mo$1@dont-email.me> (permalink) |
| References | <117gv9s$1fjmq$1@dont-email.me> <117h0rq$1g4nu$1@dont-email.me> |
On 05/09/2026 14:10, bart wrote:
> On 05/09/2026 12:43, fir wrote:
>> imo c in some way is to make programmer not thing liek in assembler
>> when you need to hardly mentally focus but you use some plain c
>> constructions (it is not on all c becouse some c is really convoluted ;c)
>>
>> if so you need easy if an sometimes those if else
>> ladders are not enough plain imo
>>
>>
>> this mean some needs some other construction that should be easier to
>> read
>>
>> see for example this code
>>
>>
>> if(dodge <1.)
>> {
>> if(dodge < .5)
>> { slog("%s easily dodged attack...", being[k].name); }
>> else if(dodge > .9)
>> { slog("%s hardly dodged attack...", being[k].name); }
>> else
>> slog("%s dodged attack...", being[k].name);
>>
>> return 1;
>> }
>>
>> imo its not readable, switch is also bad here, some needs something
>> other here soem other if realated construction
> It's not the if-else that is the problem here; the example is dominated
> by those slog calls.
>
> However your coding style doesn't help, with badly placed and
> inconsistent use of braces. The logic here is this:
>
> #define sdodge(x) slog("%s" x "dodged attack...", being[k], name)
>
> if (dodge < 1.0) {
> if (dodge < 0.5) sdodge("easily ");
> else if (dodge > 0.9) sdodge("hardly ");
> else sdodge("");
>
> return 1;
> }
>
> Switch is not going to be useful in this case, unless you call a
> function that takes 'dodge' and returns a 1/2/3 category code.
Your bracing style is also questionable - for many reasons, it is common
in style guides to recommend braces whenever you have "else" clauses,
and even more so when macros are involved.
And while refactoring out the common code is a good idea, it is almost
always best to use functions for the job.
static void sdodge(int being_index, const char * adj) {
slog("%s %sdodged attack...", being[being_index].name, adj);
}
...
if (dodge < 1.0) {
if (dodge < 0.5) {
sdodge(k, "easily ");
} else if (dodge > 0.9) {
sdodge(k, "hardly ");
} else {
sdodge(k, "");
}
return 1;
}
...
This is easier to change if you need to add more to the different
clauses, or if sdodge() needs more statements.
It is also possible to use the tertiary operator, which some people
might feel is clearer :
...
if (dodge < 1.0) {
sdodge(k, (dodge < 0.5) ? "easily " :
(dodge > 0.9) ? "hardly " :
"");
return 1;
}
...
Even better, perhaps, the classification part could be pulled out :
typedef enum { dodge_easy, dodge_medium, dodge_hard } dodge_style_t;
const char * dodge_adjectives[] = { "easily ", "", "hardly " };
dodge_style_t dodge_style_from_frac(double x) {
if (dodge < 0.5) return dodge_easy;
if (dodge > 0.9) return dodge_hard;
return dodge_medium;
}
...
if (dodge < 1.0) {
slog("%s %sdodged attack...", being[k].name,
dodge_adjectives[dodge_style_from_frac[dodge]));
return 1;
}
...
Now it's a lot easier to change things if other dodge styles are added,
or the boundaries are changed - there are fewer "magic numbers" in the
main code.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
if else is not readable fir <profesor.fir@gmail.com> - 2026-09-05 13:43 +0200
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-05 13:51 +0200
Re: if else is not readable bart <bc@freeuk.com> - 2026-09-05 13:10 +0100
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-05 14:26 +0200
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-05 14:43 +0200
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-05 14:51 +0200
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-05 14:59 +0200
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-05 15:09 +0200
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-05 15:15 +0200
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-05 15:21 +0200
Re: if else is not readable David Brown <david.brown@hesbynett.no> - 2026-09-05 16:16 +0200
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-05 16:38 +0200
Re: if else is not readable Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-09-05 19:03 +0200
Re: if else is not readable Lane W <cactus_DAC@yahoo.com> - 2026-09-05 12:09 -0600
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-05 20:45 +0200
Re: if else is not readable Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-09-06 00:36 +0200
Re: if else is not readable Lane W <cactus_DAC@yahoo.com> - 2026-09-08 19:51 -0600
Re: if else is not readable bart <bc@freeuk.com> - 2026-09-06 00:06 +0100
Re: if else is not readable David Brown <david.brown@hesbynett.no> - 2026-09-06 11:57 +0200
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-06 12:12 +0200
Re: if else is not readable bart <bc@freeuk.com> - 2026-09-06 13:54 +0100
Re: if else is not readable bart <bc@freeuk.com> - 2026-09-06 14:02 +0100
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-06 16:29 +0200
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-06 15:25 +0200
Re: if else is not readable bart <bc@freeuk.com> - 2026-09-06 16:42 +0100
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-09 14:11 +0200
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-09 14:22 +0200
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-09 14:27 +0200
Re: if else is not readable fir <profesor.fir@gmail.com> - 2026-09-09 14:51 +0200
Re: enums (was Re: if else is not readable) Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-09-19 01:33 +0000
Re: enums (was Re: if else is not readable) "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-19 16:18 +0800
Re: if else is not readable David Brown <david.brown@hesbynett.no> - 2026-09-06 19:58 +0200
csiph-web