Wednesday, November 9, 2011

An interesting find

We needed to expose a resource such that its url looked like:

http://www.example.com/widgets/<url-encoded-identifier>

In some cases, we found that the request never made it to the controller. The reason for that was when the identifier contained %2F (url encoded '/') followed by other characters, it appeared as if either apache or passenger url-unencoded it before trying to figure out who send the request to. And it couldn't find anything, so it returned a 404.

If we start the server using rackup instead of via Apache Passenger, the request would be routed correctly.

Now, if the identifier contained a space (urlencoded as +), then we saw another problem. We know Rails tries to help by unescaping parameters. When the param comes from a URL path, Rails uses URI::unescape. This leaves the + in! The parameter received by the controller is incorrect. If it was only the +, it would be one thing. It turns out that URI::unescape is depracated in Ruby 1.9.2 and it doesn't work for a few other characters as well. So gsub wasn't an option.

In Ruby 1.9.2, URI::unescape is replaced by CGI::unescape. And it behaves correctly. However, in Ruby 1.8.7, CGI::unescape is not considered stable. So, Rails had to decide which Ruby it will support better. It seems they have taken a half-way approach. Rails uses URI::unescape when unescaping params from the URL path, but it seems to use CGI::unescape when the param comes from the query string!

Given the two issues, we had to change our URL structure to something like:

http://www.example.com/widgets/<hex-identifier>?id=<url-encoded-identifier>

Grrr...

Does anyone have a better solution?

Saturday, July 16, 2011

What is Object Oriented Programming?

I find that a lot of people with a lot of experience in "Object Oriented Programming" really struggle to understand what it actually is. I wanted to find an easy way for people to learn Object Oriented Programming.

Here it is: Dr. Alan Kay on the Meaning of “Object-Oriented Programming”

As you reflect upon what you currently consider "OOP", remember an all-too-common anti-pattern: The Anemic Domain Model.

That's it! Its that easy! Now try to put it into practice and see if TDD helps you in that quest.

To see how you are doing, here's an easy way to determine whether or not you have taken the first step towards OOP:

  • Does a single class have setX() and getX() methods?
  • If so, then you fail. Its not OOP.
  • Please try again.

Sunday, August 8, 2010

Ruby and Snow Leopard upgrade

I finally decided to plonk down $29 and install Mac OS X Snow Leopard. When I tried running Cucumber, I got an error about the Gherkin lexer... "gherkin_lexer_en.bundle: mach-o, but wrong architecture". It turns out this was caused by the upgrade to Snow Leopard.

I had to upgrade mac ports, delete all my old gems and reinstall them. I presume vendorized gems will need to add a new 64-bit version.

Here are some links I found useful in this quest:


Hope that helps!

Wednesday, December 16, 2009

New Relic is awesome!

We setup New Relic in production yesterday. Its awesome! If you're running a rails project of any size, I would say its an indispensable tool.

Here's a brief review:

What it has meant to us:

  • Helped us fix 2 new performance problems in 2 hours.
  • Helped us identify an intermittent performance problems that we hadn't been able to figure out for the last 3 months (that was mainly because we weren't allowed to increase log level or access apache logs, because of IT bureaucracy. NewRelic allowed us to side-step all of them).

Pros:

  • Easy to setup. All it took was to vendorize a gem and add a yml file.
  • The ability to see what action is taking the longest and what partial rendering is taking the longest and what sql is taking the longest is awesome.
  • Made for rails
  • Pretty graphs. Keeps managers busy for hours. ;-)
  • Doesn't seem to make any material impact on the performance of the app.

Cons:

  • No visibility into Java code (we run on JRuby in JBoss and have to access some Java code - which is usually the problem). I wonder if there's a way to instrument it so certain calls are visible to New Relic. UPDATE: Yeah, there is a way to see what the Java stuff is doing. It works as advertised, but not as automagically as the Rails logging. Also, we weren't able to get it to log to the same "application". Had to give it a new name.
  • Rails only as far as I can tell. It won't work for your Java or .NET project. Sorry!
  • Have noticed a couple of stack traces with "new_relic" in them. Need to investigate why.
  • newrelic.com was down for a few minutes today. That meant we couldn't stare at the pretty graphs for that time period. Will have to see if uptime is good enough for what we're doing over the next month or so

Sunday, December 13, 2009

On Maven

Maven sucks. It's evil. It makes me want to scream! Why? Three big reasons: it hides problems, it promotes the dysfunctional organization of your team and it stains to your code like blood on cotton.

I know, I know, I shouldn't be quick to judge. Any tool can be misused. Maybe if people learned to use the tool better.

Uh, no!

After spending several months working extensively on fixing (or introducing) continuous integration across industries, team sizes, geographies and levels of complexity, I have no doubt in my mind that Maven is the problem. And I swear I went into each engagement with an open mind - even towards Maven.

The first thing that hit me is that it hides all kinds of problems. I get a jar from someone else's code. Great! I keep working, everything is great. Now I want him to add a new feature. I need to upgrade to the latest version! Oh no! The two code bases have deviated too much! Lets make a rule. We need things to be backwards compatible. Lets not change API. Lets only add new methods. Oh, and maybe if I depend on a SNAPSHOT of the other person's code. But now, he can't change his code willy-nilly. We need a change manager to coordinate changes between teams.

Then the next problem hits. Hey, why are you touching that code? Its my team's code. If you want it to do that, you need to come through the change manager. Lets meet and discuss why you need to upgrade your JDK.

Then a consultant comes along and says "WTF?" - you can do all this by not having artificial divisions in teams and code bases. Lets collectively own the code. If different things need to behave differently at different times, then lets refactor the code to meet these needs. But, uh, fixing it within Maven's world is cheaper and takes less time than combining all the code. Oh, and refactor - that sounds risky! Lets just keep going with what works for now even if we're a little less efficient.

Little does anyone see that their development velocity is excruciatingly low and the incremental investment in maven over time is a lot larger than following a simpler path. And they are completely missing what they really need to be doing: communicating more, gelling as a team, and getting into a flow.

AAARRGGGHHHH!!!!

Saturday, April 5, 2008

What is education?

Since 1999, I have been volunteering with Asha for Education - a non-profit focusing on education to wedge disenfranchised communities in India towards socio-economic development.

I was listening to The Economist (audio edition) and it brought up the topic of financial education. I had been mulling over this topic and related topics for years and all of a sudden I had an urge to write down my thoughts. So here it goes.

Political Context

To the British, educating Indians was a way to filter the best minds and train them to do the Empire's bidding. Initiative had to be suppressed. Meta-thinking, culturally and locally relevant knowledge had to be discarded in favor of abstract non-contextual, specific subjects. That Gandhi, Tagore and Ramanujam came out of this system might be seen as a testament to the quality of education afforded to them. However, I see it as disservice to the masses whose determination and intellect didn't reach super-human levels.

Around the world, there are many examples of explicit and implicit subjugation of inquiry for the sake of dogma. Religious indoctrination, communist propaganda, unquestioning prostrations to capitalism and nationalistic fervor are examples of the same ill - closed mindedness. Since this is exactly what education is supposed to counter, one can see how the influence of political context on education defeats the purpose.

Defining Education

I divide education into two realms: navigating the world as it is today and open-mindedness. The first must be relevant in your context. The second allows you to adapt with your context, or more importantly change your context as your needs evolve.

Navigating the world as it is today

First, you need to be able to read, write and do arithmetic. The three-Rs. Its so obvious, right? Yet so many people, even in the west, get it oh so wrong. There are two common threads: 1) Minorities and immigrant groups are almost always behind. Eg: Lower castes in India, African-Americans in the US, Turks in Germany, new immigrants in Sweden. 2) Homogenous populations, especially of affluent communities do well.

That means one thing: people in power set the rules which the outsiders cannot easily navigate. So, even to do something as ‘simple’ as the 3Rs, context is king.

The solution is two fold: improved techniques and curriculum customized to context.

Improved techniques

A critical phase where teaching techniques need to be improved are in the first six years of a child’s life. Here’s a short list:

  • providing a safe environment conducive to exploring,
  • proper nutrition and physical activity,
  • using pictures and sounds at a very early age,
  • teaching words before letters,
  • exposure to multiple languages and a diverse group of people.

None of these are new concepts. See Jean Piaget. Yet, somehow, we manage to de-prioritize, sacrifice or otherwise ignore them.

Custom Curriculum

The criticism for customizing curriculum circles around differing standards. How can you measure progress across the varying curricula? How do you know if you are providing ‘equal’ education? I say lets worry about that at 10th grade. Until then, the path a child takes could be and should be relevant to the particular environment the child is in. A child in coastal regions should learn how to fish and a child in a desert should learn how to ride a camel. What is important is how well they do in their environment and that isn’t always comparable.

Specifically, these would make a huge difference:

  • Building friendships with people who are different than you and are in your general neighborhood
  • Learn local crafts and skills - from where to sail to catch fish to when to sow what to how to make a pot.
  • Local language, stories and math in local context

There are additional aspects that are either ignored today or not universally available. Here’s another list:

  • Financial education
  • Entrepreneurship
  • Self-governance
  • Sex education
  • Health and sanitation
Open Inquiry

When it comes to contextual learning, Vygotsky’s work is very pertinent. Given that context plays a huge role in the development of any individual, it is entirely possible to create a compliant society. However, the next challenge lies in rising above it. How do you create a self-correcting, sustainable and peaceful society? That’s where open inquiry comes in.

To get there, three things need to be nurtured:

  • Discovery oriented education
  • Initiative
  • Meta-cognition

I remember how I learned as a child. I learned a lot of things by watching others and imitating - cricket, for instance. But I also learned things by discovering them. I discovered sewage treatment plants in the forest. I discovered Mercury (its found in thermometers). I discovered electricity (its found in batteries). I even discovered non-conformism before I ever heard of Thoreau. While following others and doing as you are told are important, the next level of learning takes place through discovery.

When a child discovers something, it increases self-confidence, it increases curiosity and it increases initiative. I believe discovery oriented education, be it in science, social studies, language or geography, is critical in a child’s intellectual development.

As self-esteem, joy of learning and initiative are established, it is also important to step outside the Vygotskian trap - that of social context. It is critical that kids are introduced to those on the other side of the tracks. Further, making friendships across caste, ethnic or class boundaries allows one to question one’s own perspectives and biases. It leads to open inquiry and a meta-level understanding of one’s society and self.

But why?

I don’t buy into too many absolutes. The end-goal isn’t the ideal state defined by any particular ideology. To me, the end-goal is two-fold: graceful adaptability in an ever-changing world and continuous drive to make the world a little better. 

This includes how we react to daily hurdles as well as to tragedies. Lets take tragedies first. There are several things that are tragic. The ones that get me riled up the most though are when people with the capacity to change a tragic situation don’t - either because they don’t want to go against the grain, or because they don’t see the tragedy or even worse they think somebody else should do it. Whether it be Darfur or the Tsunami or Hurricane Katrina. As a people, we can react sooner and more effectively if we (myself included) had a greater empathy, initiative and sense of responsibility. This can be developed as outlined above.

Day-to-day hurdles, on one hand, are a practice-field for how we react to the big ones. On the other hand, they are much more relevant and have a greater impact on each individual. How do we react to finding ourselves in debt or in a racially segregated city (like Chicago) or in a jealous rage or in the middle of an armed conflict or around a corrupt bureaucracy?

I believe the answer lies in education as defined above.

Friday, April 4, 2008

Murder!

(Spoiler alert: Overlook by Michael Connelly and Simple Genius by David Baldacci)

Recently, I read Overlook by Michael Connelly. A few chapters in it was pretty easy to guess who the murderer was. The story kind of dragged into terrorism and that word 'Allah'. It was a cheap attempt at exploiting both America's national fear and leaps in logic based on stereotypes. Pretty early on it was obvious that the wife - Alicia - was one of the murderers. The whole terrorism thing was just a distraction. (The victim was screaming his wife's name when he was shot - hence the sound of "Allah" immediately followed by a gun shot). Well written, good twist at the end, but not a great story, IMO.

Soon after, I read Simple Genius by David Baldacci. I liked this book a lot better. The story was good and the surprises were actually unexpected and yet realistic in the context of the story. When it was discovered that one of the murder victims had a love interest in a woman named Alicia, I immediately thought of the Alicia in the Overlook and wondered if this Alicia was a murderer too.

Sure enough, she was! How weird is that? Two consecutive novels with a murderer named Alicia!