White Space Makes All the Difference

Space is Important!

As long as I have been writing code, there have been debates about white space. This seems silly, but people are opinionated, and some of the most opinionated people in the world are also probably the smartest people in the world. Space is important in the world it allows us to tell one thing from another. Could you imagine if space didn’t exist. There would only be one thing that existed that had no end or beginning.

Space in text helps us see words and give meaning to sentences. Canyoureadthissentancewithoutspace? That was kind of hard to read right? There are debates about space in writing styles. How many spaces belongs after a period. You probably don’t think about it, but when designing fonts, the artists care a lot about space, at individual letter level. If you didn’t get the message yet. Space is important.

What is white space?

In different programming language space is treated differently. In character encodings a space is a character, a tab is a character, even carriage return and line feed are characters. These things matter. The help us to understand how to interpret the words that we see. What’s probably not often to non-programmers and maybe even some programmers is that code is written for human consumption. That is why we have compilers. Compilers turn text that humans read into binary code that computers can read. Even in interpreted languages the intermediate language that the compiler generates before the JIT compiler does its magic is somewhat human readable, meaning it has spaces and symbols we can likely recognize. It was designed that way.

Why it matters in programming?

I’m sure most people reading this are familiar with C# or a C style language. If you have programmed in one of those then the { and } characters are likely your friends. You have probably typed more curly braces than you can even imagine. You may or may not have thought about why those characters are necessary in code. Yes, I’m sure you realize they signify the beginning and end of a body of something. It could be a method (function), namespace, Property, case statement or whatever. I would imagine the simplest explanation for them is because it was easier to write a parser that could just look for those symbols that would had no other meaning, to understand where something begins and ends. However, the consequence of the decision.

One point of view might be that curly braces are empowering the programmer to format their code bodies, in whatever way pleases them most. The only problem with that is if you give 10 different programmers the freedom to solve a problem how they want is you might get 10 different results.

At this point, as far as I know most programmers have settled on indentation to show that one line of code is a child of another line of code. If a line gets to long, you can simply start a new line and indent it underneath the previous line. Here is a really simple stupid example of that:

public void Foo(string name)
{
    var hello = "Hellllllloooooooooooo"
        + name;
    Console.WriteLine(hello);
}

Theoretically it could also be written like this

public void Foo(string name)
{var hello = "Hellllllloooooooooooo"
+ name;Console.WriteLine(hello);}

Which would you rather read. Having just curly braces allows for really stupid looking code that compiles, and runs. However the point is for a human to be able to quickly read and understand what the code is doing. In this case obviously the first example, I hope you agree, is more readable.

Most people don’t write code like the second example, but you what you might see in a C style language is something like this.

var Foo  = function(name) {
       var hello = "Hellllllloooooooooooo"
           + name;
       Console.Log(hello);}

I switched from C# to javascript syntax on you just to prove a point. The reason the javascript people might write something like that is to take away some of the power of the curly braces. In the C# version the curly braces take up an entire line. Just to be clear you can write javascript in the same style as C# above, this is simply the style I have observed when looking at javascript.

So imagine if the javascript version didn’t require those curly braces. In this example it would look like

  var Foo  = function(name) 
       var hello = "Hellllllloooooooooooo"
           + name;
       Console.Log(hello);

Would you still consider this to be readable? Without the curly braces you would still require proper indentation to understand the logic of course, but doesn’t that just look nicer? I know that is subjective, but when consuming text, other than code we typically don’t look for curly braces. Its just a little unnatural. Wouldn’t it be a little crazy if you had to start a paragraph with { and end with }.

{In html we do this with tags which is also seems quite silly. Why do we need markers to identify intent? Fortunately someone figured this out and invented MarkDown to solve that problem. }

So now that you have seen the freedom for ugliness that curly braces have introduced not only by themselves requiring the { and } characters but allowing developers to freely express their points of view about code formatting while still compiling. I hope so. Before we get to F#, I want to share one more language that takes white space very seriously in its language. That language is Python. It has a concept of significant white space. It enforces indentation rules. If you look at the javascript example again

var Foo  = function(name) 
       var hello = "Hellllllloooooooooooo"
           + name;
       Console.Log(hello);

You can see that the second and fourth lines are indented to the same degree. That is what is required in Python. It tells the compiler these are separate commands. If Console was indented by another space and this were Python the compiler would be likely complain. If you are curious about python and white space feel free to google that. Its an interesting topic for nerds like me. Anyway, you are probably wondering what does this stuff have to do with F#.

Well in F# there are no curly braces, and NO TABS, no semicolons, and indentation is used to determine where code body starts and ends. Let me repeat. Don’t try to write code with TABS in F#. Obviously don’t use semicolons or curly braces either, on occasion you might need some parenthesis, but not always. That debate has been settled by the F# team and they are turned off by default in F# IDEs. The compiler is actually really excellent at helping you figure this out as well. Here is an example of what the foo function would look like in F#.

let Foo name  =  
      printfn
        "Hellllllloooooooooooo %s" 
         name

Notice the “Helll…” string is indented underneath printfn. That is denoting that it is being passed as argument to the printfn function. Also name in this case is the second argument to the printfn function. It is indented the exact same amount.

What would happen if name were indented the same amount as printfn?

  let Foo name  =  
      printfn
        "Hellllllloooooooooooo%s" 
      name

In this case the compiler is looking for the string to pass for the %s string placeholder. And it will return an error message: “The expression is a function value, i.e. it is missing arguments. It’s type is string -> unit”. If you aren’t familiar with the syntax string -> unit. It simply means that the function takes a string and returns a unit. A unit in F# means it doesn’t have a return value. Without indenting the name variable it doesn’t know that name is the argument passed for %s.

This might take some getting used to, but I think you will find, not typing curly braces, and semicolons is a great trade off. There is a lot less noise in F# code.

Spread The Word
Adam.Wright
 

Adam Wright is a technologist specializing in software development using Microsoft technologies.

Click Here to Leave a Comment Below 0 comments