const assignments problem again
Lutger Blijdestijn
lutger.blijdestijn at gmail.com
Sun Aug 7 05:01:56 PDT 2011
bearophile wrote:
...
> In my precedent post about this topic I have discussed "nested constness"
> and another partially silly idea. Recently I have seen a language that
> suggests me this:
>
> // version #6
> const foo;
> if (abs(e.x - v.x) > double.min)
> foo = (v.y - e.y) / (v.x - e.x);
> else
> foo = double.max;
>
>
> The compiler makes sure all paths assign values with the same type to foo
> (as in the case of the two returns inside the delegate, that must be of
> the same type). But if you introduce goto instructions version #6 looks
> fragile. If the assign is far away from the definition the code looks not
> so nice any more, so this feature is meant for short-range initializations
> only.
>
> Bye,
> bearophile
I like the ternary operator the best for this, as it is the simplest. I
always write them like this though, liberally include parenthesis and never
nest:
(condition)
? (truth value)
: (false value)
When it gets more complicated, you can always rewrite either the whole
expression (to if/else or a function) or refactor parts of the expression to
small functions. I never find this to be much of a burden.
Python has if/else expressions, but due to it's (messy) scoping rules I
almost never find them an improvement to if/else statements.
I like the single assignment feature, but not for this reason. I think it
would be more useful for creating immutable data in general.
More information about the Digitalmars-d
mailing list