Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #171408

Re: strings or enums?

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: strings or enums?
Date 2023-07-29 21:35 +0100
Organization A noiseless patient Spider
Message-ID <87jzuixxp6.fsf@bsb.me.uk> (permalink)
References <a056e184-c3d8-48ae-9ffe-a3693ebdbbe6n@googlegroups.com>

Show all headers | View raw


Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:

> Do people prefer string or enum arguments to functions taking one of a
> restricted list of options?  The function I'm writing is
>
> char *loadasutf8wthknownformat(const char *filename, format, int *error);
>
> If format is an enum, I'd have to create an enum with a list of
> formats supported. If it's a string, the caller has to pass one of a
> restricted set of formats.

I think I'd #define the formats (as you suggest later on).

> Advantages to the enum
> It's a lot harder to pass a typo or an ill- formed format.
> Caller has a list of allowed formats which automatically update.
> It's more machine efficent (but won't matter for this function)
>
> Advantages to the string
> Several functions might operate on text file formats. Strings don't
> pollute the namespace.
> It's easier to print out a human-readable diagnostic at runtime.
> It's slightly more natural if the origin of the value is an input string
> anyway and not a hard-coded constant.

When I first learned Lisp, I could not work out what symbols were
supposed to be.  They were "things" whose only purposes appeared to be
to different from other symbols, but they all sorts of uses, and this is
one other them: they are intrinsically human-readable distinct values.

> If an exponential combination problem starts, the string can solve it 
> by parsing. e.g the suffix "le" or "be" could always mean "big-endian"
> or "little-endian" . With enums you would have to double the length of the
> list.
>
> The hybrid compromise which we often use at work is to #define 
> string constants.

And you can combine them:

  decode(filename, ENCODING_UTF_16 | ENCODING_BIG_ENDIAN);

-- 
Ben.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

strings or enums? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-07-29 03:20 -0700
  Re: strings or enums? fir <profesor.fir@gmail.com> - 2023-07-29 06:43 -0700
    Re: strings or enums? fir <profesor.fir@gmail.com> - 2023-07-29 07:21 -0700
      Re: strings or enums? fir <profesor.fir@gmail.com> - 2023-08-02 13:45 -0700
  Re: strings or enums? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-07-29 21:35 +0100
    Re: strings or enums? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-07-30 08:28 -0700
      Re: strings or enums? Richard Damon <Richard@Damon-Family.org> - 2023-07-30 12:19 -0400
      Re: strings or enums? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-07-30 18:09 +0100
        Re: strings or enums? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-07-30 11:00 -0700
          Re: strings or enums? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-07-30 20:15 +0100
            Re: strings or enums? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-07-31 04:23 -0700
  Re: strings or enums? David Brown <david.brown@hesbynett.no> - 2023-08-01 20:16 +0200
    Re: strings or enums? Paul N <gw7rib@aol.com> - 2023-08-02 06:30 -0700
      Re: strings or enums? David Brown <david.brown@hesbynett.no> - 2023-08-02 18:26 +0200
        Re: strings or enums? Blue-Maned_Hawk <bluemanedhawk@gmail.com> - 2023-08-02 13:34 -0400
          Re: strings or enums? David Brown <david.brown@hesbynett.no> - 2023-08-02 23:50 +0200
    Re: strings or enums? fir <profesor.fir@gmail.com> - 2023-08-02 08:40 -0700
      Re: strings or enums? fir <profesor.fir@gmail.com> - 2023-08-02 08:54 -0700
        Re: strings or enums? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-08-02 09:00 -0700
          Re: strings or enums? fir <profesor.fir@gmail.com> - 2023-08-02 09:14 -0700
            Re: strings or enums? scott@slp53.sl.home (Scott Lurndal) - 2023-08-02 16:21 +0000
              Re: strings or enums? fir <profesor.fir@gmail.com> - 2023-08-02 09:23 -0700
                Re: strings or enums? fir <profesor.fir@gmail.com> - 2023-08-02 10:00 -0700
              Re: strings or enums? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-08-02 09:28 -0700
            Re: strings or enums? fir <profesor.fir@gmail.com> - 2023-08-02 09:22 -0700
          Re: strings or enums? David Brown <david.brown@hesbynett.no> - 2023-08-02 18:28 +0200
            Re: strings or enums? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-08-02 09:30 -0700
              Re: strings or enums? fir <profesor.fir@gmail.com> - 2023-08-02 10:04 -0700
                Re: strings or enums? fir <profesor.fir@gmail.com> - 2023-08-02 10:07 -0700
              Re: strings or enums? David Brown <david.brown@hesbynett.no> - 2023-08-02 23:54 +0200
                Re: strings or enums? Vir Campestris <vir.campestris@invalid.invalid> - 2023-08-05 11:47 +0100
                Re: strings or enums? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-05 09:45 -0700
    Re: strings or enums? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-08-02 08:52 -0700
      Re: strings or enums? David Brown <david.brown@hesbynett.no> - 2023-08-02 18:29 +0200

csiph-web