Sunday, September 13, 2009

Constraints in Python

With every language that I learn, I pay close attention to my work flow. I take note of anything that gets in the way of getting my task accomplished. In a perfect language, I should never have to worry about the language ever. A free flow of ideas should come out. Whenever I find myself totally immersed in a problem, I keep track of what made it so. Recently, the one feature that surprised me the most was Python's forced indention. At first I thought, how nice not to use curly brackets to denote scope, but what about that white space? But, I put my best foot forward and worked on some code. I must admit that I love it now! It makes code more readable by keeping the rules consistent (you have to be or your code will have bugs). Formatting my code is now one less worry I have. I am forced to indent as I go along. This constraint is liberating in the fact that it allows me concentrate on the problem at hand and is one less thing to worry about. No more worrying about if the curly braces go on the same line or not depending on local coding standards. It makes code easier to read across Python programmers. In fact, I have yet to meet a Python program that I could not understand easily from first glance which is a rare accomplishment in other languages.

I also love the constraint about one line lambdas. I thought it was going to drive me crazy, but I have embraced completely. It keeps your lambda functions short and sweet like they really should be. But, the best side effect is that if that lambda needs to grow, you simply move it out to a named function automatically making your code more readable. Excellent.

Both of the features that I just mentioned are merely constraints that take away thinking about the language and force you to write readable code. I always like and embrace the design decisions to constrain the coder to make more maintainable code. The less I have to think about the language, the more time I spend on the problem I am trying to solve and that's the way it should be. It just amazed me that sometimes I find them in the strangest of ways. I would have never thought that forced indention or one lined lambdas would bring me so much coding pleasure.


tenuki said...

I had kind of the same feeling you mention here. As Smalltalker, at first I didn't like the indentation, but when I have had to start coding python I discovered that it's useful.

But, I must say that anyway I believe Smalltalk is always a step ahead. Why? Because you have prettyprint. You can write your code in a faster way and get it automatically formated in a standard way.

But, also, I think it could be taken forward in this way: Suppose you don't like the prettyprint why of formatting. You can modify the IDE to show you the code prettyprintted in the way you most like, anytime, and save it in the standard formatting for your office standards, for example.

Unknown said...

I always use automatic formatting in any language I use. I thought it was cool that it is unneeded in Python. It's also nice that the formatting is the same from developer to developer, no more taste wars.

The more the language takes away mundane issues (formatting), the more time you have to think about your problem at hand which is the whole point of languages.

tenuki said...

Yes. Your point is clear!

Ron Smith said...

Glad to hear from another convert.