Style guide menu

Indentation

Indentation is how far in a line starts. It shows which lines belong inside which, the way a list inside a list is pushed further in. These rules decide the empty space in a file, and turf fmt already applies four of them for you.

By the end of this page you will

  • Indent Turf code the way everyone else does.
  • Use blank lines to split code into steps.
  • Know which of these rules turf fmt fixes today.

Spaces and empty lines are invisible to the compiler and very visible to people. A tool can fix all of them, so they are the easiest rules to follow: run turf fmt --write and they are done.

TSG-101 Indent with four spaces

RequiredChecked by turf fmt

Each level of nesting is four spaces deeper than the one around it. Never use tabs: a tab looks two spaces wide on one screen and eight on another, so the same file looks different for everyone.

Do
tsg_101.tr
fn main(): int {    for i in 0..3 {        if i == 1 {            printline(i);        }    }    return 0;}
Don't
tsg_101_avoid.tr
fn main(): int {  for i in 0..3 {          if i == 1 {     printline(i);          }  }  return 0;}

TSG-102 No spaces at the end of a line

RequiredChecked by turf fmt

Spaces after the last character of a line are invisible, so nobody adds them on purpose. They only show up as noise when someone else’s editor removes them and every line looks changed.

TSG-103 Never more than one blank line in a row

RequiredChecked by turf fmt

A blank line is a paragraph break: it separates one idea from the next. Put one blank line between top-level declarations, and use single blank lines inside a function to split it into steps. Two or more in a row say nothing a single one does not.

Do
tsg_103.tr
fn twice(n: int): int {    return n * 2;}fn main(): int {    immut n: int = 4;    printline(twice(n));    return 0;}
Don't
tsg_103_avoid.tr
fn twice(n: int): int {    return n * 2;}fn main(): int {    immut n: int = 4;    printline(twice(n));    return 0;}

TSG-104 End every file with one newline

RequiredChecked by turf fmt

The last line of a file ends with a newline character, and there is exactly one. Many tools expect it, and without it the last line shows up as changed every time someone adds a line after it.

TSG-105 Keep lines to 100 characters

RecommendedNo tool checks this yet

A line of code fits in 100 characters, counting the indentation. Long lines push the end of a thought off the side of the screen, and side-by-side views in code review cut them off. When a call or a signature gets too long, break it after a comma and indent the continuation by one level.

Do
tsg_105.tr
fn describe(name: string, age: int, city: string, job: string): string {    return name + " is " + (age to string) + ", lives in " + city        + " and works as " + job;}fn main(): int {    printline(describe("Mira", 34, "Lisbon", "a carpenter"));    return 0;}
Don't
tsg_105_avoid.tr
fn describe(name: string, age: int, city: string, job: string): string {    return name + " is " + (age to string) + ", lives in " + city + " and works as " + job + "." + "";}fn main(): int {    printline(describe("Mira", 34, "Lisbon", "a carpenter"));    return 0;}
Esc

Type to search.