Ruby : Evil of Inheritance

As you know that `access modifiers` in ruby  are concerned with how a message is passed rather than who is receiving the message. See my previous blog post for more info about access modifiers in ruby.

The main problem is, there is no proper encapsulation from ancestors to child inheriting them. Every piece of code that parent has is redefined in child as well. So problems like below might occur frequently

Dirty Namespace

Lets say you have long chain of inheritance like A < B, B< C, C < D and so on. If class A is defined in your code base and other classes belong to some other library you used, then all implementation details (method && attributes && @instance vars) are defined in your namespace too.

What can go wrong?

Well, since there can be 100s of methods counting( private / protected  / public) you cannot know the name of all the methods. You might accidentally override the existing behavior or @attributes and things will get messier.

There is no way you can keep parent’s methods fully private.

Avalanche of methods / Gigantic interface

Consider a model in Rails inherited from ActiveRecord::Base say `User`. As there was movement saying `Skinny Controllers fat Models`, the god object `user` has so many methods defined in it (normally 100s app specific methods). Now counting the methods it inherited from its ancestors it will reach 1000s.

In my case its found to be

> User.instance_methods.count + User.private_instance_methods.count
 => 1610 


[ Comment below if you need further clarifications ]



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s