Search code examples
c#programming-languagesreadability

Why C# doesn't accept the boolean operators as words ("and", "or", "not" ,..etc) with the current operators?


My if stetement:

if (!string.IsNullOrEmpty(Person.Name))
        // some code

I think the "!" operator is less readable when writing imperative code,

Are you agree with me the following code is more readable?

if (not string.IsNullOrEmpty(Person.Name))
            // some code

I'm not talking about replace the current operators, but where's the problem if the language accept them both ?


Solution

  • There are a couple of good reasons:

    • There are some fine points that the words don't convey too well. For example, a short-circuit AND operator vs one that doesn't short-circuit (&& vs &). VB recently(ish) introduced a short-circuit AND operator called AndAlso. That never made any sense to me. It seems to confuse people from the start.

    • Less typing. Boolean logic is used everywhere, and a couple of keystrokes saved on each expression can add up to a lot -- or at least, I've found that having to type more is pretty annoying.

    • Because it's what C, Java, Objective-C, and C++ programmers are used to. People have been using this syntax for 40(?)-ish years now. It lends an air of continuity to the languages that people seem to like.

    • "and", "or", and "not" are English words, and a syntax using glyphs like ! and &&/& is more linguistically and culturally neutral.

    • Adding more operators which mean the same thing is unnecessary for the operation of the language, and can be confusing.