3

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 ?

Homam
  • 23,263
  • 32
  • 111
  • 187
  • 9
    Don't agree.. It's the same once you get used to it. – Oren A Sep 04 '10 at 13:50
  • 2
    The problem? Suddenly you can never name a variable or field "not". If you're not used to "!" yet, don't worry, you will be eventually. – Kirk Woll Sep 04 '10 at 13:50
  • Personally, I agree that it is more readable, and I prefer to use VB.NET instead of C# for that very reason. I don't mind C#, indeed it was my first .NET language, and I'm quite happy programming in either as the client wishes. But although I prefer the verbosity of VB, I wouldn't want C# to be made more VB-like. – ChrisA Sep 04 '10 at 14:02
  • 1
    If you don't like it, you can pick another language, such as VB.NET... the C# language is not going to change just because you don't like it's operators ;) – Thomas Levesque Sep 04 '10 at 14:03
  • 5
    @Kirk: Who will name a variable or field `not`? – kennytm Sep 04 '10 at 14:07
  • 1
    @KennyTM: Apparently (according to google) "not" means "musical note" in Indonesian. Maybe an indonesian programmer working on some music app. Or me loaded up on cheap bourbon trying to play a trick on my fellow programmers. – Dave Markle Sep 04 '10 at 14:19
  • Interestingly enough, in C++ you could define macros named like that which were substitutes for the symbolic operators. My brother-in-law wrote macros called AND OR NOT that behaved this way and liked using them. I never liked it... but macros aren't supported in C# so its kind of moot, if not interesting. – Nick Sep 04 '10 at 14:37
  • @Thomas Levesque, i think he is coming to C# from VB.NET :P – Garis M Suero Sep 04 '10 at 15:07
  • @KennyTM, here's an example: Func not = gender => gender == Gender.Male ? Gender.Female : Gender.Male; Perhaps you should give the *not* function a longer variable name. Or perhaps not. – Kirk Woll Sep 04 '10 at 15:56
  • 1
    @Kirk: You should call it [`sex_change`](http://en.wikipedia.org/wiki/Sex_change_%28disambiguation%29) :p. – kennytm Sep 04 '10 at 16:09
  • Sounds good, I think VB.NET does have this. But generally the trend is to go shorter expression nowadays.Inclusion of Lamdba expression is an example of such. So I think I am ok with ! operator. – abhishek Sep 04 '10 at 16:13
  • If you want "and", "or", and "not" to be keywords, then you want a different language. Try Delphi Prism (http://www.embarcadero.com/products/delphi-prism), which has those keywords exactly as you want them, and is (AFAIK) as full-featured as C#. – Joe White Sep 04 '10 at 19:56
  • Symbols take a bit of getting used to, but the fact that they're visually distinct from identifiers is an advantage. My wish for C would be an "if!" statement which would swap the true and false clauses, without performing an extra operation on the expression (I'm sure nearly any decent compilers would omit the negation instruction with a primitive type, but if "foo" is a custom type I don't think a compiler can assume that "!foo" will evaluate as true when "foo" is false or vice versa. – supercat Sep 05 '10 at 00:18
  • IMHO, the readable way would be if(Person.Name) ^^ – Guillaume86 Jan 29 '11 at 22:51

4 Answers4

11

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.

Dave Markle
  • 95,573
  • 20
  • 147
  • 170
  • 10
    @lomaxx: Language neutral is the weakest point imho. Most documentation is in english anyway. All the other keywords are english anyway (should we replace `class` by `#$-` to be language-neutral?). –  Sep 04 '10 at 14:01
  • +1 for the difference between && and &, || and | that using AND and OR cannot easily represent. – Ian Johnson Sep 04 '10 at 14:10
  • 3
    Actually [C++ support `and`, `or` and `not` natively, and C too by including ``](http://en.wikipedia.org/wiki/Iso646.h). – kennytm Sep 04 '10 at 15:06
  • @KennyTM: That's very interesting -- I didn't know that. It would be really interesting to know the reasoning behind putting this in the standard library. Ill bet there were fights! – Dave Markle Sep 04 '10 at 16:13
  • 1
    @Dave Markle - Because some keyboards (non-QWERTY keyboards in particular) at the time either didn't have keys for the boolean operators, or the layout was such that typing them was not easy. – In silico Sep 04 '10 at 16:21
7

If you want your code to be more "readable" then perhaps VB is a better option for you. It is completely valid to have statements like

If value Is Not Null Then
   'do something
End If

I personally find that the c# syntax of ! and && and || to be very readable because they're consistant across a number of languages and also tend to follow the conventions used in mathematics where these operations often gain their origins.

It also means there is no ambiguity to their meaning as it is a symbol.

lomaxx
  • 113,627
  • 57
  • 144
  • 179
  • 5
    On VB and friends: `Projects promoting programming in "natural language" are intrinsically doomed to fail.` -- Dijkstra –  Sep 04 '10 at 13:58
1

The problem, in a word, is complexity. From a usability perspective, you don't want to confuse developers with multiple ways of doing the same thing. From a compiler perspective, the more tokens you have, the worse the performance. Clearly one more token won't make a difference, but think of all the other redundancies others might ask for. Better to force everyone to do things the same way. At least that way we'll be sure to keep coming back to Stack Overflow!

user392650
  • 51
  • 3
0

I have an interesting point in mind. Often you want to use the properties of the language. If you have a logical expression in normal form, where the main operator in the Polish tree of the logical operations is an AND (&&) and the first operand is false, the language won't calculate the value of the other operands. Similarly, if the main operator in the Polish tree of the logical operations is an OR (||) and the first operan is true, the other operands won't be calculated. Let's see a few examples

//...
// Position is a class
private bool checkMate(ref int numberOfPlies, ref ChessPosition position, int maxNumberOfPlies, List<Move> moves)
{
    if (numberOfPlies > maxNumberOfPlies)
        return false;
    position.makeMove(moves[numberOfPlies]);
    if (!(position.getSuccessOfLastMove())))
        return false;
    numberOfPlies++;
    return ((
            ((isCheckMate()) ||
            ((checkMate(ref numberOfPlies, ref position, maxNumberOfPlies, moves)))
           ));
}
//...

The above method checks whether a varitation leads to a checkmate from a given chess position and the checkmate is given in a certain amount of plies. Position is a class, its makeMove() method makes a move in the given position, its getSuccessOfLastMove() is true if the last move was valid and is false if the last move was not valid. Move is another class. Ply is a half-move in chess (Move = white moves and black moves, Ply = White or Black made a move). isCheckMate checks whether a given position is a checkmate.

We don't want to call checkMate if isCheckMate() is true, so we don't need other operation here than the usual ||.

However, here we want to evaluate all the operands:

//... bool isThisFlowerYellowOrYellowish(Flower flower) { return (((flower.flowerColor == flower.yellow) OR (flower.FlowerColor == flower.yellowish)) AND (flower.setColorToYellowIfTheColorIsYellowish())); } //...

The code above is not a valid C# code, but let's see why we need an OR in C#. In the example above we want to run the setColorToYellowIfTheCollorIsYellowish() even if we know the result of the total evaluation before evaluating the function, but the code is not working because we don't have an OR operation which calculates all the operands even if we can know the result. If this was possible, we would be able to not write the function in an alternative way.

//...
bool isThisFlowerYellowOrYellowish(Flower flower)
{
    if (flower.FlowerColor == flower.yellowish)
        return flower.setColorToYellowIfTheColorIsYellowish();
    return (flower.flowerColor == flower.yellow);
}

//...

This is disgusting compared to the second example in my opinion. If we had an "OR" and an "AND" operator which are not optimised and evaluates all the operands no matter what, then the language would be richer with a feature, but, we can survive without these operators, because we have alternative solutions when needed and this is a very rare case.

In my opinion we don't need a "NOT" operator, because that's a unary operator and it would be similar with the ! operator.

In my opinion we shouldn't change the !, ||, &&, & and | operators because all the programmers are used to them. If they are changed in C#, the language will not have backwards compatibility.

EDIT:

On a second thought, the language would have backwards compatibility if the old operators' meaning would be unchanged, regardless of whether the keywords of not, or, and would be introduced.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175