Sunday, November 13, 2011

To Type or Not to Type

Again, the superiority of dynamically typed language is taken up by a hacker over hacker news. This time, with a different tack. Types, according to our lovely author, is not a sign of sadism and/or masochism where one derives great pleasure out of torturing oneself and everyone else with all of those stupid declaration; rather, it is a sign of n00bs, who can't comment enough in there code.

Oh, how I hate the word n00b. In thy name, how many bugs/stupid features have been created? In the olden days, the number 1 cause of bugs were efficiency, which modern programmers seem to abandon amass (for a good reason, of course). These days, n00b-proof is their war banner for creating bugs.

Back to the discussion. Are new programmers more drawn to comments and statically typed system? I doubt it. Actually, it acts the reverse: new programmers (aka n00bs) detest those things. After all, they do not learn how to program to do those dumb and boring task. Heck, in programming classes, even calculating Fibonacci numbers are too dumb, too useless for those newbies. Nay, they want "cool" new tech, like GUI, like 3D, like games, like Web, etc. Things that they can show off to their n00b-er friends; things that can earn them millions in days. Oh, and they want those things yesterday.

Generally speaking, I see the urge to organize, model, and document code as a sign of maturity, and the actual action of doing so a sign of discipline. It is easy to see why: writing those documentations require effort, great effort even since they are not particularly creative or fun. Creativity and fun sustain a programmer; activities without these hurts, sometimes physically. Extending extra effort to do those activities, thus, is hard, and great discipline is required to do so. Thus, calling those with the discipline to do things that you hate n00bs is not just insulting: it's stupid and n00bs.

However, is type necessary, even? (since sadist and masochist people like pain, too).

Types are unnecessary, if your program is hacked together within about 2 - 3 sessions, and its useful lifespan is within about a month, or less. Anything more than that, and you are begging for troubles, not even in the long run.

First and foremost, let's make this clear: you need to know meta anyway. You need to know what a function does, what it expects, and what it returns. You do. Otherwise, you will start to add "2" to 1. That's mild. How about asking a missile system to shoot at pi number? Thus, you need to know what a function expects, and what it will return.

Now, the question becomes: how do you remember this meta? First note: you will need this 2 weeks from now. Second note: your buddy writes the function for you. Third note: oh, that function is too slow, so you need to rewrite it.

There are 2 schools of thought on this matter, as far as I see: there are the n00bs, stupid, incapable people who do not believe in their miserable brains; and there are super uber programmers/hackers who can remember everything. The former ones would prefer to write down the meta information, preferably in such a manner that it will be forced to keep up with the code base (aka the type system); the latter just keep it in their minds, and re-read the code, follow the logic when they (verily occasionally) forget.

You pick: which one is more usable?

I am seriously amazed at how people talk about a program or a code base as if it is a living, breathing creature: it needs freedom. Dynamically typed system frees the code from this, from that. As if the code cares about those grand freedom. In the end of the day, no matter what language you use, no matter what feat you attempt, a computer program calculates some pieces of data, and put them in some appropriate places. End. In addition, the most important aspect of a program is to calculate the the right thing, and put the right info into the right places. All programs, thus, are inherently deterministic and static.

Now, I will admit this: static type system can be painful from time to time. Sometimes, the pain is good. For example, it requires that you know what you are trying to do. I greatly admire and am entertained by the notion of "exploration programming," in which you do a task that you have no clue what it is. It invokes the image of Columbus striving into the unknown, doing the unthinkable. And we still call Native Americans Indians. But seriously, this is programming, not exploring. We want our programs to be right, to solve our issues, not to embark in a great saga. Thus, we want to know what we are doing, so we can do the right things.

Sometimes, the pain of static type system is in deployment: for the most cases, statically typed code requires separate compilation, and modification cannot be done on-the-fly. However, this can be solved, more or less, in a good setup. These days, you can create a system such that upon commit, it can compile all of the new code, switch over to backup server, deploy new binaries to the main server, switch back to main server, deploy new binaries to the backup server. It's not exactly on-the-fly, but hourly is totally feasible. And let's be frank, if your customer service is good, how bad it is to ask the customers to wait a day? For most cases (provided it is really a day), not that big of a deal. How about this case: upon a specific input, your system will throw an exception, and will print the stack trace, which reveals your database structures, along with password and username to the table to contain all personal information, to the user?

Lastly, it can be a pain to type in the declaration. However, very frankly, except the case where you actually use Notepad (without the plus plus) to write code, this is largely a solved issues. Heck, IDEs these days will put in those code for you with rather high accuracy!

All in all, the question of typing is more of a personal matter than anything else. If you would like, I believe that it is provable how static type helps a code base reduce bugs (hell, it removes a class of bugs), improve long term (or even short term, when the dude who wrote the code takes off for a month and his code needs to be modified) productivity, and generally make life easier. It can also be proven that all of the talking about types is just waste of everyone time, and all things can be done in all languages (except explicit toy ones), and that we should get back to work instead of arguing over silly things. Your call.