Jump to content

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

Coding Style Guidelines


  • Please log in to reply
19 replies to this topic
rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011
Some useful advice from the inventer of Linux, Linus Torvalds.

Linux kernel coding style

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


G. Sperotto
  • Members
  • 539 posts
  • Last active: Jun 20 2015 04:54 PM
  • Joined: 12 Dec 2011
I keep seeing people ignore the absolut need of planning the overall organization/maintainability in a script. It's just a matter of time till they find themselves with a 500 lined headache-o-matic of a code.

But the most funny thing is watching them post "whats wrong with my code!!?!?!?!??" in the support session :lol:

"What is a suitable automation? Whatever saves your day for the greater matters."
Barcoder - Create QR Codes and other Barcodes using only Autohotkey !!


jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
I had trouble getting past the first chapter.

[quote name="Chapter 1]Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen. The answer to that is that if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.[/quote]
I like reading an instruction guide that tells you what to do and then insults you (and your code) if you don't like it. :wink:

guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
valid warning though

In short, 8-char indents make things easier to read, and have the added
benefit of warning you when you're nesting your functions too deep.
Heed that warning.


fragman
  • Members
  • 1591 posts
  • Last active: Nov 12 2012 08:51 PM
  • Joined: 13 Oct 2009
I tend to disagree, atleast for an example like this:
class test

{

	f()

	{

		loop 10

		{

			if(A_Index = 1)

				msgbox first

		}

	}

}


guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
lol obv you use discretion for something so subjective

rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011
I tend to follow about 90% of his suggestions in my code. I only use a four-space indentation though. Not because I think it's right or better, it's just what I've always done. Eight spaces just looks ... weird.

[quote name="Chapter 1]The answer to that is that if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.[/quote][quote name="fragman"]I tend to disagree, atleast for an example like this:
class test
{
	f()
	{
		loop 10
		{
			if(A_Index = 1)
				msgbox first
		}
	}
}
[/quote]
Well, I wouldn't count the first two indentations as part of the 3-indentation "limit". The statements in the loop would be the first indentation I would count.

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


girlgamer
  • Moderators
  • 3263 posts
  • Last active: Feb 01 2015 09:49 AM
  • Joined: 04 Jun 2010
Coding style guidelines are very individual and much depends on who the code is going to be used by or produced for. Indentation style is one of the more personal choices whether it's 3 spaces or 5 spaces or 8 spaces all depends on the level of readability and page format you want to convey. Personally i like a tab that is 4 spaces for compactness but that can vary by circumstance. Another visual indentation style i often use is when indenting the body of a subroutine or function to make the routine name more visible when scanning through the code
SubroutineName:
    MsgBox,0,,This is msg box 1
    MsgBox,0,,This is msg box 2
    return
this form of indentation helps me quicky identify where subroutines begin and end in my code. Especially in longer pieces.
Another form of styling is the naming conventions for variables, subroutines and functions. Occasionally i will use something called Hungarian Notation to help me remember what a given routine, function or variable's purpose is http://en.wikipedia....garian_notation this can be handy if not overdone.
Whether to leave in debugging code or not and the extent of errorchecking for boundary cases is also a style guideline. Code generalization and routine factoring are also style issues as well.
If i were to give any advice at all, I'd say pick a simple subset of styling conventions and stick with them for all your projects. Start with indentation and code factoring and try to keep your code as simple as possible given the job you need to do. The principle of Occam's Razor http://en.wikipedia....i/Occam's_razor is an important concept to apply to any code you write. And try to make your code as re-usable as possible to help you keep from having to reinvent the wheel every time.

The universe is a wondrous place! The faster you create unbreakable code, the faster the universe creates people that can break it. All scripting follows the rule Rule Of Twos -- 1) Good, 2) Fast 3) Cheap -- pick any Two.
I guarantee absolutely nothing about any code I provide except that it works in my machine. ●
MMO Fighter   KeyLooperDemo   Key Spammer   TinyClickRecorder  GGs Password Generator.ahk
For the newest version of AutoHotkey and some killer scripts go here.
Rock-on%20kitten.gif


cheochi
  • Members
  • 8 posts
  • Last active: Jun 21 2012 08:42 PM
  • Joined: 21 Jun 2012
Useful information shared...Thanks

fischgeek
  • Moderators
  • 1074 posts
  • Last active: Jul 07 2015 06:27 PM
  • Joined: 20 Apr 2009
I follow some of those guidlines. However, I use the indentation that is defaulted in Scite4AutoHotkey. So, however many spaces that is. However, I do tend to use OTB for my functions.

guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011

Do not add spaces around (inside) parenthesized expressions. This example is
*bad*:

s = sizeof( struct file );


not so sure i agree here

i've been working on my code and was finding that its much easier to read if () statements with function calls inside if there is a space such as
if ( isMyFunctionTrue(param1, param2) )


rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011

i've been working on my code and was finding that its much easier to read if () statements with function calls inside if there is a space such as

if ( isMyFunctionTrue(param1, param2) )

Since a function follows the if, you don't need the outside parentheses at all.

In addition, the open-parenthesis may be omitted entirely if the first item after the word "if" is a function call or an operator such as "not" or "!".

if isMyFunctionTrue(param1, param2)

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
yea i know that, i've tried that as well, but i like to enclose all my ifs in parenthesis not only to standardize the use of expression-if but also because i find it easier to read (pradoxically i know)

IsNull
  • Moderators
  • 990 posts
  • Last active: May 15 2014 11:56 AM
  • Joined: 10 May 2007

yea i know that, i've tried that as well, but i like to enclose all my ifs in parenthesis not only to standardize the use of expression-if but also because i find it easier to read (pradoxically i know)

Me too - AHK has too many special handlings when parenthesis are omitted that I tend to use them always. In v2 this might change, as a if will always be followed by an expression.

Some other rules I use - personally but also enforce in teams, no matter what language used:

[*:2mm0qmes] Good readable Variable names / Method names (no abbreviations, no one-char names; exception: i,j for counters)
[*:2mm0qmes] Method Line-Count should fit on a single screen. Max 3 nested Code-Blocks. (=> create new Methods where the code is "out-sourced")
[*:2mm0qmes] Never ever have redundant code. Sounds obvious, but some people don't get it.... they copy 30lines of code, and change one single thing and generate lot of redundant overhead

Scoox
  • Members
  • 194 posts
  • Last active: Jun 09 2017 03:34 AM
  • Joined: 28 Nov 2010

I have a question regarding the user of upper/lower case in AHK.

Why is "if" not capitalized but "IfWinExist" is? I know it doesn't really matter but I am a big fan of standards so it would be useful if anyone would kindly post a list of keywords that should be lower-case.


RegJump() - Jump to registry path in Regedit

HoverScroll() - HoverScroll() - Scroll controls without focus