Is it a good idea to use == instead of = everywhere where possible? Topic is solved

Discuss Autohotkey related topics here. Not a place to share code.
Forum rules
Discuss Autohotkey related topics here. Not a place to share code.
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Is it a good idea to use == instead of = everywhere where possible?

11 Apr 2018, 11:49

What's your own practice:
  • to use == everywhere where possible
  • or use it only where it is really necessary?

Code: Select all

var := "Foo"
if (var = "foo")
    msgBox % "Success"
    
; vs.

var := "Foo"
if (var == "Foo")
    msgBox % "Success"
Last edited by john_c on 11 Apr 2018, 11:53, edited 2 times in total.
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Is it a good idea to use == instead of = everywhere where possible?

11 Apr 2018, 11:51

i dont think we need a thread for every coding convention.

further, = and == perform two different comparisons. see the docs:

https://autohotkey.com/docs/Variables.htm#Operators

john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Is it a good idea to use == instead of = everywhere where possible?

11 Apr 2018, 11:52

guest3456 wrote:perform two different comparisons
Yes, I understand. This is exactly what my question is about.
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Is it a good idea to use == instead of = everywhere where possible?

11 Apr 2018, 12:09

john_c wrote:
guest3456 wrote:perform two different comparisons
Yes, I understand. This is exactly what my question is about.
Then you use whatever suits your use case best. It's simple like that.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Is it a good idea to use == instead of = everywhere where possible?  Topic is solved

11 Apr 2018, 14:04

- I basically always use =, and not ==, unless I specifically want to do a case-sensitive comparison (usually when I'm writing a string function).
- As was mentioned elsewhere you can use !(var1 <> var2) or !(var1 != var2) to do a case-sensitive/case-insensitive comparison based on A_StringCaseSense.
- Usually = will suffice, but checking over all instances of == in my main scripts, there is one classic exception: date formats, e.g. if (vFormat == "yyyyMMddHHmmss").
- One curious thing is, if you know that the text will always be a certain case e.g. a window class/window title, or if you know that case is irrelevant, e.g. InStr/RegExMatch searching for a space or comma. Is it faster to do a case-insensitive or a case-sensitive comparison. I would have thought that case-sensitive would be faster, however, IIRC when doing benchmark tests, sometimes case-insensitive searches were faster.
- Another point is that sometimes I convert strings to a specific case, prior to comparison, using the Format function: vText := Format("{:U}", vText).
- Also, there is no such thing as a 'case-insensitive' comparison, either the text is converted to lower case, and a case-sensitive comparison is done, or the text is converted to upper case, and a case-sensitive comparison is done (text is sorted according to the Unicode number for a character). E.g. this is relevant when sorting text, whether the underscore is considered to come before or after letters. A Chr(65), _ Chr(95), a Chr(97).
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 10 guests