Home > Coding > Clean Code: Inheritance & Polymorphism or.. How to remove all those ifs!

Clean Code: Inheritance & Polymorphism or.. How to remove all those ifs!


 

After a while coding i an application you have to step back and look at your code. And this is where it sometimes gets scary!

It usually gets scary because there are a lot of twist and turns in the code!

{You know the typical: “If a user is active do this but if he or she is blond do that …” etc}

This code will not improve by itself. And it will get scarier once you’ve put that 9th or 10th change request in it!

It turns out that the conditions you have in a body of code … the more chances are that something will not get caught (unexpected at the time of writing the code).

So I think that at one point you have to start using the real power of OO!,

Time to introduce Inheritance and polymorphism!

 

 

For the folks I am working with… look at how many times the code is referencing the role and privileges of a user!

Instead of having a generic “User” and checking 100’s of times elsewhere in the code  “if user is an employee…”, Perhaps it would be better to have “EmployeeUser”!

Advertisements
Categories: Coding
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: