Skip to content

Styleguide

Many ideas for the style guide here are borrowed fom the great FastAI library. So, some of the code style may seem similar.

There is good reason to borrow coding style ideas from the fastai library. Some of them are 👇

  • Avoid too much scrolling while reading the code.

  • Convey information as fast as possible.

  • Keep things simple.

  • Spend less time on thinking what a particular piece of code does i.e. the name of a code piece should make it clear about the intention of that code.

Naming

  • Module names are single words with the starting letter as uppercase.

  • Functions and variables are lowercase single words.

    • For example,makedocs. This is to keep thing simple and easier for the user to remember the names. For example, the end user should not get confused about wether the name had an underscore or not.

  • Struct names are single word and start with an upper case.

  • Commonly used things should be shorter.

    • For example,

      • "object" is named o.

      • "image" is named img.

      • input is denoted with x.

    • Look in the "Abbr" section for a list of abbrevations used.

      • Feel free to add any abbrevations which are missed.

Layout

  • One line of code should implement one complete idea, where possible

Generally therefore an if part and its 1-line statement should be on one line Using the ternary operator x = y if a else b can help with this guideline.

  • If a 1-line function body comfortably fits on the same line as the function section, feel free to put them together.

    • In such cases, wherever possible, the functions can also be denoted in the "expression" format like func(x) = x+1.

  • If you’ve got a bunch of 1-line functions doing similar things, they don’t need a blank line between them

    • These can be grouped together.

begin
f(x:: Int8) = x+1
function f(x:: Float16) x*2 end
end
------
Output
------
f (generic function with 2 methods)
  • Aim to align statement parts that are conceptually similar. It allows the reader to quickly see how they’re different.

    • For example

      if foo == 0 x = f(x)
         else     x = f(x) end