! or 'not' to validate an expression in an if statement as false? Topic is solved

Talk about anything
Leli196
Posts: 188
Joined: 29 Aug 2015, 05:47

! or 'not' to validate an expression in an if statement as false?

15 Apr 2016, 16:37

Hey guys,

If you want to validate an expression in an if statement as false there are mainly two ways to do so: with an ! or with a not.
I would like to know which way you prefer and whether there are any advantages or disadvantages with any of the methods. Some examples:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



So what do you think?
Shadowpheonix
Posts: 1258
Joined: 16 Apr 2015, 09:41

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 10:41

! is slightly less typing, and not is easier to read. As far as I know, there are no other differences.

I personally tend to use ! when dealing with a simple If !var, and not when dealing with anything more complex.
wolf_II
Posts: 1959
Joined: 08 Feb 2015, 20:55

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 11:17

I fully agree with Shadowpheonix, with a slight adjustment:
Documentation wrote:Logical-NOT. Except for its lower precedence, this is the same as the ! operator. [...]
Logical-not (!): [...] Note: The word NOT is synonymous with ! except that ! has a higher precedence.

An example with precedences: ! > + > Not

Code: [Select all] [Download] GeSHi © Codebox Plus

Test1 := Not False + True   ; Not 1 = 0
Test2 := ! False + True ; 1 + 1 = 2
MsgBox, %Test1%`n%Test2%
Leli196
Posts: 188
Joined: 29 Aug 2015, 05:47

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 11:31

Interesting. So would you recommend to solely use ! instead of not because it is much shorter - what is especially with large expressions advantageous - but then give up the intuitive appearance of not?

Greetings Leli196
Guest

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 12:18

You can use both like many of us do. Alternating them as you feel.
Ah, such freedom :)
Leli196
Posts: 188
Joined: 29 Aug 2015, 05:47

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 12:37

True, but I am somehow really into consistency therefore I try to find some "rules" so that I do not find myself brooding over this all the time xD. Some thing that comes to my mind is to use not when it comes to functions, e.g. if not GetKeyState("a", "P") and when it comes to vars !, e.g. if (!window1_ID and !window2_ID) or (!window3_ID)

Greetings Leli196
User avatar
tidbit
Posts: 1083
Joined: 29 Sep 2013, 17:15
Location: USA

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 12:42

I always use !. symbols stand out more than words. using "not" "and" "or" in if's and friends might get confusing since they might look like variables/text. a symbol breaks stuff up nicely.

Code: [Select all] [Download] GeSHi © Codebox Plus

if (var and var2)
if (var && var2)
if (not var or var2=3)
if (!var || var2=3)

plus, "!=" exists, there's no "not=". so it helps with consistency.

I also always put ()'s on my if's, for consistency. if's without ()'s can behave funky. just always use ()'s to be safe, IMO
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
Shadowpheonix
Posts: 1258
Joined: 16 Apr 2015, 09:41

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 13:18

tidbit wrote:I always use !. symbols stand out more than words. using "not" "and" "or" in if's and friends might get confusing since they might look like variables/text. a symbol breaks stuff up nicely.

Code: [Select all] [Download] GeSHi © Codebox Plus

if (var and var2)
if (var && var2)
if (not var or var2=3)
if (!var || var2=3)

plus, "!=" exists, there's no "not=". so it helps with consistency.

I also always put ()'s on my if's, for consistency. if's without ()'s can behave funky. just always use ()'s to be safe, IMO


My problem is that when looking at the more complex if statements like if (!var || var2=3), I have a tendency to read it as lvar instead of !var.

I absolutely agree about always using ()'s though. :D
User avatar
tidbit
Posts: 1083
Joined: 29 Sep 2013, 17:15
Location: USA

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 13:26

TBH, I never use simply "var" or "!var". that confused the crap out of me as a noob. "if var what?!?!". and the confusion pops up on IRC.forums every now-and-then.
I try to always show what it's comparing to. if (var>=1) or if (var!="") aswell as the true/false keywords.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
Just_Be

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 13:57

admit it, there are some things that at times confuse you today as well... 8-)

I still go into ternary limbo at times or get the "do i need to put %var% now or var...?!"-fever.
User avatar
Exaskryz
Posts: 2863
Joined: 17 Oct 2015, 20:28

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 14:07

Shadowpheonix wrote:My problem is that when looking at the more complex if statements like if (!var || var2=3), I have a tendency to read it as lvar instead of !var.

I absolutely agree about always using ()'s though. :D


I think that may be a problem with your font though. ! vs l is pretty clear to me in my Notepad++. I'm using courier new: !lIi1
Shadowpheonix
Posts: 1258
Joined: 16 Apr 2015, 09:41

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 14:56

Exaskryz wrote:
Shadowpheonix wrote:My problem is that when looking at the more complex if statements like if (!var || var2=3), I have a tendency to read it as lvar instead of !var.

I absolutely agree about always using ()'s though. :D


I think that may be a problem with your font though. ! vs l is pretty clear to me in my Notepad++. I'm using courier new: !lIi1


I think it is more a matter of me trying to speed-read when I am not very good at doing so. No issues if I actually slow down and deliberately read every word. :lol:
Leli196
Posts: 188
Joined: 29 Aug 2015, 05:47

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 15:28

tidbit wrote:I always use !. symbols stand out more than words. using "not" "and" "or" in if's and friends might get confusing since they might look like variables/text. a symbol breaks stuff up nicely.

Code: [Select all] [Download] GeSHi © Codebox Plus

if (var and var2)
if (var && var2)
if (not var or var2=3)
if (!var || var2=3)

plus, "!=" exists, there's no "not=". so it helps with consistency.

I also always put ()'s on my if's, for consistency. if's without ()'s can behave funky. just always use ()'s to be safe, IMO


How about the other people here? Do you use and/or or &&/||? Until now I have been always using the former and did not question it. However tidbit's point seems very reasonable and I could think of changing my behavior and use the latter.

Furthermore it would be interesting to know whether you prefer != over <> or the other way around.

Greetings Leli196
wolf_II
Posts: 1959
Joined: 08 Feb 2015, 20:55

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 15:55

I use And, Or or Not, when I see a chance, that I might still be able to understand what I just coded in about 2 months time.
(Increased readability, you know, self-explaining code.)

I use &&, || or ! along with comments, if I can see that my code is too complex for that.

I prefer != over <> because it looks closer to that I know from school.
User avatar
Exaskryz
Posts: 2863
Joined: 17 Oct 2015, 20:28

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 16:26

I guess I hadn't chimed in with my use of !. I like the ! for the reason tidbit gave with it being visually outstanding from commands and variables. That applies to || and && too.

I use != because that reads left to right to me as "not equals", which is what that means. The <> just isn't intuitive to me.
User avatar
tidbit
Posts: 1083
Joined: 29 Sep 2013, 17:15
Location: USA

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 16:34

I hate <>.
! means "not" = means "equals". not equals. makes sense. "var not equals 5"
< means "less than" > means "greater than". less than greater than. makes no sense. "var less than greater than 5"

I'll move this topic to off-topic. it's not really asking for help, just formatting/style prefs.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
Leli196
Posts: 188
Joined: 29 Aug 2015, 05:47

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 16:45

tidbit wrote:I'll move this topic to off-topic. it's not really asking for help, just formatting/style prefs.

True. I didn't know where to put something like this, now I know better for the future.

tidbit wrote:I hate <>.
! means "not" = means "equals". not equals. makes sense. "var not equals 5"
< means "less than" > means "greater than". less than greater than. makes no sense. "var less than greater than 5"

Well, it probably means something like less than or greater than , that makes sense to me. However, this may be somehow reasonable when used with numbers; but when it comes to strings less than or greater than is rather stupid.

Greetings Leli196
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: ! or 'not' to validate an expression in an if statement as false?

18 Apr 2016, 17:46

Leli196 wrote:Well, it probably means something like less than or greater than , that makes sense to me. However, this may be somehow reasonable when used with numbers; but when it comes to strings less than or greater than is rather stupid.
But there's no "or" symbol in there :P To me (and clearly others), it simply reads as "less than greater than", which would essentially be equivalent to <= (less than or equal to), as "greater than" includes all numbers above the number in question, so if we're looking for anything LESS than that, it would be that number or anything less. Considering integers, for example,

consider x <> 5

breaking that down, >5 means 6, 7, 8, 9 etc.
therefore <(>5) would be < 6, 7, 8, 9 etc.,
which simplifies to < 6
i.e. 5, 4, 3, etc.
i.e. <= 5

And yes, when it comes to non-numeric variables, it makes zero sense at all.

In short, I agree with those who said this symbol doesn't make sense :P and naturally I prefer !=, which I was familiar with from other languages when I started AHK, and seems more intuitive.
just me
Posts: 5425
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: ! or 'not' to validate an expression in an if statement as false?

19 Apr 2016, 01:40

SifJar wrote:But there's no "or" symbol in there :P To me (and clearly others), it simply reads as "less than greater than" ...

If you look at <= there's no "or" symbol in there, too. So you read it as "less than equal", which would essentially be equivalent to < (less than)?

If (A <> B) results to false, A actually is either less or greater than B, and that's exactly what "not equal" stands for. And it's true for number as well as string comparisons.
Leli196
Posts: 188
Joined: 29 Aug 2015, 05:47

Re: ! or 'not' to validate an expression in an if statement as false?

19 Apr 2016, 10:19

tidbit wrote:I also always put ()'s on my if's, for consistency. if's without ()'s can behave funky. just always use ()'s to be safe, IMO

Although this post is not new: Do you even write if (GetKeyState("a", "P")) and not if GetKeyState("a", "P")?
How about the other people here? Do you use parantheses around functions?

Greetings Leli196

Return to “Offtopic”

Who is online

Users browsing this forum: Google [Bot] and 2 guests