Page 1 of 1

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

Posted: 11 Apr 2018, 11:49
by john_c
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"

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

Posted: 11 Apr 2018, 11:51
by guest3456
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

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

Posted: 11 Apr 2018, 11:52
by john_c
guest3456 wrote:perform two different comparisons
Yes, I understand. This is exactly what my question is about.

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

Posted: 11 Apr 2018, 12:09
by gregster
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.

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

Posted: 11 Apr 2018, 14:04
by jeeswg
- 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).

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

Posted: 11 Apr 2018, 15:16
by john_c
@jeeswg Thanks. Excellent explanation.