 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Voltron43
Joined: 27 Mar 2009 Posts: 76 Location: Dublin, IE
|
Posted: Thu Jun 18, 2009 11:55 pm Post subject: Techniques for Shortening Code |
|
|
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 |
|
| Back to top |
|
 |
Drugwash
Joined: 07 Sep 2008 Posts: 921 Location: Ploiesti, RO
|
Posted: Fri Jun 19, 2009 12:49 am Post subject: |
|
|
| Code: | | MsgBox, % "The window " (ErrorLevel ? "does not exist" : "exists") ".`nErrorLevel: " ErrorLevel |
 |
|
| Back to top |
|
 |
Voltron43
Joined: 27 Mar 2009 Posts: 76 Location: Dublin, IE
|
Posted: Fri Jun 19, 2009 12:53 am Post subject: |
|
|
| 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 |
|
| Back to top |
|
 |
TLM
Joined: 21 Aug 2006 Posts: 2926 Location: The Shell
|
|
| Back to top |
|
 |
icefreez
Joined: 15 May 2007 Posts: 169
|
Posted: Fri Jun 19, 2009 2:26 pm Post subject: |
|
|
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  |
|
| Back to top |
|
 |
TLM
Joined: 21 Aug 2006 Posts: 2926 Location: The Shell
|
Posted: Fri Jun 19, 2009 3:58 pm Post subject: |
|
|
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}" |
This is where the brain explosions start :hihi:..
Any ideas of how to get this working?? _________________
paradigm.shift:=(•_•)┌П┐RTFM||^.*∞ |
|
| Back to top |
|
 |
HotKeyIt
Joined: 18 Jun 2008 Posts: 4652 Location: AHK Forum
|
Posted: Fri Jun 19, 2009 4:08 pm Post subject: Re: Techniques for Shortening Code |
|
|
| 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  |
|
| Back to top |
|
 |
erictheturtle
Joined: 27 Jun 2007 Posts: 101 Location: California
|
Posted: Fri Jun 19, 2009 8:14 pm Post subject: |
|
|
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 |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|