Human above all, with pathos, weaknesses and grumpy at times. Speak for myself; think out loud. Direct, seemingly hard faced. Urged to fix things. Am fortunate.
Apply Polymorphism Or You Will Drive Yourself Crazy
Don’t try to do to much. ([Class Relationships][(/2010/08/23/Class-Relationships-Don’t-lose-your-train-of-thoughts.html#main)]). Identify polymorphism in 3 steps. ([How to identify polymorphism][(/2010/08/15/Identify-polymorphism-Don’t-abuse-the-If.html#main)])?
find the common operation (e.g. #whatAreYou)
find how is every operation different and put it in a method in each own class (e.g. #isAString, #isAnInteger, “is nuts”)
introduce an interface for the above classes with that common method
That’s all really. Try it with a Calculator example now.
Using polymorphism will
eliminate the need for multiple if(s)
eliminate the check for null
eliminate the exceptional case (in this case to return null)
Plus on each Personality class you get
Disclaimer: This is actual (disguised) code as seen on a very popular API. There are even more problems to discuss about it but let’s just focus on one thing at a time.