Philosophy


When coding there are many guidelines you might opt to follow, such as Convention Over Configuration, the Principle of Least Surprise, and others, all intended to prevent you from falling into certain pitfalls. One such pitfall is one that I myself often fall victim to, and involves spreading the knowledge of the internal workings of one component to several others. That is, if A, B, and C all know how D works, then there’s really little point in grouping functionality inside D in the first place. Avoiding this pitfall is what the Law of Demeter tells us to do. To quote the Wikipedia article:

When applied to object-oriented programs, the Law of Demeter can be more precisely called the “Law of Demeter for Functions/Methods” (LoD-F). In this case, an object A can request a service (call a method) of an object instance B, but object A cannot “reach through” object B to access yet another object to request its services. Doing so would mean that object A implicitly requires greater knowledge of object B’s internal structure. Instead, B’s class should be modified if necessary so that object A can simply make the request directly of object B, and then let object B propagate the request to any relevant subcomponents. If the law is followed, only object B knows its internal structure.

Unfortunately this is done all the time in Rails, partly because they make it so darn easy to access associations and their associations:

  1.  
  2. shipment.user.profile.mailing_address
  3.  

Looks innocent enough. But what happens when we sprinkle this around our codebase in a variety of forms, and then without warning the requirements change and all of a sudden the mailing address is now attached to the user rather than the profile. This will require going through all your code that might look for the mailing address and updating it. You’d better pray you have near 100% test coverage.

The unpredictable and despotic need for change that creeps into every project is the reason you should care about the Law of Demeter, also called the Principle of Least Knowledge. But what do we do about it? The Wikipedia excerpt above makes it pretty clear that we should define mailing_address on Shipment and User:

  1.  
  2. class Shipment < ActiveRecord::Base
  3.   …
  4.   def mailing_address
  5.     user.mailing_address
  6.   end
  7.   …
  8. end
  9.  
  10. class User < ActiveRecord::Base
  11.   …
  12.   def mailing_address
  13.     profile.mailing_address
  14.   end
  15.   …
  16. end
  17.  

Okay, fine. Not the prettiest, but it does help us with refactoring. You may have noticed a problem with both the old code and the new version: what if one of the associations is nil? We’ll get a big fat NoMethodError of course! Let’s fix that:

  1.  
  2. class Shipment < ActiveRecord::Base
  3.   …
  4.   def mailing_address
  5.     user ? user.mailing_address : nil
  6.   end
  7.   …
  8. end
  9.  
  10. class User < ActiveRecord::Base
  11.   …
  12.   def mailing_address
  13.     profile ? profile.mailing_address : nil
  14.   end
  15.   …
  16. end
  17.  

Getting kinda ugly, but it works better now. Both refactoring and nil problems are taken care of. Now that we’ve got that, we can rip it out. A while back Rails got a method called delegate that’ll let us do just this type of thing, providing both the refactoring safety and nil safety. Using this method we can change our code to this:

  1.  
  2. class Shipment < ActiveRecord::Base
  3.   …
  4.   delegate :mailing_address, :to => :user
  5.   …
  6. end
  7.  
  8. class User < ActiveRecord::Base
  9.   …
  10.   delegate :mailing_address, :to => :profile
  11.   …
  12. end
  13.  

Isn’t that cool? Now it’s nice and semantic, safe, and refactorable!

When I was in high school, I sometimes found myself in my counselor’s office when it wasn’t strictly necessary. He was a bright guy who enjoyed talking with intelligent students, and I was happy to get away from many of the other students at times. However, he was also the girls’ basketball coach, so they often hung out in there too. This combination prompted a few memorable quotations from the basketball girls, one of which was, in response to something I’d said, “You’re one of those people who knows too much for his own good, aren’t you?”. At the time I was kind of shocked by this deliciously ambiguous question.

I look back on it after having read The God Delusion, and it strikes me that perhaps this question comes from a desire not to know, not to be challenged, and to make it easier to believe what the questioner has already decided to believe. After all, one good way to protect one’s invalid beliefs is to block any evidence that might contradict them. I’ve not stumbled onto anything new here, but I find it interesting how pervasive this attitude is. The thing I should be most concerned about is heading off this tendency in myself.

Or maybe I should just turn my brain down a notch and not worry about it so much. Hah, right…

One so rarely gets to use the word “concubine” in a sentence, much less a song.

I’ve been reading a book that was given to me as a gift, “The Monk and the Philosopher : A Father and Son Discuss the Meaning of Life”. It’s interesting that I agree, in part, with most of Buddhist philosophy and at the same time I agree, in part, with most of Ayn Rand’s philosophy. In “Atlas Shrugged”, one small character is a woman who once had partial ownership of a motor company. She and her siblings inherited the company from their father and decided to make it into a communist system.

They turned the company to shit, in other words, with the lives of its workers and their families with them. When the main protagonist of the book, Dagny Taggart, went to meet this woman, Dagny thought to herself as the woman was speaking that she should remember this moment, because she was seeing pure evil in its unadulterated form. This woman was a self-proclaimed Buddhist.

As I said, I don’t agree with either philosophy entirely. At the same time, it’s disheartening to see how sharply they disagree. It’s like you have two good friends who can’t stand each other.

Both seem to ignore certain parts of biology and its implications for human nature and philosophy. I believe that human nature is to be inherently selfish. Buddhism kind of acknowledges this. Rand celebrates this. What, then, do they ignore? They ignore the subtleties of the theory of human nature as espoused by Richard Dawkins. For example: Ayn Rand claims, through John Galt, that human nature contains no inherent altruism, and that any tendencies toward it are signs of corruption.

Perhaps Rand expects us to act against our natural tendencies, but that’s not what I get from reading her. I won’t cover Dawkins’ theory in detail, but it comes down to that genes, and not humans or other “individual” animals, are the base of replication. This means that they “do their best” to achieve their own good, with the good of the individual they reside in being closely tied, but not identical, to their own welfare. This can result in altruistic behavior, especially toward those individuals likely to carry copies of many of the same genes you do (i.e. your family, especially siblings and parents). Read more about it in “The Selfish Gene”.

If that’s the case, then Rand must make some case for acting against our inner drive to occasionally be altruistic. I haven’t read anything of the sort, but I can guess how it would go: one would be better off without acting on the pre-programmed impulses given to us by our genes, and instead relying on reason to make decisions.

Buddhism, I believe, doesn’t take into enough account the truly selfish nature we humans have. For beings such as us, letting go of our conception of “I” and “me” is very, very difficult. Even so, how are we to know that doing so will lead us to a better place than we are in now?

I suspect that the problem between the two sides of this coin is one primarily of semantic sensitivity. Rand abhors the word “altruism”, while Buddhism strives to eliminate the problems of the “ego”. Reconciling the two would be a difficult task for a seasoned philosopher, and I would love to read a book on the subject. Until such a book comes around, I’ll be forced to try to live with this apparent contradiction.

As John Galt says, there are no contradictions in the world - if you think you’ve found one, check your premises.

Technorati Tags: , , ,