On Conciseness

In my early days of working on Sympy, someone would review my code and essentially say “You can replace these 20 lines with just one line”. And I would be like “Dammit! You cant’t do this. I spent a long time writing this program.” But, I had to give in.

I realized I should write concise code myself. Initially, I would just write the lame 20 line program and then try to compress it. That would give me a tight final code although I had wasted a lot of time on the initial version. Gradually, my programs started getting shorter and shorter from the beginning.

I think writing small code is the way to go. Often while doing any kind of programming, one should ask himself “What can I do that will make this program short.”

Reading

Some may think whether making a code concise decrease the readability of the code. I believe concise code improves readabilitiy. A concise code means there are fewer components and consequently fewer interactions in between the componenets. Thus a concise code is easy to load in the head. A concise code would mean the whole program can be used routinely since it at once fits into the mind. Thus the whole design can be used regularly.

Writing

Writing concise code saves time. Actually, it can save us a lot of time. A code that’s five times shorter will take five times less time to write. A common misconception may be some concise code may have actually taken a lot of time to write. That’s what I was doing when I was writing a longer version and then gradually compressing it. That does happen but I think you can decrease the need of compressing by various heuristics. Obvious examples include list comprehensions in python, character input and output programs of K & R.

Doing it

How can we write concise code? One approach might be to write the programs as a series of layers with one layer supplying components from which higher levels can be built. That’s how softwares are generally written, whether top-down or bottom-up.
But here we lose in writing departement – someone has to write the lower layers. It may save time for a particular person if the labour is divided in between layers but overall it doesen’t saves time.

It’s equally important how these components fits together to make the complete program. I don’t think we have much choice here. It is dictated by the syntax of whatever programming language we are using. But what I’m sure here is syntax does play a big role in making programs short. It is perhaps no accident that java programs tend to bloat but python programs can be made cryptically dense using list comprehensions.

Choosing Tools

Thus, we have arrived at a useful heuristic for choosing a language. We can ask – what language will make my program short? I think a language can help us by solving the two problems we raised earlier.

The lower layers of our problem can be written by someone else as library functions. A language is good if it has good library functions for our task. And these libraries have to come with language itself and not to be installed seperately as third party.

The tools can help arranging the components by supplying us with good idioms or by just having concise code syntax. There are definite ways in C and Python that makes programs short. And these are best learned while working with programmers who are experienced in that particular language.

A consequence of all these observations is that all languages are not equivalent.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to On Conciseness

  1. Manjit says:

    i would say brilliant.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s