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