Spaces in code

Since I started to write code, I’ve been trying to find the right way to place spaces in my code. No matter what language I was programming in, I would not find a way of making it feel right. Standard ways of doing it didn’t cut it either.

A few months ago I stumbled upon a way to do it. The idea is to copy how we use parenthesis and spaces in natural language.

So, instead of writing function(arg,arg2), I write function (arg, arg2). Instead of function() or function ( ), I write function ().

That is, I leave a space before each opening parenthesis, a space after each comma, and a space after each closing parenthesis. The same also goes for colons.

Also, when using square brackets, the same logic applies than with round or curly brackets. For example, instead of writing object[iterator] or object [ iterator ], I write object [iterator].

It doesn’t feel either cramped or longwinded, it’s just like reading text.

Just wanted to share it with you.