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


Groups > comp.lang.c > #164125

Re: A question about style

From Manfred <noname@add.invalid>
Newsgroups comp.lang.c
Subject Re: A question about style
Date 2021-12-30 18:34 +0100
Organization Aioe.org NNTP Server
Message-ID <sqkqit$19s8$1@gioia.aioe.org> (permalink)
References <sqjivq$get$1@oguzismail.eternal-september.org>

Show all headers | View raw


On 12/30/2021 7:18 AM, Oğuz wrote:
> I have this if-else thingy:
> 
> if (!expr[expr_length - 1].is_number) {
>    if (skip_adj[expr[expr_length - 1].value][op])
>      return false;
> }
> else if (expr_length > 2 && !expr[expr_length - 2].is_number) {
>    if (skip_alt[expr[expr_length - 2].value][op])
>      return false;
>    else if (expr[expr_length - 3].is_number
>        && expr[expr_length - 3].value < expr[expr_length - 1].value
>        && skip_alt_asc[expr[expr_length - 2].value][op])
>      return false;
> }
> 
> (`expr' is a fixed-size array of structs, I increase `expr_length' as I 
> fill it in. `skip_*' are two dimensional arrays of booleans, but I don't 
> have any problem regarding them.)
> 
> And it looks ugly; barely readable even with comments. I want it to be 
> readable at least. Possible emendations I can think of are:
> 
> 1. Define macros for `expr[expr_length - 1]', `expr[expr_length - 2]', 
> etc. and use them instead. But, what am I gonna name them?
> 
> 2. Define a macro `expr_end' for `(expr + expr_length)', and use 
> `expr_end[-1]', `expr_end[-2]', etc. instead. But I'm afraid this might 
> still be hard to understand for someone who's not familiar with the code 
> (e.g. me 3 months later from now).
> 
> So, do you guys have better ideas for making this piece of crap a bit 
> more readable (other than rewriting it in C++)? Thanks in advance.

Maybe Öö Tiib still has a point: it's probably more about the logic than 
aesthetics - I mean, your code might be logically correct, but it is the 
underlying model that is not clear.
Often, in this kind of thing, a clear code is the result of:
- clear requirements
- clear logic modeled upon such requirements
- then, translation of model into code is often also clear.

What makes me wonder most about those lines is that all that if/else 
intricacy is done to yield a true/false flag - is this really the best 
possible design of what you are trying to do (whatever it may be)?

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


Thread

A question about style Oğuz <oguzismailuysal@gmail.com> - 2021-12-30 09:18 +0300
  Re: A question about style Öö Tiib <ootiib@hot.ee> - 2021-12-30 04:42 -0800
    Re: A question about style Öö Tiib <ootiib@hot.ee> - 2021-12-30 04:47 -0800
      Re: A question about style James rock <jamesroack12@gmail.com> - 2021-12-30 15:40 +0000
      Re: A question about style Oğuz <oguzismailuysal@gmail.com> - 2021-12-30 19:24 +0300
  Re: A question about style Manfred <noname@add.invalid> - 2021-12-30 18:34 +0100
  Re: A question about style Bart <bc@freeuk.com> - 2021-12-30 18:14 +0000
  Re: A question about style Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-30 19:43 +0100
    Re: A question about style Meredith Montgomery <mmontgomery@levado.to> - 2021-12-30 16:56 -0300
  Re: A question about style Paul N <gw7rib@aol.com> - 2021-12-30 12:15 -0800
    Re: A question about style Oğuz <oguzismailuysal@gmail.com> - 2021-12-31 09:05 +0300
  Re: A question about style Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-12-30 14:01 -0800
    Re: A question about style Oğuz <oguzismailuysal@gmail.com> - 2021-12-31 09:11 +0300
  Re: A question about style Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-30 16:22 -0800
  Re: A question about style Jens Schweikhardt <usenet@schweikhardt.net> - 2022-01-02 13:28 +0000

csiph-web