Code layout

Category: visual studio fsharp


SamDhillon on Fri, 04 Sep 2015 20:04:18

The layout of F# code seems ... well, naff!

What am I missing by being unimpressed by the use of indents and new lines?

Why not put arguments in brackets and explicitly indicate the relationship between chunks of code? This approach seems to obfuscate, its appears fragile and error prone.

Any hints as to the not so obvious power of this format?



Mr. Tines on Fri, 04 Sep 2015 21:14:46

If you conventionally bracket and consistently indent inside the brackets, then you're being redundant; the same in F# light syntax as in Python.  If you fall on the Ruby side, then you can always opt for the verbose ML-style syntax instead (but few people do).

The F# power tools extension provides depth colouring which shows the inferred blocks.

Mr. Tines on Tue, 08 Sep 2015 18:16:42

Why not put arguments in brackets

It makes partial application simpler to express, syntactically, as in this trivial example

let add x y = x + y;;
let increment = add 1;;

yawar.amin on Fri, 06 Nov 2015 00:53:13

If you have a compound expression or binding, you can get very neat and readable code by putting 'subcomponents' on the following lines with an increased level of indent. Think about the meaning of the code, and how the various expressions and bindings are made of their constituent parts. A few examples:

let x =
  // something
  // something else

if x = 1
  then "yes"
  else "no"

if y = z
    let a = 1
    let b = 2

    a + b

let a =
    "a href=['\"](.[^'\"]+)['\"]",