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.

Posted in Uncategorized | 1 Comment

Beauty is a practical necessity

There is no permanent place in the world for ugly mathematics- G.H. Hardy

When we say something is beautiful, we often believe that this is a subjective feeling. It’s just a feeling that comes, but we do recognise a beautiful object when we see one. As mostly is the case, things that we percive as beautiful are also considered same by nearly everyone else. Is it the case that our metrics are same?

What I think is that this thing called beauty is not only formally definable but is indispensable for any good design. There seems a strong correlation between beauty of a thing and it’s timelessness

We can identify traits that make a thing beautiful in some objects like theorems or computer programs. Maybe it is the case that these same traits characterize other domains.

Simple

A beautiful thing is often simple. It’s workings can be easily understod. A thing that is unduly complicated often doesen’t give any pleasant feeling. Note that it is often the case that there is a lot of hard work that goes into that design. But no signs of false starts, painful dissapointements, messy wrong approaches show up in the final product. The person seeing that thing thinks that he might come up with the same idea himself if he had thought about it in that particular way.

Surprising

A beautiful thing is often surprising. Simple problems have obvious solutions. In a hard enough problem, we first try out the obvious ways first. If they do not work, we try approaches that progressivly become unconventional. Though there may not be a linear connection but as the problems get harder, it’s solution becomes more and more unconventional.

The surprise component of a design often gives a feeling to the person viewing it that it is just made by a man who thinks completely different than me. I can never think in that way. But most probably, the designer has run out of conventional approaches and has to try some unconventional one. These other methods of attack comes from experience in trying out conventional ones.

Solution to a Right Problem

The surprising solution has to solve a hard problem. Beautiful solutions solve the difficult problem. After all, if the problem is simple, it can be solved by conventional methods we learn in classes.

This requirement is necessary but not sufficient. A lot of problems are very hard but their solution does not have beauty. You must insist on right problems. Which one are right ones? Problems that have a general appeal and have a broad range of applicability like finding an algorithm that can cluster just anything ranging from computer clusters, astronomical objects to audio inputs.

Many times problems of broad appeal comes from ones that do not. For example the earlier attempts of finding a general characteristic of heat engines, although a problem of huge practical utility, later became the groundwork of more fundamental concepts of energy and entropy that constitutes the science of thermodynamics. It maybe caused by hind sight or by some other more subtle causes. It may happen that staring a problem long enough just trains the unconsious mind to do it itself involuntary and then the conscious mind free to look it at different ways.

Concise

A beautiful idea is often wonderfully concise. A fabulous idea have as few premises as possible. All implications can be understood with the help of these components. Big, messy things often do not have this kind of beauty. A concise thing is such that one can expected to use all of the design regularly.

Necessity of Beauty

So, why are these things necessary traits of a design. I think it’s absolutely necessary for it’s timelessness. The essence of science is cumulative. We often build our own on top of others. If the thing is beautiful it is more likely to be used by others in current or future generation. And since the whole purpose of the design, at least in programming or math or physical sciences is to get used, it is not surprising that these traits are of paramount important.

One can see examples everywhere. Physical theories like special relativity and maxwell’s equations, systems like C, lisp and unix, mathematics like euclid’s geometry and descartes’ coordinate geometry. Any scientific theory or engineering design that we recognise as outstanding all have these traits.

One can see the role of beauty in a very pragmatic way. If one wants to work with a design of other, it should be simple enough to understand, it must solve a right problem, it must be concise so that it can all be put in head at once. The surprise trait gives design an attractive feeling that makes it more appealing to get used. The word “appealing” may sound frivolous but it’s not. The surprising traits are a direct consequence of solving a hard and right problem.

Getting these in design

So, should one consciously try to have these traits or they just come naturally in a design? I thing simplicity and conciseness can often can be cultivated from the start. In fact they are good heurisitics to start attacking the problem because if you force solution to be simple and force yourself to use as few components as possible, there is more chance that the final product would have beauty. C was deliberately designed to have a minimalist design. The whole language is explained in an unbelivably thin book. As said in the beginning by the authors (one of whom has actually designed the language), a programmer can reasonably expect to learn and use the entire language. An extreme opposite example of this philosophy is C++. Very few can use all of C++. And even if C++ is more widely used than C (for reasons not important here), most of us belive that C is more beautiful than C++.

The surprise element comes up with solving hard and problems viewed in aright way. The surprise traits of special relativity came to einstein only after a decade thinking of a seemingly mundane problem of motion of bodies through ether (which he proved as unnecessary in final theory).

Posted in Uncategorized | Leave a comment

Power of Abstraction

I think fundamentally in our learning experience we constantly strive to keep things as abstract as possible. For example, when were getting started with basic algebra, we were taught the idea of balancing on both sides of equal sign–subtract this number from both sides, divide that number from both sides and so on, until we get what we want. After some time we start taking these ideas for granted and began worrying about bigger problems like how to model real world problems in basic algebra and the like. I realized that the point of the excercises in textbooks is to get the readers start taking the illustrated ideas for granted so that now they can work on more bigger problems. They want to make an abstraction layer of the new ideas.

When I was in high school, I was told by many people to just concentrate on basics or fundamentals. I think I took the idea too literally. Or maybe I misinterpreted their advice. I would force myself to worry about the rules of basic algebra while solving calculus or three-dimensional geometry problems. That made my life very coagulated. I would get so focussed on unduly details that the problem will look much more difficult than it usually is. The point is, if I was forced to solve these problems in a limited time frame, i.e. in tests, I would solve them fine like other kids. But after that I would not get that much satisfaction from that. It was like I have cheated my way through it.

This went for a long time, I think for nearly four years. Maybe more. I was worried that if the foundations are not correctly understood, that will create problems for me later on. What if my intellectual tower of the concepts falls apart because of these hallowed fundamental concepts, wasting years of my effort? These fears contained me for a significant amount of time.

Time went on and eventually I switched my intersts from theoretical physics to computer science. I had to because I diden’t got into any Physics program. I thought I might make a new start here since I was getting started from absoulte scratch. After my first semester, I started studying a book and corresponding course “Structure and Interpretation of Computer Programs”. The course started right away with the idea of abstraction being a central idea of computer science– First we have primitive expressions, then we notice common patterns in these expressions and build procedures that embodies the general rule from which all the computations of that type can be captured. Then we will notice patterns in these procedures themselves and build compound procedures, and so on.

I realized that in programming the basic idea of abstractions was to save ourselves from worrying about details because what will matter is what the function computes, not how it computes as long as it is computing correctly It doesen’t matter if (double x) is implemented as (define (double x) (* 2 x)) or (define (double x) (x + x)). The important thing is the result is correct and sound.

I gradually realized that I should just go on without fearing about what may come ahead. I just trusted myself that what I have understood is right because I have thought about it enough. My life diden’t became a walk on park after realizing this but I started feeling that the difficulties I was now facing was not waste based on irrational fears.

Now the question arises, what if I tomorrow read a new concept that shows incompatibility from a previous understood concept. Well, in that cases one of the two things may happen. Either I had misunderstood the earlier concept or it may lead me to the discovery of a new rule. The second possibiltiy fascinated me because I realized that all nearly all the advances in physics follow this route. This is the source of all our new knowledge.

But what if the first case happens. Well, then my understanding will deepen, what else. I realized that earlier I was afraid not only because the old concept may fail but also because the ideas that I studied between old and the new one would also get hallowed. That seems terrifying if we just look at it. It actually happened to me sometimes. The amazing thing was that it was not a pain at all. All the ideas just align themselves with the new found rule almost instantaneouly. It just felt amazing.

I think if one sees a newthing, one should get as familiar with it as possible so that we start taking that thing for granted. For myself, I think this is the way I can work on problems that truly interst me.

Posted in Uncategorized | 1 Comment