This Place is Taken

Saturday, January 19, 2013

The Flowchart Of Gandalf’s Problem-Solving

The Flowchart Of Gandalf’s Problem-Solving:

He’s a wizard so you’d think he could solve just about any problem all on his own, but even he needs help from a few select sources. This flowchart created by Emil Johansson of the Lord of the Rings Project breaks down Gandalf’s choices and how Hobbits and eagles figure prominently in every plan of attack.
(I Love Charts via Laughing Squid)

Friday, January 11, 2013

The Unreasonable Effectiveness of C

The Unreasonable Effectiveness of C:

The Unreasonable Effectiveness of C

For years I've tried my damnedest to get away from C. Too simple, too many details to manage, too old and crufty, too low level. I've had intense and torrid love affairs with Java, C++, and Erlang. I've built things I'm proud of with all of them, and yet each has broken my heart. They've made promises they couldn't keep, created cultures that focus on the wrong things, and made devastating tradeoffs that eventually make you suffer painfully. And I keep crawling back to C.
C is the total package. It is the only language that's highly productive, extremely fast, has great tooling everywhere, a large community, a highly professional culture, and is truly honest about its tradeoffs.
Other languages can get you to a working state faster, but in the long run, when performance and reliability are important, C will save you time and headaches. I'm painfully learning that lesson once again.
Simple and Expressive
C is a fantastic high level language. I'll repeat that. C is a fantastic high level language. It's not as high level as Java or C#, and certainly no where near as high level as Erlang, Python, or Javascript. But it's as high level as C++, and far far simpler. Sure C++ offers more abstraction, but it doesn't present a high level of abstraction away from C. With C++ you still have to know everything you knew in C, plus a bunch of other ridiculous shit.
"When someone says: 'I want a programming language in which I need only say what I wish done', give him a lollipop."
- Alan J. Perlis
That we have a hard time thinking of lower level languages we'd use instead of C isn't because C is low level. It's because C is so damn successful as an abstraction over the underlying machine and making that high level, it's made most low level languages irrelevant. C is that good at what it does.
The syntax and semantics of C is amazingly powerful and expressive. It makes it easy to reason about high level algorithms and low level hardware at the same time. Its semantics are so simple and the syntax so powerful it lowers the cognitive load substantially, letting the programmer focus on what's important.
It's blown everything else away to the point it's moved the bar and redefined what we think of as a low level language. That's damn impressive.
Simpler Code, Simpler Types
C is a weak, statically typed language and its type system is quite simple. Unlike C++ or Java, you don't have classes where you define all sorts of new runtime behaviors of types. You are pretty much limited to structs and unions and all callers must be very explicit about how they use the types, callers get very little for free.
"You wanted a banana but what you got was a gorilla holding the banana and the entire jungle."
- Joe Armstrong
What sounds like a weakness ends up being a virtue: the "surface area" of C APIs tend to be simple and small. Instead of massive frameworks, there is a strong tendency and culture to create small libraries that are lightweight abstractions over simple types.
Contrast this to OO languages where codebases tend to evolve massive interdependent interfaces of complex types, where the arguments and return types are more complex types and the complexity is fractal, each type is a class defined in terms of methods with arguments and return types or more complex return types.
It's not that OO type systems force fractal complexity to happen, but they encourage it, they make it easier to do the wrong thing. C doesn't make it impossible, but it makes it harder. C tends to breed simpler, shallower types with fewer dependencies that are easier to understand and debug.
Speed King
C is the fastest language out there, both in micro and in full stack benchmarks. And it isn't just the fastest in runtime, it's also consistently the most efficient for memory consumption and startup time. And when you need to make a tradeoff between space and time, C doesn't hide the details from you, it's easy to reason about both.
"Trying to outsmart a compiler defeats much of the purpose of using one."
- Kernighan & Plauger, The Elements of Programming Style
Every time there is a claim of "near C" performance from a higher level language like Java or Haskell, it becomes a sick joke when you see the details. They have to do awkward backflips of syntax, use special knowledge of "smart" compilers and VM internals to get that performance, to the point that the simple expressive nature of the language is lost to strange optimizations that are version specific, and usually only stand up in micro-benchmarks.
When you write something to be fast in C, you know why it's fast, and it doesn't degrade significantly with different compilers or environments the way different VMs will, the way GC settings can radically affect performance and pauses, or the way interaction of one piece of code in an application will totally change the garbage collection profile for the rest.
The route to optimization in C is direct and simple, and when it's not, there are a host of profiler tools to help you understand why without having to understand the guts of a VM or the "sufficiently smart compiler". When using profilers for CPU, memory and IO, C is best at not obscuring what is really happening. The benchmarks, both micro and full stack, consistently prove C is still the king.
Faster Build-Run-Debug Cycles
Critically important to developer efficiency and productivity is the "build, run, debug" cycle. The faster the cycle is, the more interactive development is, and the more you stay in the state of flow and on task. C has the fastest development interactivity of any mainstream statically typed language.
"Optimism is an occupational hazard of programming; feedback is the treatment."
- Kent Beck
Because the build, run, debug cycle is not a core feature of a language, it's more about the tooling around it, this cycle is something that tends to be overlooked. It's hard to overstate the importance of the cycle for productivity. Sadly it's something that gets left out of most programming language discussions, where the focus tends to be only on lines of code and source writability/readability. The reality is the tooling and interactivity cycle of C is the fastest of any comparable language.
Ubiquitous Debuggers and Useful Crash Dumps
For pretty much any system you'd ever want to port to, there are readily available C debuggers and crash dump tools. These are invaluable to quickly finding the source of problems. And yes, there will be problems.
"Error, no keyboard -- press F1 to continue."
With any other language there might not be a usable debugger available and less likely a useful crash dump tool, and there is a really good chance for any heavy lifting you are interfacing with C code anyway. Now you have to debug the interface between the other language and the C code, and you often lose a ton of context, making it a cumbersome, error prone process, and often completely useless in practice.
With pure C code, you can see call stacks, variables, arguments, thread locals, globals, basically everything in memory. This is ridiculously helpful especially when you have something that went wrong days into a long running server process and isn't otherwise reproducible. If you lose this context in a higher level language, prepare for much pain.
Callable from Anywhere
C has a standardized application binary interface (ABI) that is supported by every OS, language and platform in existence. And it requires no runtime or other inherent overhead. This means the code you write in C isn't just valuable to callers from C code, but to every conceivable library, language and environment in existence.
"Portability is a result of few concepts and complete definition"
- J. Palme
You can use C code in standalone executables, scripting languages, kernel code, embedded code, as a DLL, even callable from SQL. It's the Lingua Franca of systems programming and pluggable libraries. If you want to write something once and have it usable from the most environments and use cases possible, C is the only sane choice.
Yes. It has Flaws
There are many "flaws" in C. It has no bounds checking, it's easy to corrupt anything in memory, there are dangling pointers and memory/resource leaks, bolted-on support for concurrency, no modules, no namespaces. Error handling can be painfully cumbersome and verbose. It's easy to make a whole class of errors where the call stack is smashed and hostile inputs take over your process. Closures? HA!
"When all else fails, read the instructions."
- L. Lasellio
Its flaws are very very well known, and this is a virtue. All languages and implementations have gotchas and hangups. C is just far more upfront about it. And there are a ton of static and runtime tools to help you deal with the most common and dangerous mistakes. That some of the most heavily used and reliable software in the world is built on C is proof that the flaws are overblown, and easy to detect and fix.
At Couchbase we recently spent easily 2+ man/months dealing with a crash in the Erlang VM. We wasted a ton of time tracking down something that was in the core Erlang implementation, never sure what was happening or why, thinking perhaps the flaw was something in our own plug-in C code, hoping it was something we could find and fix. It wasn't, it was a race condition bug in core Erlang. We only found the problem via code inspection of Erlang. This is a fundamental problem in any language that abstracts away too much of the computer.
Initially for performance reasons, we started increasingly rewriting more of the Couchbase code in C, and choosing it as the first option for more new features. But amazingly it's proven much more predictable when we'll hit issues and how to debug and fix them. In the long run, it's more productive.
I always have it in the back of my head that I want to make a slightly better C. Just to clean up some of the rough edges and fix some of the more egregious problems. But getting everything to fit, top to bottom, syntax, semantics, tooling, etc., might not be possible or even worth the effort. As it stands today, C is unreasonably effective, and I don't see that changing any time soon.
Follow me on Twitter for more of my coding opinions and updates on Couchbase progress.

Posted January 8, 2013 1:00 PM

Saturday, December 29, 2012

One Idiot – A fun & nice movie by IDFC on Financial Literacy

One Idiot – A fun & nice movie by IDFC on Financial Literacy:
IDFC foundation has released a small 30 minutes movie called “One Idiot to spread financial literacy for today’s generation which feels that life is all about spending and looking “cool”. The movie is directed by Amol Gupte, who had also directed the movie “Taare Zameen Par”. The movie ‘One Idiot’, shows how a bunch of students who are in their early 20′s make fun of a guy who looks dumb and does not believe in showing off, only to find out later one day that he is actually a multi millionaire, living and enjoying his life. The overall message of the movie is that you have to be prudent and responsible when it comes to money and start your systematic investments however small they are and over a long term, you will be on path of financial freedom.


I watched the movie few months back when someone from IDFC had asked for my comments on the movie. Overall I think you should watch this movie and also share it with your children who are in school and going to enter their working life. Watch the movie and share what you liked about the movie ? Do you think its able to give that message of “saving and investing that money is important from starting” .

Monday, December 24, 2012

An Interview With the Nativity Innkeeper

An Interview With the Nativity Innkeeper:

Your name, please.
Ben Cohen.
Occupation?
Retired. I was an innkeeper.
In Bethlehem.
Right.
And in fact it was your inn where Jesus was born.
That’s right. Well, not in the inn itself. Out back.
In the animal shed.
Yeah. I still get a lot of flak for that.
How do you mean?
I mean that people still criticize me for not having room at the inn. They say to me, you couldn’t give a pregnant woman a room? You couldn’t give a room to the woman pregnant with the divine child? Couldn’t even spare a broom closet for the Baby Jesus?
How do you respond to that?
I say, well, look. First off, it wasn’t just me. If you go back you’ll see that every inn was full.
Because of the census.
Census, schmensus. It was the foot races. Bethlehem versus Cana. Also there was a touring theater troupe from Greece. Only appearance in Judea. The city was packed. We had reservations for months.
But Mary was pregnant.
I had three pregnant ladies at the inn that night. One was giving birth when Joe and Mary showed up. She was down the hall, screaming at the top of her lungs, cursing like you wouldn’t believe. Her husband tried to encourage her to push and she kicked him in the groin. Think about that. She’s crowning a baby, and she takes the time to put her foot into her husband’s testicles. So maybe you’ll understand why even if I had a room, I wouldn’t be in a rush to give it up to those two.
But you ended up letting them go out to the animal shed.
That was an accident.
How so?
Joe comes in and asks for a room, and I tell him we’re all out of rooms and have been for months. Foot races. Theater groupies. And such. And he says, come on, please. I’ve got a pregnant lady with me. And I say, you hear that down the hall? I’m full up with pregnant ladies. And he says, this baby is important. And I say, hey, buddy, I don’t care if he’s the Son of God, I don’t have any rooms.
So there’s some irony there.
I guess so. And then he says, look, we’ll take anything. And so I say, as a joke, all right, you can go and sleep with animals if you like. And he says fine and slaps some money on the counter.
He called your bluff.
Yeah. And I say, I was kidding about that. And he says, and my wife’s water just broke in your lobby. What could I do? I pointed him in the direction of the animals.
It’s better than having the baby in the street.
I suppose so, but you know, if the reason they were in Bethlehem was because of the census, then he had family in the area, right? It’s his ancestral home and all that. He can’t say to a cousin, hey, give us a couch? There are some family dynamics going on there that have been conveniently left unexamined, if you ask me.
Joseph had a lot on his mind.
Must have.
So the baby is born, and they place him in the manger.
Which, by the way, I told them not to do.
Why?
Because how unsanitary is that? Do you know what a manger is?
As far as I know, it’s the place you put infant messiahs.
It’s a food trough for animals.
Oh. Interesting.
“Oh, interesting” is right. Let me ask you. So your baby is born, and the first thing you do is put him in an open container filled with grain and covered in oxen drool? Does this seem reasonable to you?
You did have them out with the animals. Their options were limited.
I rented cribs. I asked Joseph, do you want a crib. And he said, no, we’re fine, and then sets the kid in the food box. And I say to him, you’re new at this, aren’t you.
In his defense, he was.
And then someone says, look, the animals, they are adoring the baby. And I say, adoring, hell. They’re wondering why there’s a baby in their food.
On the other hand, the image of the Baby Jesus in the manger is a classic one.
Yeah, I mention that when people get on my case about not giving Joe and Mary a room. I tell them that having a Christmas carol called “Away in a Hotel Room” doesn’t have quite the same ring to it. They never have anything to say to that.
It’s said that a star appeared on the night when Jesus was born. Did you see it?
No. I was too busy trying to convince Joseph to rent a crib.
It’s said it was bright enough to lead the Three Wise Men to your inn.
Well, three men showed up at the inn. I don’t know how wise they were.
How do you mean?
The baby is born, right? And then these guys show up. And they say, we have brought gifts for the child. And I say, that’s nice, what did you bring. And they say, we have brought gold and frankincense and myrrh. And I say, you’ve got to be kidding.
What’s wrong with that?
Let me quote another Christmas song for you. “A child, a child, shivers in the cold, let us bring him silver and gold.” Really? Silver and gold? And not, oh, I don’t know, a blanket? An newborn infant is exhibiting signs of possible hypothermia and your response is to give him cold metal objects? Who ever wrote that song needs a smack upside the head.
You’re saying the gifts were inappropriate.
What’s wrong with diapers? A nice jumper or two? A Baby Bjorn? They were riding around on a donkey, you know. A Baby Bjorn would have come in handy. Have you ever in your life gone to a baby shower where someone says, congratulations on the baby, here’s some perfume. No. Because most people have some sense.
I think the idea is that all the gifts were fit for a king.
Yes, a king who first pooped in my animals’ manger. I would have appreciated a gift of diapers.
Point taken.
And another thing, they brought all these expensive gifts, but do you ever hear about Joe and Mary and Jesus being anything but poor? Or at the very most working class?
Now that you mention it, no.
Exactly. I think what happened is these three guys show up and they say, here are all these expensive gifts we got your baby. Oh and by the way, we happen to know King Herod thinks your baby’s a threat and plans to kill every kid younger than two years of age just to be sure, so you better go. Egypt’s nice this time of year. What? You’re traveling by donkey? Well, then you can’t take all these nice gifts with you. We’ll just hold on to them for now, write us a letter when you get settled and we’ll mail them. And then they never do.
I don’t think there’s scriptural support for that theory.
I’m not saying I have any evidence. All I’m saying is that it makes sense.
After the Three Wise Men, were there other visitors?
Yeah. It got a little crowded. The animal sheds aren’t designed for a large amount of foot traffic. And then that kid showed up with a drum, and I said, all right, fine, we’re done.
The song of that incident suggests the drum went over well.
Let me ask you. You’re a parent, your child has just been born, he’s tired, you’re tired, people won’t leave you alone, and then some delinquent comes by and unloads a snare solo in your baby’s ear. Does this go over well?
Probably not, no.
There you go.
After the birth, did your inn benefit from the notoriety?
Not really. Jesus kind of slipped off everyone’s radar, for, what? Thirty years? Thirty-five?
Something like that.
Right. So there wasn’t much benefit there. I got some mileage out of telling the story about the crazy couple who rented my animal shed, and the visitors, and the drumming, but I mostly told it to friends. Then just as I’m about to retire someone tells me of this hippie preacher in Jerusalem who got in trouble with the Romans. And I say, hey, I think I know that guy. I think he got born in my shed. And then, well. You know what the Romans did to him.
Yes.
Romans, feh. Then I sold the inn to my nephew and retired to Joppa. By the time Jesus became really famous I was out of the game. And then my nephew sold the inn and they put that church there.
The Church of the Nativity.
You been?
I have, yes.
It’s nice. I liked the inn better, of course.
Looking back, would you have done anything differently?
I would have comped Joseph the crib.
That still would have changed the Christmas carol.
I know. But, look. You didn’t have to wash out that manger.