But Why?

I often want to use an operator called “but”, the complete equivalent to “and”. It would let me say:

if guest.wants_to_see(report) but report.is_premium_content
    raise PremiumContentException

As opposed to:

if guest.wants_to_see(report) and report.is_premium_content
    raise PremiumContentException

The first example is easier to comprehend because it’s closer to the business rule and the way we think about it. You could argue it’s pointless to add redundant terms to a language, but 100101100101001010010111010101.

So there are times when “but” is clearer than “and”. Likewise, there are times when “and” is clearer than “but”, which implies that we need both. Here’s an example of the latter.

if guest_user.wants_to_see(report) and report.is_public

I like how some languages include redundant words like “unless” and redundant constructs like “do … while”. Some say they create many ways to do the same thing (Perl argument), but that’s often not the case; there’s often one way that’s the clearest, as the examples above illustrate.

So do any languages have a “but”? I see that Perl does, but it apparently does something else. Also, can any Ruby gurus suggest a way to bake this into the Ruby syntax?

3 thoughts on But Why?

  1. “but” is simply a negation of the and clause –

    if guestuser.wantstosee(report) and not report.ispublic raise PremiumContentException end

    Sorry…java programmer, so no idea if my negation syntax is correct…but you should get the idea.

Leave a Reply to Anonymous Cancel reply