AWK Style Guide - soimort/translate-shell GitHub Wiki
Adapted from WebKit Coding Style.
TL;DR
We use K&R style with camelCase in our AWK code.
ยง1. Naming
1. Use CamelCase.
โ Right:
BuildPath = "build/"
โ Wrong:
build_path = "build/"
buildpath = "build/"
2. A global variable always starts with an uppercase letter. A function or a local variable always starts with a lowercase letter.
โ Right:
function init() {
}
โ Wrong:
function Init() {
}
3. Local variables must be explicitly declared, after the actual function arguments and separated with 4 spaces or a line of 4 hash characters (####
).
โ Right:
function init( i) {
for (i = 0; i < 10; i++) {
}
}
function init(####
i) {
for (i = 0; i < 10; i++) {
}
}
โ Wrong:
function init() {
for (i = 0; i < 10; i++) {
}
}
ยง2. Indentation
1. Use spaces, not tabs.
2. The indent size is 4 spaces.
โ Right:
BEGIN {
print $0
}
โ Wrong:
BEGIN {
print $0
}
3. A case label should line up with its switch statement. The case statement is indented.
โ Right:
switch (i) {
case 0:
return "red"
case 1:
return "green"
case 2:
return "blue"
default:
return "default"
}
โ Wrong:
switch (i) {
case 0:
return "red"
case 1:
return "green"
case 2:
return "blue"
default:
return "default"
}
ยง3. Spacing
1. Do not place spaces around unary operators.
โ Right:
++i
โ Wrong:
++ i
2. Do place spaces around binary and ternary operators.
โ Right:
isDef = s == "default" ? 1 : 0
getline line < file
โ Wrong:
isDef=s=="default"? 1:0
getline line<file
3. Do place spaces between variable names and literals.
โ Right:
script = script "\n" line
โ Wrong:
script = script"\n"line
4. Do not place spaces before comma and semicolon.
โ Right:
for (i = 0; i < 10; i++) {
}
โ Wrong:
for (i = 0 ; i < 10 ; i++) {
}
5. Do place spaces between control statements and their parentheses.
โ Right:
for (i = 0; i < 10; i++) {
}
โ Wrong:
for(i = 0; i < 10; i++) {
}
6. Do not place spaces between a function and its parentheses, or between a parenthesis and its content.
โ Right:
f(a, b)
โ Wrong:
f (a, b)
f( a, b )
ยง4. Line breaking
1. In principle, each statement should get its own line; however, short statements with similar structures can be put into one line.
โ Right:
i++; j++
i = 0
r = r ","
โ Wrong:
i = 0; r = r ","
2. An else statement should go on the same line as a preceding close brace if one is present, else it should line up with the if statement.
โ Right:
if (cond) {
doSomething()
} else {
doSomethingElse()
}
if (cond)
doSomething()
else
doSomethingElse()
โ Wrong:
if (cond) {
doSomething()
}
else {
doSomethingElse()
}
3. An else if statement should be written as an if statement when the prior if concludes with a return statement.
โ Right:
if (cond)
return someValue
if (cond)
doSomething()
โ Wrong:
if (cond)
return someValue
else if (cond)
doSomething()
ยง5. Braces
1. Always place the open brace on the line preceding the code block; place the close brace on its own line.
โ Right:
function main() {
}
โ Wrong:
function main()
{
}
ยง6. Parentheses
1. Place braces before pipes.
โ Right:
("subprogram " parameterize(args)) | getline
โ Wrong:
"subprogram " parameterize(args) | getline