in reply to Useless unless

I agree with tilly's Re^2: Useless <c>unless</c>. I don't use unless frequently in my code. The only place I use it is in the following construct -  die $usage unless (@ARGV>0);. Every other place I switch to if as it is easy to read (at least to me)

That said, I would not advocate for removing this feature from the language itself. I feel removing a feature should be done if pretty much (subjective huh?) everyone in the user community do not use it (or) the feature has a significant overhead to the parser and has little benefit or other reasons that my mind cannot grasp.

I am aware of unless traps. But I cannot blame the lang if i fall for it. I always have the choice to pick if.

Regarding the otherwise suggestion i like it, if it is not going to span multiple lines. What i miss the most is something like this -

doX() if ($cond) otherwise doY();

I know i could do

if ($cond) { doX(); } else { doY(); }

(or)

($cond) ? doX() : doY();

The last one is pretty much the same as otherwise but just that otherwise gives a natural lang feel

-SK

Replies are listed 'Best First'.
Re^2: Useless unless
by BrowserUk (Patriarch) on Aug 07, 2005 at 07:16 UTC
    doX() if ($cond) otherwise doY();

    I think I may be the only person in the world that would (actively) prefer to see that as:

    $cond and doX or doY;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      What happens if doX returns a false value?
        I think BrowserUk's point was on functions that don't return a value. I was assuming that in my post o/w I need to use the ternerary operator to catch it. Well one might argue that it is not always required to trap the return value but I feel if the funcion returns something then it is worth something

        I still like the otherwise idea coz it makes the code more english like. Nothing more, nothing less. :) I cannot argue with BrowserUk on the equivalence of the code! :)They do the same thing!

        Obviously, if there was any chance of that it would have to be

        $cond and doX,1 or doY;

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.