Re: [dev] [dwm][st] why use ints over bools?

From: Mattias Andrée <maandree_AT_kth.se>
Date: Sat, 22 Jun 2024 07:35:06 +0200

On Fri, 21 Jun 2024 23:29:29 +0200
Storkman <storkman_AT_storkman.nl> wrote:

> On Fri, Jun 21, 2024 at 10:12:15PM +0200, Mattias Andrée wrote:
> > On Fri, 21 Jun 2024 21:14:19 +0200
> > Markus Wichmann <nullplan_AT_gmx.net> wrote:
> >
> > > Am Thu, Jun 20, 2024 at 11:53:45PM +0200 schrieb Страхиња Радић:
> > > > Given that, why complicate code by introducing a separate, superfluous,
> > > > type?
> > >
> > > Let me offer a counterpoint: Expressiveness. If I make a function return
> > > a bool, then everyone from the world's worst junior dev to Ken Thompson
> > > himself will be able to see at the very first glance that the function
> > > is returning a boolean value. If I make a function return an int, that
> > > is not so clear.
> >
> > Expressiveness is great, but _Bool is more than just an expression of
> > intent. If _Bool was nothing more than a typedef of int, it would be great.
> >
> > However, in the real world, I also find that boolean isn't expressive enough
> > and enum is needed. This is because people do silly things like returning
> > true or false, without it being obvious that the function should even return
> > anything at all,
>
> Doesn't the function having a return type make it obvious that it returns something?

Yes, but apart from that. If you just look at the name,

>
> > do indicate for example if the function was successful,
> > or the action was completed or need to be made again, or if the action
> > modified the stated (could be true if the state was not modified or true
> > if the state was not modified).
>
> If I have a function like "bool is_cromulent(foo)", I'd expect it to return
> true when foo IS cromulent (or 1 if the return type is int).
> Are you saying it should return cromulence_t with values either
> IS_CROMULENT or IS_NOT_CROMULENT?

No, I'm talking about bad code.

What would you expect "bool applyConfiguration(conf)" to return?

Maybe it returns true on success and false on failure.
Maybe it returns true the state was modified and false otherwise.

>
> > > [...] Not so long ago, this very list opined that C89 is the only true
> > > C standard [...]
>
> If there was a new poll, I demand a recount!
>
Received on Sat Jun 22 2024 - 07:35:06 CEST

This archive was generated by hypermail 2.3.0 : Sat Jun 22 2024 - 07:36:09 CEST