On 02/10/11 13:49, Andrej Mitrovic wrote:
> On 2/10/11, Walter Bright <newshou...@digitalmars.com> wrote:
>> auto x = (localtime().hours >= 8) ? "awake!" : "asleep, go away.";
> 
> Aye, a one liner!
> 
> I hate seeing things like this:
> if (funcall())
> {
>     var = "foo";
> }
> else
> {
>     var = "bar";
> }
> 
> So much clutter instead of using the simple:
> var = funcall() ? "foo" : "bar";
> 
> I also see this sometimes:
> 
> auto var = funcall();
> if (var == "foo" || var == "bar" || var == "foobar" || var == "barfoo")
> {
>     // some complex code
> }
> else if (var == "blue" || var == "green")
> {
>     // some complex code
> }
> else if ()// more and more code..
> { }
> 
> But not many people seem to know that D supports strings in case statements:
> switch(funcall())
> {
>     case "foo":
>     case "bar":
>     case "foobar":
>     case "barfoo":
>     {
>         // complex code
>     }
>     break;
>     case "blue":
>     case "green":
>     {
>        // complex code
>     }
>     break;
>     default:
> }
> 

Even better:

switch( funcall() ) {
    case "foo", "bar", "foobar", "barfoo": {
        // complex code
        break;
    }

    case "blue", "green": {
        // complex code
        break;
    }

    default:
        // do nothing -- i like to comment defaults
}

Also often forgotten, that 'case' clauses take an argument list, not
just an expression.  And yeah, in this case at least... it still fits in
80 columns.  (I prefer 90 myself, but it's moot.)

-- Chris N-S

Reply via email to