[Fake it ‘til you make it] SOLID OOP – S stands for “Not my fucking job”

You’re knee-deep into your interview now, with your interviewer absolutely wowed by your grasp of the Liskov Substitution Principle. You allow yourself to breathe and think “I’ve got it, this job’s mine”. Your interviewer senses this moment of weakness, and hoping to catch you off-guard she asks, “How about the other SOLID principles?” What do you do? You tell her “S stands for ‘Not my fucking job!'”

Like we did with LSP, we can introduce a rant into this discussion. This again shows your interviewer that you’ve thought a little deeper on the subject, and not just ate the bullshit from whatever training you claimed to have attended.

You: S actually stands for SRP, which means Single Responsibility Principle. It’s better than LSP, as far as mnemonics go. Its textbook definition sucks balls, though. “A class should have only one reason to change.” Wow. How ambiguous. How do you even interpret that? Oh, I have a class “Person” with first name and last name. I need to change it because it doesn’t have a middle name – reason one. Oh shit, I need to change it again because it doesn’t have an age – reason two. Does that mean I fucked up SRP?

Interviewer: So where does “not my fucking job” come in?

I personally really dislike that textbook definition. I even asked the instructor the very same question I just handed you. I don’t recall the exact wording of his answer, but it was akin to the way Donald Trump answers a lot of questions – he said a lot of words without really saying anything.

Anderson Cooper: How do you get Mexico to pay [for the wall]?

Donald Trump: Because they are ripping us left and right (sic). By the way, I love the Mexican people. Many Mexican people work for me, many Mexico (sic) people I do business with. They purchase things from me like apartments etc., etc.,

For goodness’ sake, this is the first principle in SOLID. If you’re going to be teaching this shit, you better be prepared to answer that question. If I were to teach this topic, I would like to start by saying the obvious – every class should have only one job, one responsibility – and so I will.

Every class should have only one job, one responsibility.

If a class needs to be modified, it should be because something about that job has changed. Going back to our example with the “Person” class, we can say that its job is to hold and provide information about a person. At the start of our development, we only needed the first and last name of any given person. Later on, we found out that some government forms require a middle name, and so we have to add it to the class. Further down the road we found out that a person’s age is also relevant to our business, and so we add it again.

We had to change it two times, each with two different triggers. The “reason” for doing the change is the same for both times though. Can you guess what that reason is? In both instances, the “Person” class is no longer able to adequately perform its job to hold and provide the necessary information that our business requires. This demonstrates how there’s only one reason to change, and that reason is the job or the responsibility of the class.

What would you do if your boss suddenly tells you that you need to be able to print a hard copy of the information inside each “Person”? As a developer, you should create another class whose job is to print the details about a person, and leave the “Person” class alone.

Back to your interview:

You: Imagine for a bit that we’re classes in a piece of software. You’re an interviewer, right? Your job is to interview an applicant, and say if he passes or fails. Now here comes your boss. He tells you – hey, can you give me a pie chart of the people you interviewed split by gender? Would you do it?

Interviewer: Simple enough to do, so yeah, probably.

You: Well, if we’re actually following SRP, you should give your boss a very respectful “No, sir. It’s not my fucking job.” And then you tell him to hire someone else to do his pie chart.

Interviewer: But why?

And that’s the nub, isn’t it? Why do you need to create a new class when it may be simple enough to add the functionality to your existing class? I am too lazy to write down an entire treatise on this right now, so I’ll just leave you with this reply:

You: Hmmm… asking that question is just like asking “why should we do object-oriented programming”? If you’re not willing to do it, you might as well just do imperative programming. If you want your code base to be a complete clusterfuck, go ahead. If you want to take advantage of cool stuff like the Strategy design pattern, grow some balls and tell your boss it’s not your fucking job.

Interviewer: Strategy design pattern? What’s that?

You: Hire me if you want to find out.

Boom! Mic drop!

If you’re still having trouble grasping or remembering SRP, just remember this more politically correct version of an old but gold nugget of wisdom:

tumblr_oaz07gwyiy1vzbr0vo1_1280

 

Tired of learning? Why not take a break and read about bachelor parties instead?

Advertisements

2 thoughts on “[Fake it ‘til you make it] SOLID OOP – S stands for “Not my fucking job”

  1. Pingback: [FITYMI] A Dip in the Dependency Inversion Principle – SOLID OOP | Error in Compilation

  2. Pingback: [FITYMI] Open and Closed Principle – Okay, now I have to Refactor | Error in Compilation

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