while(...){ ... }else ...

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Sun Jan 18 15:34:00 PST 2015


On Sun, 18 Jan 2015 23:16:11 +0000
Jens Bauer via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> I'd like to give an example on a different use of the same 
> feature:
> 
> while(length--)
> {
>      *d++ = *s++;
> }
> if(length == -1)  /* the programmer will need to know the value 
> of length here. */
> {
>      /* handle case where length was exactly 0, it's now -1 */
> }
> 
> would become:
> 
> while(length--)
> {
>      *d++ = *s++;
> }
> otherwise
> {
>      /* handle case where length was exactly 0, it's now -1 */
> }
> 
first sample is just a bad code, but second sample is a kind of cypher.

there is no sense in doing such microoptimisations, as decent modern
compiler is able to do dataflow analysis and cse. and both samples are
bad, 'cause they violates the rule "check first, then process".

did you profile your code and found that such loops are real
bottlenecks? i bet they aren't. yet they less readable than clearly
spelled preconditions.

this is microoptimisation in the sake of microoptimisation, it wins
almost nothing and hurts code readability.

all in all it's one of that things that looks nice, but makes more
problems than they are able to solve. that's why language design is
hard: nicely looking features can have many unforseen consequences. ;-)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20150119/8a8dacc7/attachment.sig>


More information about the Digitalmars-d mailing list