Hypocritical Methods

I haven’t posted for a while, but I’ve just got to post about this.

I commented on a question on stackoverflow.com (“How to find the source of a request”, http://stackoverflow.com/questions/6551954/how-to-find-the-source-of-a-request):

Why do you want to do this? It sounds like a bad idea. What if you add another assembly? What if you move functionality from one assembly to another? Perhaps you should instead add a parameter to this method, and set it differently between the calls.

This promoted Dan Abramov (http://stackoverflow.com/users/458193/dan-abramov) to give the best answer I’ve ever seen to this class of question (http://stackoverflow.com/questions/6551954/how-to-find-the-source-of-a-request/6552237#6552237).

The question as asked was a specific case of a very general situation I see fairly frequently. You’ve got a method or other piece of code and you want it to behave differently in different contexts. You decide you need to understand the context of the call in order to decide how to behave. You ask about looking at the stack trace, or the calling web page or something like that.

The answer, as Dan puts it so eloquently, is to pass a parameter instead (in the case of wanting a UI control or other object to behave differently, the answer may be to set a property instead).

This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s