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

Talk about anything
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

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

19 Apr 2016, 10:49

always. stay consistent, avoid any issues (it's a common issue for a noob to not use () and simply adding them solves the issue), save work later on if you need to add other stuff with && or || or if you prefer the { on the same line as the if/while/etc (OTB style)
if (GetKeyState("a", "P"))

though, with some functions like getkeystate, fileexist, and others, I do not always include an "=1" or "=0" (as I mentioned earlier that I always add them). the names make it obvous when reading.
"if the file exists..." is perfectly readable.
"if the file exists=1..." just looks silly :P
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
Leli196
Posts: 215
Joined: 29 Aug 2015, 05:47
Location: Germany

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

19 Apr 2016, 11:52

Interesting. How about an expression with several parentheses like this: (A_Index > 1) || (A_Index = 1 && !current_ip != ""). Would you put this as a whole in parentheses as well when used with an if statement? So if ((A_Index > 1) || (A_Index = 1 && !current_ip != ""))

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

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

19 Apr 2016, 12:10

like my example on the previous page. if (EVERYTHING)
if (var)
if (var && var2 || func())
if ((var3 || var) && var2)

not a bunch of ()'s like some people prefer: if (var) || (var2+5) || (func()-1)
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
john_c
Posts: 493
Joined: 05 May 2017, 13:19

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

11 Apr 2018, 06:45

tidbit wrote:Always. Stay consistent, avoid any issues (it's a common issue for a noob to not use () and simply adding them solves the issue).
From what I tested, () isn't always a good idea. Here it is:

Code: Select all

; Works correctly
var := "aaa"
if var contains foo,bar,baz
    msgBox % "Success"
    
; As I understand, parentheses are illegal here
var := "aaa"
if (var contains foo,bar,baz)
    msgBox % "Success"
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

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

11 Apr 2018, 06:51

Yeah you are on to something however you got it backwards.
NEVER USE CONTAINS.
Recommends AHK Studio
john_c
Posts: 493
Joined: 05 May 2017, 13:19

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

11 Apr 2018, 07:04

@nnnik
nnnik wrote:Yeah you are on to something however you got it backwards.
NEVER USE CONTAINS.
Thanks. But there are some questions from my side:

1. Why we should avoid it?

2. Do you suggest to use inStr() or regExMatch() instead?

Code: Select all

var := "foo"
if inStr(var, "foo") || inStr(var, "bar")
    msgBox % "Success"

var := "foo"
if regExMatch(var, "(foo|bar)")
    msgBox % "Success"
3. Also, do you suggest to avoid if var in and if var between as well?
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

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

11 Apr 2018, 07:14

Yeah they are not expressions and are inconsistent with expression syntax - due to that they are not an option.
Sadly there is not a simple alternative available - depending on who you ask there might be several answers:

Code: Select all

isDay := { moday:1, tuesday:1, wednesday:1, thursday:1, friday:1, saturday:1, sunday:1 }
if ( isDay[ day ] )
	Msgbox % day " is the name for a day"
Depending onthe circumstances different answers might be better
Recommends AHK Studio
john_c
Posts: 493
Joined: 05 May 2017, 13:19

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

11 Apr 2018, 07:23

@nnnik Thanks. Now there is another good option.
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

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

11 Apr 2018, 10:56

also, the "if var in/contains/type" style if's are on a separate page than normal if's for a reason: they are different than normal if's.
they are not expressions, cannot use (), cannot use with || && and so on. they need to be EXACTLY as documented on their own page, not as documented on some other page (normal if's).

(same goes for hotkeys and send. so many people try to do {enter}:: or w/e. why? SEND docs are a completed different page than hotkey docs. stick with the correct page, folks! You wouldn't go read a monster trucks manual to fix a moped.)
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
Cerberus
Posts: 172
Joined: 12 Jan 2016, 15:46

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

03 Jun 2018, 20:44

tidbit wrote:also, the "if var in/contains/type" style if's are on a separate page than normal if's for a reason: they are different than normal if's.
they are not expressions, cannot use (), cannot use with || && and so on. they need to be EXACTLY as documented on their own page, not as documented on some other page (normal if's).

(same goes for hotkeys and send. so many people try to do {enter}:: or w/e. why? SEND docs are a completed different page than hotkey docs. stick with the correct page, folks! You wouldn't go read a monster trucks manual to fix a moped.)
Don't you think perhaps a warning would be in order on those pages, so that people won't mix together two incompatible modes? I believe "if"* does contain some nice warnings about the brackets and expression mode, but I don't know about the
"contains" page or section (or wherever that term is described).

Return to “Off-topic Discussion”

Who is online

Users browsing this forum: No registered users and 53 guests