Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

template for structured coding


  • Please log in to reply
8 replies to this topic
Ace_NoOne
  • Members
  • 299 posts
  • Last active: May 02 2008 08:19 AM
  • Joined: 10 Oct 2005
Since I often start new projects with AHK, I began to work out a certain structure to keep my code organized.
So I've created a template that contains the basic structure for a new project as well as a few standard routines.

I'm not sure if anyone can make use of this (as everyone's got his own style, oviously*), but it provides me with an opportunity to test my new AutoHotkey.net account... ;)

Anyways, here we go:
/*
[program name] v[#].[#]
 by [author name]

[short description]
*/

/*
To Do:
¯¯¯¯¯¯
- [...]
*/

/*
Revision History
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
# v[#].[#] ([yyyy]-[mm]-[dd])
* initial release
*/

/*
********** Settings, Variable Declarations **********
*/

#SingleInstance Force
#NoEnv
OnExit, quit

programName = [program name] ; DEBUG: to do
programVersion = [#].[#] ; DEBUG: to do
programFullName = %programName% v%programVersion%
programAuthor = [author name] ; DEBUG: to do

/*
********** Auto-Execute Section **********
*/

; backup clipboard contents -- DEBUG: optional
clipboardBak := ClipboardAll
; process command line parameters -- DEBUG: optional
GoSub, getParams
; construct tray menu -- DEBUG: optional
GoSub, trayMenu
; end of auto-execute section
Return

/*
********** Hotkeys **********
*/

; DEBUG - note: hotkeys make the script persistent!

; [ALT]+[ESC]: terminate script
!ESC::
	Suspend ; exempt from suspension -- DEBUG: optional
	GoSub, quit
Return

/*
********** Subroutines **********
*/

; process command line parameters -- DEBUG: optional (see above)
getParams:
	If 0 > 0
	{
		Loop, %0% ; for each parameter
		{
			param := %A_Index%
			; check for switches
			StringLeft, paramType, param, 1
			If paramType = - ; switch indicator
			{
				; determine type of switch
				StringMid, switch, param, 2, 1
				; switch
				If switch = x ; DEBUG: template (replace "x")
				{
					; access value (= next parameter)
					param = % A_Index + 1
					var_x := %param% ; DEBUG: template (replace "var_x")
				}
			}
		}
	}
Return

; construct tray menu -- DEBUG: optional
trayMenu:
	; set tray tip
	Menu, Tray, Tip, %programFullName%
	; disable standard menu items
	Menu, Tray, NoStandard
	; show info message
	Menu, Tray, Add, &About, about
	; separator
	Menu, Tray, Add
	; terminate script
	Menu, Tray, Add, &Quit, quit
Return

; show info message -- DEBUG: to do, optional
about:
	MsgBox, 64, %programFullName%,
	( LTrim
		%programFullName%
		%A_Space%by %programAuthor%

		[short description]

		Use [ALT]+[ESC] to terminate the program.
	)
Return

; terminate script
quit:
	; restore clipboard contents -- DEBUG: optional (see above)
	Clipboard := clipboardBak
	; terminate script
	ExitApp
Return

/*
********** Functions **********
*/
(I'd suggest you make the file read only so you can easily start new projects off of it)


* ... and likely a much better one than my own, since I'm not a very experienced coder at all


UPDATE: (17:00 GMT)
Slightly altered the way command line parameters are processed.

UPDATE: (21:30 GMT)
Added subroutine for processing command line parameters (see this thread).

UPDATE: (2006-12-01)
Posted the latest version and replaced the link to the file with the actual code.

UPDATE: (2007-07-19)
Another update.

UPDATE: (2007-07-20)
Removed "Join" from about: routine's MsgBox (thanks engunneer); fixed typo.

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
Thank you for sharing. It is always good practice to have a template. I hope new users will accept this help and learn from it.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

holomind
  • Members
  • 341 posts
  • Last active: Aug 23 2015 03:27 PM
  • Joined: 11 Mar 2006
hi, the post is quite old, but the idea of using a template to start an ahk script instead of scratch is good.
i would also add a section for configuration, as i realize its quite important to have user-configuration when you compile your script and the user cannot change the config easily in sourcecode.

Ace_NoOne
  • Members
  • 299 posts
  • Last active: May 02 2008 08:19 AM
  • Joined: 10 Oct 2005
I have (again) updated the original template and edited the initial posting accordingly.

hi, the post is quite old, but the idea of using a template to start an ahk script instead of scratch is good.

That's what I thought... ;)

i would also add a section for configuration, as i realize its quite important to have user-configuration when you compile your script and the user cannot change the config easily in sourcecode.

Well, I usually use external files for any user settings if required; the built-in IniRead makes that quite easy.
However, feel free to create your own version of this template if you like.
Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
you should remove the Join from the about box
; show info message -- DEBUG: to do, otpional
about:
   MsgBox, 64, %programFullName%,
   ( LTrim
      %programFullName%
      %A_Space%by %programAuthor%
      
      [short description]
      
      Use [ALT]+[ESC] to terminate the program.
   )
Return

then you don't have to have `n everywhere

Ace_NoOne
  • Members
  • 299 posts
  • Last active: May 02 2008 08:19 AM
  • Joined: 10 Oct 2005

you should remove the Join from the about box
[snip]
then you don't have to have `n everywhere

Good catch - why didn't I think of that?! (Probably a remnant from my earlier days... )
Done.

Thanks!
Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
I also tend to keep settings in an INI file alot - maybe you can add a rough outline of generic ini reading?

I usually write mine from scratch each time, but I have seen functions posted in the forum that read every setting from the ini file automagically.

Ace_NoOne
  • Members
  • 299 posts
  • Last active: May 02 2008 08:19 AM
  • Joined: 10 Oct 2005

I also tend to keep settings in an INI file alot - maybe you can add a rough outline of generic ini reading?

I usually write mine from scratch each time, but I have seen functions posted in the forum that read every setting from the ini file automagically.

I write mine from scratch too - and to be honest, I usually like it that way.
IniWrite is extremely easy to use, so I don't see much of a benefit there.

However, feel free to create a fork - if I like it, I might be tempted to adopt it after all...
Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey

Superfraggle
  • Members
  • 1019 posts
  • Last active: Sep 25 2011 01:06 AM
  • Joined: 02 Nov 2004
Ive just added somefunctions for loading and saving ini files.

see here
Steve F AKA Superfraggle

http://r.yuwie.com/superfraggle