if false keyword, alternative to bang ! operators

Topics: C# Language Design
Oct 27, 2014 at 7:26 PM
I find the bang operator ! to be extremely easy to miss in both creation of negative if statements and in reading an if statement and understanding the boolean logic it represents.

Some possible usages:
iffalse(true == false)
if false(true == false) // i like this one 
if(mybool) false //probably my favorite
if(mybool is false)
if_false(1 > 2)
if-false(0 < -1)
iff(false) // i really don't like this one
false(mybool) 
Oct 27, 2014 at 9:14 PM
I would think this would be helpful when dealing with things like three-way logic where it would be possible for (bool)x to be false but for (bool)(!x) to also be false. Saying if false(someCondition) execute the following code under the opposite condition from if (someCondition).
Oct 28, 2014 at 5:03 AM
dotnetchris wrote:
I find the bang operator ! to be extremely easy to miss in both creation of negative if statements and in reading an if statement and understanding the boolean logic it represents.

Some possible usages:
iffalse(true == false)
if false(true == false) // i like this one 
if(mybool) false //probably my favorite
if(mybool is false)
if_false(1 > 2)
if-false(0 < -1)
iff(false) // i really don't like this one
false(mybool) 
You never have to use the ! operator if you don't want to. I work with a developer who writes == false everywhere rather than use the operator. I don't see why you would possibly need additional language support for this.
Oct 28, 2014 at 9:10 AM
I'd like this too but think it's way too much buck for way too little bang. But Perl has the perfect name for this conditional - unless.
Oct 28, 2014 at 2:35 PM
Edited Oct 28, 2014 at 2:42 PM
JesperTreetop wrote:
I'd like this too but think it's way too much buck for way too little bang. But Perl has the perfect name for this conditional - unless.
So this would be
unless(mybool) { 
   code code code 
}
?
This still feels very positive connotation that it would expect mybool to be true.

Looking up perl posts about unless you mentioned showed me other syntax i really liked. I'm not sure if it's valid perl or just pseudo code but it was
if(not mybool)
Not operator would be a good alternative to ! also

Ultimately i always strive for human readable code especially regarding logic flow. I feel a business person should be able to review code and have a conceptual understanding of it. My current work around for lack of alternatives to bang is
if(@int.HasValue.Equals(false))
Oct 28, 2014 at 4:24 PM
JesperTreetop wrote:
I'd like this too but think it's way too much buck for way too little bang.
I see what you did there.

I think ! is appropriate given the heritage of the syntax of the language. There are a few situations where it gets annoying combined with other operators, like is, requiring wrapping in additional parentheses, which is kind of annoying. Perhaps addressing those scenarios would be better.
Oct 29, 2014 at 9:10 AM
Edited Oct 29, 2014 at 9:26 AM
Halo_Four wrote:
JesperTreetop wrote:
I'd like this too but think it's way too much buck for way too little bang.
I see what you did there.
Haha! Believe it or not, that wasn't intentional.
I think ! is appropriate given the heritage of the syntax of the language. There are a few situations where it gets annoying combined with other operators, like is, requiring wrapping in additional parentheses, which is kind of annoying. Perhaps addressing those scenarios would be better.
It certainly fits with the rest of the language. And I agree that the only actual problems are the lack of an "ain't" operator and having to go back and forth to parse out negation in large conditionals. Nothing that couldn't be solved by tidying up the code.

dotnetchris wrote:
So this would be
unless(mybool) { 
   code code code 
}
?
This still feels very positive connotation that it would expect mybool to be true.
That's how it would be used. It doesn't feel weird in actual use. Obviously, if you end up saying unless (!something), it gets a bit weird. Additionally, in Perl, they have postfix conditionals on expressions - meaning you can say exit() unless $isValid; .
Looking up perl posts about unless you mentioned showed me other syntax i really liked. I'm not sure if it's valid perl or just pseudo code but it was
if(not mybool)
Not operator would be a good alternative to ! also
The not operator in Perl seems to be there only for completeness with the other logical operators and they are spelled out. And you can certainly use ! in Perl too. (That whole page is interesting reading - Perl uses different operators for numeric and string equality and comparison, for example.)
Ultimately i always strive for human readable code especially regarding logic flow. I feel a business person should be able to review code and have a conceptual understanding of it. My current work around for lack of alternatives to bang is
if(@int.HasValue.Equals(false))
That's what I strive towards too - but I'll never reach that goal, since the purpose of code is to execute all the niggling details necessary to make things happen. The most you'd be able to do is have that level of clarity for some abstractions. The conceptual understanding requires a shift in thinking beyond just readable code. That said, being able to actually understand your own code is a worthwhile goal unto itself.
Oct 29, 2014 at 12:15 PM
JesperTreetop wrote:
Halo_Four wrote:
JesperTreetop wrote:
I'd like this too but think it's way too much buck for way too little bang.
I see what you did there.
Haha! Believe it or not, that wasn't intentional.
I think ! is appropriate given the heritage of the syntax of the language. There are a few situations where it gets annoying combined with other operators, like is, requiring wrapping in additional parentheses, which is kind of annoying. Perhaps addressing those scenarios would be better.
It certainly fits with the rest of the language. And I agree that the only actual problems are the lack of an "ain't" operator and having to go back and forth to parse out negation in large conditionals. Nothing that couldn't be solved by tidying up the code.

dotnetchris wrote:
So this would be
unless(mybool) { 
   code code code 
}
?
This still feels very positive connotation that it would expect mybool to be true.
That's how it would be used. It doesn't feel weird in actual use. Obviously, if you end up saying unless (!something), it gets a bit weird. Additionally, in Perl, they have postfix conditionals on expressions - meaning you can say exit() unless $isValid; .
Looking up perl posts about unless you mentioned showed me other syntax i really liked. I'm not sure if it's valid perl or just pseudo code but it was
if(not mybool)
Not operator would be a good alternative to ! also
The not operator in Perl seems to be there only for completeness with the other logical operators and they are spelled out. And you can certainly use ! in Perl too. (That whole page is interesting reading - Perl uses different operators for numeric and string equality and comparison, for example.)
Ultimately i always strive for human readable code especially regarding logic flow. I feel a business person should be able to review code and have a conceptual understanding of it. My current work around for lack of alternatives to bang is
if(@int.HasValue.Equals(false))
That's what I strive towards too - but I'll never reach that goal, since the purpose of code is to execute all the niggling details necessary to make things happen. The most you'd be able to do is have that level of clarity for some abstractions. The conceptual understanding requires a shift in thinking beyond just readable code. That said, being able to actually understand your own code is a worthwhile goal unto itself.
The problem I have with unless is that the term does not feel like negation. It feels like a synonym for if. I also don't think that !s are hard to read.
Oct 29, 2014 at 4:04 PM
Edited Oct 29, 2014 at 6:14 PM
My alternative proposal would be
ifnot(false)
{}
I am also in fav of the idea of adding alternative operators for logical opeators(just like python and coincidentally cpp) such that:
! not 
&& and
|| or
so the constuction
if(not true)
{}
would seem logical

Iit would require some changes in parser... because of alpha characters used as operator compiler would have to check for spaces in between these, name identifiers and other keywords.

EDIT:
ifnot operator however would have that advantage it could swallow composite expressions without a need for additional parenthesis

PS. Right now... some crazy alternative(surely more eyebanging) to bang is triple bang
if(!!!IsGood())
Oct 29, 2014 at 4:11 PM
arekbal wrote:
I am also in fav of the idea of adding alternative operators for logical opeators(just like python) such that:
! not
&& and
|| or
I really like this. That hadn't even crossed my mind to include AND and OR. Those would be great additions. They would definitely help me on my quest of producing as close to human english readable code.

Does VB support AND, OR, and/or NOT?
Oct 29, 2014 at 6:54 PM
VB has AND, OR that dont short circuit evaluate. ANDALSO, ORELSE are. NOT is boolean complement \ negatation.

If you are after making you source read more like psuedocode \ or written pudgen english then vb is for you.
Also consider renaming the idenifier if you use the complement more. Eg If Not_Active Then
Nov 1, 2014 at 10:27 PM
You have my vote for 'if(not mybool)'
Nov 1, 2014 at 11:08 PM
Why not just VB instead?
Nov 7, 2014 at 10:26 PM
How about simply
if !(expression)
So I could do
if !(myvalue is string)
instead of the more cumbersome
if (!(myvalue is string))