Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Memorizing C operator precedence
Date: Mon, 25 Apr 2022 14:48:48 -0700
Organization: A noiseless patient Spider
Lines: 57
Message-ID: <86pml46dhb.fsf@linuxsc.com>
References: <20220413163721.82@kylheku.com> <6295432d-6ca9-49ea-b29c-858de4b65b75n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="2b2bd3060d7fb14ae545fc6b32585799"; logging-data="7476"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX189xLGD73fKQK6WgxtErLVoLKoz2UMjmKE="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:vcaxcr9sdOURJElBPYxsRD+cZqE= sha1:Y+04RcK9jjj6lIQKwonD1BAX0vo=
Xref: csiph.com comp.lang.c:165934
Paul N writes:
> On Saturday, April 16, 2022 at 3:28:00 PM UTC+1, Alan Mackenzie wrote:
>
>> Stefan Ram wrote:
>>
>>> Alan Mackenzie writes:
>>>
>>>> Breaking up a complicated expression _fragments_ it
>>>
>>> Example for the refactor "extract variable" in pseudocode:
>>>
>>> return quantity * item_price -
>>> max(0, quantity - 500) * item_price * 0.05 +
>>> min(quantity * item_price * 0.1, 100);
>>>
>>> ------------------>
>>>
>>> const base_price = quantity * item_price;
>>> const quantity_discount =
>>> max( 0, quantity - 500 )* item_price * 0.05;
>>> const shipping = min( base_price * 0.1, 100 );
>>> return base_price - quantity_discount + shipping;
>
> How about a compromise?
>
> return quantity * item_price
> - max(0, quantity - 500) * item_price * 0.05 // quantity discount
> + min(quantity * item_price * 0.1, 100); // shipping
Speaking for myself I would rather see something like this:
[...]
unsigned n = quantity;
double price = item_price;
return sale_charge( n, price ) + shipping_charge( n, price );
double
sale_charge( unsigned n, double price ){
return base_charge( n, price ) - quantity_discount( n, price );
}
double
shipping_charge( unsigned n, double price ){
return min( base_charge( n, price ) * 0.1, 100.0 );
}
double
base_charge( unsigned n, double price ){
return n * price;
}
double
quantity_discount( unsigned n, double price ){
return n > 500 ? (n-500) * price * 0.05 : 0;
}