Sunday, June 9, 2013

Why doesn't Python use curly braces(for blocks of code)?

If you've used Python at all you know that it uses indentation to denote blocks instead of curly braces. Theres a couple reasons for this but it boils down to the Don't Repeat Yourself(DRY) principal.

How is using braces repeating yourself?

Well you've probably worked in a language that relies on braces for scope since most of the most common languages do this (Java,C,C++, Javascript, PHP,etc.). When working with one of these languages have you ever had to refactor legacy code that wasn't indented properly? Although the code has braces, we can't easily figure out what is going on. Where does one method end and the other one begin? The first thing you probably did was use the auto-indent feature in your trusty IDE so you could at least get a high level idea of the format of the code.

So it ends up that those curly braces didn't help you much. And they shouldn't because they really aren't for you. They are for the compiler. You see blocks of code in terms of indentation. The compiler interprets blocks of code based on braces. There's no reason to have both.