aesthetic aspects
#1
Guest
Posted 12 September 2012 - 07:02 PM
What do you prefer?
If (){
}
or
If ()
{
}
If ( var = blah )
If (var = blah)
Stuffs like that, even if it's only a space or or the space between the edge of the editor and the next command in if statements.
#2
Posted 12 September 2012 - 07:11 PM
if with the { on the same line, but now i put it on its own line for better readabilitysame with the parens, i now use spaces too to pad the parens so the inside is easier to read
ive done both, my preferences keep changing. but because of that, sometimes my code isn't consistent and there are both types
#3
Posted 12 September 2012 - 07:29 PM
My case was the opposite. I started favoring readability and eventualy, when I got really confident with the syntax, changed toi used to do the
ifwith the{on the same line, but now i put it on its own line for better readability
if ... {.Also, Guest asked about spaces between parentheses...
Well, I don't see any difference, but the thing is: I always use an expression. Never something like
if var = text (althought sometimes I may use if var in x,y,z, but still rarely)Even 95% of my assignments use
:= instead of = (unless it's a really special case or something).
#4
Posted 12 September 2012 - 08:45 PM
Loop %A_WinDir%\*.*
If ( A_LoopFileTimeModified >= Time )
Time := A_LoopFileTimeModified, File := A_LoopFileLongPath
MsgBox, Lastest file in %A_WinDir% is `n`n%File%:= is for assignment, = is for comparsion. That is the best way for everybody, unless people require autotrim.
#5
Posted 12 September 2012 - 09:04 PM
If ()
{
This will be done.
if ()
{
This will be done.
}
}I also try to dismember the code into small functions (15 lines max) and create an index for all these functions. The function names should resemble what they do and any variable names should resemble the meaning of the values they hold.
#6
Posted 12 September 2012 - 09:18 PM
I really need to read up more on this part... I'm always using "Curly Braces" ;-)I prefer to avoid curly braces, whenever I can:
Loop %A_WinDir%\*.* If ( A_LoopFileTimeModified >= Time ) Time := A_LoopFileTimeModified, File := A_LoopFileLongPath MsgBox, Lastest file in %A_WinDir% is `n`n%File%
:= is for assignment, = is for comparsion. That is the best way for everybody, unless people require autotrim.
#7
Posted 12 September 2012 - 09:26 PM
I really need to read up more on this part... I'm always using "Curly Braces" ;-)
nothing to read up on
SKAN will still have to use curly braces in cases where he needs a block of code more than one line, unless he wants to chain them all with commas
no curly braces means only the very next line will be executed if the condition is true
#8
Posted 12 September 2012 - 09:38 PM
Personally, I can't stand when two statements are on the same line. I think it's confusing and and it makes the script harder to debug. I much perfer logically grouping the statements with braces.Time := A_LoopFileTimeModified, File := A_LoopFileLongPath
+10:= is for assignment, = is for comparsion. That is the best way for everybody, unless people require autotrim.
I only use expression ifs and expression assignments. I use one true brace formatting whenever possible:
if () {
}
#9
Posted 13 September 2012 - 05:08 AM
:= is for assignment, = is for comparsion. That is the best way for everybody, unless people require autotrim.
+10 as well
I also prefer OTB (One true brace) style
if () {
}
This has also advantages in code folding editors: it folds up to one single lineif () {
instead ofif ()
{
In opposite to SKAN I also tend to use ALWAYS the braces - even if only one codeline is part of the if-block. This makes it easier for me to read code fluently, as there is always a visual mark to identify end of block (+ code folding is possible - and the "code folding lines" (don't know the correct term) of the editor shows the end of the block ...).
I also try to avoid two or more assignments on one line.
Generally I don't like the sophisticated oneliners (compacted code) as often seen in perl for example - they almost only demonstrate the coolness of the programmer, making it very hard to understand for others. I prefer readability over compactness.
#10
Posted 13 September 2012 - 10:26 AM
About combining lines: I see it is harder to read, nevertheless in AHK it is even faster. You can always do sth like this:
if (true)
A()
, b := true
, c()With this you can omit braces if you like to, you have the extra speed but ti's still readable. All advantages combined
#11
Guest
Posted 13 September 2012 - 01:49 PM
if (true)
A()
, b := true
, c()Care to explain?
#12
Posted 13 September 2012 - 02:10 PM
if (true)
A()
, b := true
, c()as equal to this:if (true)
A(), b := true, c()Which is just one line and therefor the if needs no braces.
#13
Posted 13 September 2012 - 04:14 PM
No offence intended, but that style is going to introduce bugs which are nightmarishly difficult to find, i.e.About combining lines: I see it is harder to read, nevertheless in AHK it is even faster. You can always do sth like this:
if (true) A() , b := true , c()With this you can omit braces if you like to, you have the extra speed but ti's still readable. All advantages combined
if (true)
A()
b := true
, c()IMO, losing the logical grouping afforded by the braces just to gain a few milliseconds just isn't worth it.
#14
Posted 13 September 2012 - 04:20 PM
#15
Posted 13 September 2012 - 06:13 PM
+1 That is my philosophy as well. The goal is readable, self documenting code.I prefer readability over compactness.
Also check out this other thread: Coding Style Guidelines.




