AutoHotkey Community

It is currently May 26th, 2012, 6:49 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: June 19th, 2009, 12:55 am 
Offline

Joined: March 27th, 2009, 12:46 pm
Posts: 76
Location: Dublin, IE
Being a noobie to AHK, my code tends to be a little longer than it can be. Going through other people's code and reading the command list, I've come across some nice ways of shortening my code and I thought it'd be nice to start a thread compiling all of the techniques.

Feel free to add/correct and of techniques!

TERNARY OPERATORS
Code:
; Original code
If var = 1
    i = 1
Else
    i = 2

; Shortened code
i := var = 1 ? 1 : 2
    I also found the ternary operators useful for if statment message boxes.
Code:
; Original code
If var = 1
    MsgBox, Var equals 1!
Else
    MsgBox, Var doesn't equal 1!

; Shortened code
i := var = 1 ? "It exists!" : "It doesn't exist!"
MsgBox, % i

    If the message boxes are long, you can use the ternary operators to continue the line above (although, this example doesn't make the number of lines any smaller).
Code:
i := var = 1
    ? "Why does var have to equal 1?"
    : "Var does not have to equal 1 all of the time"
MsgBox, % i
    Has anybody figured out how to shorten Else If statements?
ASSIGN OPERATOR ".="
    The ".=" operator is very useful when creating parsed lists in loops.
Code:
; Original code
Loop, 10
    i := i "|" A_Index

; Shortened code
Loop, 10
    i .= "|" A_Index

BOOLEAN
    Instead of having to check if boolean values equal 0 or 1, you can just drop the "= 1" in the if statment and check if the boolean is not blank or zero.
Code:
; Original code
WinWait, Untitled - Notepad, , 1
If ErrorLevel = 1   ; i.e. it's not blank or zero.
    MsgBox, The window does not exist.`nErrorLevel: %ErrorLevel%
Else
    MsgBox, The window exists.`nErrorLevel: %ErrorLevel%

; Shortened code
WinWait, Untitled - Notepad, , 1
If ErrorLevel   ; i.e. it's not blank or zero.
    MsgBox, The window does not exist.`nErrorLevel: %ErrorLevel%
Else
    MsgBox, The window exists.`nErrorLevel: %ErrorLevel%

_________________
My Scripts


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 19th, 2009, 1:49 am 
Offline
User avatar

Joined: September 8th, 2008, 12:26 am
Posts: 1048
Location: Ploieşti, RO
Code:
MsgBox, % "The window " (ErrorLevel ? "does not exist" : "exists") ".`nErrorLevel: " ErrorLevel

;)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 19th, 2009, 1:53 am 
Offline

Joined: March 27th, 2009, 12:46 pm
Posts: 76
Location: Dublin, IE
Drugwash wrote:
Code:
MsgBox, % "The window " (ErrorLevel ? "does not exist" : "exists") ".`nErrorLevel: " ErrorLevel

;)
I didn't know that you could interject the if statement inside the message box text.

Thanks for the tip.

_________________
My Scripts


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 19th, 2009, 2:42 am 
Offline

Joined: August 21st, 2006, 7:07 pm
Posts: 2925
Location: The Shell
Ternary threads are always welcome :D

Heres a really good one

http://www.autohotkey.com/forum/viewtopic.php?t=29752

I dont understand why there is not more (a dedicated page) on this in the chm included with AHK??

_________________
Imageparadigm.shift:=(•_•)┌П┐RTFM||^.*∞


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 19th, 2009, 3:26 pm 
Offline

Joined: May 15th, 2007, 8:59 pm
Posts: 169
Ohh this is great I knew about a few of these shortening techniques through various code I had learned but I defiantly learned more from this thread :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 19th, 2009, 4:58 pm 
Offline

Joined: August 21st, 2006, 7:07 pm
Posts: 2925
Location: The Shell
So this is where this process makes me feel lie a big dummy.

Trying to get Tooltip to show multiple characters equivalent to

Code:
Send, {s 3} ;Type 3 s's

but in a tooltip now..
Code:
ToolTip, sss ; Dont want to have to type Char#'s

and shortened to something like..
Code:
ToolTip % Send . "{s 3}"
:lol:
This is where the brain explosions start :hihi:..

Any ideas of how to get this working??

_________________
Imageparadigm.shift:=(•_•)┌П┐RTFM||^.*∞


Report this post
Top
 Profile  
Reply with quote  
PostPosted: June 19th, 2009, 5:08 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
Voltron43 wrote:
BOOLEAN
    Instead of having to check if boolean values equal 0 or 1, you can just drop the "= 1" in the if statment and check if the boolean is not blank or zero.
Code:
; Original code
WinWait, Untitled - Notepad, , 1
If ErrorLevel = 1   ; i.e. it's not blank or zero.
    MsgBox, The window does not exist.`nErrorLevel: %ErrorLevel%
Else
    MsgBox, The window exists.`nErrorLevel: %ErrorLevel%

; Shortened code
WinWait, Untitled - Notepad, , 1
If ErrorLevel   ; i.e. it's not blank or zero.
    MsgBox, The window does not exist.`nErrorLevel: %ErrorLevel%
Else
    MsgBox, The window exists.`nErrorLevel: %ErrorLevel%


You should note it is not allways good to use it like this.
And often you notice it much later when your code get more complex.
Code:
DetectHiddenText,Off
DetectHiddenWindows,Off

MsgBox Function: `nResult:`tDetectHiddenWindows %A_DetectHiddenWindows%`n`tDetectHiddenText %A_DetectHiddenText%

Function("A1 B1")
MsgBox Function: Function("A1 B1")`nResult:`tDetectHiddenWindows %A_DetectHiddenWindows%`n`tDetectHiddenText %A_DetectHiddenText%

Function("A0 B0")
MsgBox Function: Function("A0 B0")`nResult:`tDetectHiddenWindows %A_DetectHiddenWindows%`n`tDetectHiddenText %A_DetectHiddenText%

AnotherFunction("A0 B0")
MsgBox Function: AnoterFunction("A0 B0")`nResult:`tDetectHiddenWindows %A_DetectHiddenWindows%`n`tDetectHiddenText %A_DetectHiddenText%


Function(parameter){
   ;A= DetectHiddenText , B= DetectHiddenWindows
   If (parameter){
      Loop,Parse,parameter,%A_Space%
         If (option:= SubStr(A_LoopField,1,1))
            %option%:= SubStr(A_LoopField,2)
   }
   If A
      DetectHiddenText % a ? "ON" : "Off"
   If B
      DetectHiddenWindows % b ? "ON" : "Off"
}
AnotherFunction(parameter){
   ;A= DetectHiddenText , B= DetectHiddenWindows
   If (parameter){
      Loop,Parse,parameter,%A_Space%
         If (option:= SubStr(A_LoopField,1,1))
            %option%:= SubStr(A_LoopField,2)
   }
   If (A!="")
      DetectHiddenText % a ? "ON" : "Off"
   If (B!="")
      DetectHiddenWindows % b ? "ON" : "Off"
}

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 19th, 2009, 9:14 pm 
Offline

Joined: June 27th, 2007, 9:07 pm
Posts: 101
Location: California
More like Techniques for Obfuscating Code amirite? ;D

Seriously though, playing around with syntax, and trying to find the most compact way of writing this is certainly good fun. But don't forget, it's easy to write code that only you have to understand, but it is significantly harder to write code that others will understand.

Please, when you write your code, think of the children-- er, other programmers.

;)

_________________
-m35


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: fusion1920, Ragnar, Retro Gamer and 11 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group