Amar Sagoo

28 April 2005

You but me

It would be nice if English-like programming languages such as BASIC or AppleScript allowed using but in place of and.

Logically, and and but are the same; only but carries some additional semantics implying contradiction or contrast. (Note how I cleverly avoided using two successive buts there).

Why bother? Well, consider the following example:

If License.IsValid And License.IsBlackListed Then
    LectureUserAboutPiracy
End If

Now read this:

If License.IsValid But License.IsBlackListed Then
    LectureUserAboutPiracy
End If

Although interpreted the same by the computer, the second version makes the programmer's intention more obvious to a reader of the code.

This is similar in spirit to how you can use the in AppleScript and HyperTalk to make code more readable without affecting its behaviour. But this would be even more useful, because complex logical propositions can be quite a pain to decipher when you don't know the original intention. In fact, even constructing them can be difficult when you're having to translate from real-world terms into and and or-terms.

6 comments:

Sven said...

Interesting thoughts, but doesn't this put some strains on making sure the variables will sound reasonable in such usage.

You also need to take into account that "but" always sounds like an objection or negative comment - "I did finish my homework, but the dog ate it".

Sven said...

Oh, and I believe AppleTalk is a protocol for communication. AppleScript is, however, a scripting language where the article 'the' is optinal, and it does indeed make the language easier to cope with for beginners.

Amar said...

Thanks Sven, I did mean AppleScript. I corrected this in the post.

Z. D. Smith said...

wouldn't it be fairly trivial to define the 'but' operator as a synonym of 'and'? I'm not familiar with applescript, but in general this doesn't seem like a major difficulty.

Amar said...

Sven, regarding your first comment: I'm not sure I understand. I am only proposing "but" as an optional substitute for "and" that you use when appropriate, not as a permanent replacement, if that's what you thought. Sorry if I misunderstood completely.

TyskJohan said...

Well, that's similar to what I learnt in perl but lack everywhere else: unless(~), which is a replacement for if(!(~))