-
Notifications
You must be signed in to change notification settings - Fork 391
AWK Style Guide
Adapted from WebKit Coding Style.
We use K&R style with camelCase in our AWK code.
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++) {
}
}
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"
}
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 )
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()
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()
{
}
1. Place braces before pipes.
✓ Right:
("subprogram " parameterize(args)) | getline
✗ Wrong:
"subprogram " parameterize(args) | getline