Practices: Object Oriented Development Jan
Join the DZone community and get the full member experience.Join For Free
i’m starting off this series talking about object oriented programming. it’s amazing how many people write c# or vb (.net) with long, procedural methods with really no understanding of the true fundamentals of oo design.
i know, i know – it’s not you, and know what? i’m not talking about you.. but check out the code from the person next to you. see what i mean?
just because you’re writing in an oo language, doesn’t mean that you’re writing oo code , or taking advantage of the object oriented benefits. an object oriented language is more than classes with methods, properties and events (even vb 6 had that!).
learn the fundamentals.
get to know interfaces , abstract classes , structs . i’m not picking on the thousands and thousands of extremely talented .net developers that came from vb6 (i’m one of them), but i am acknowledging the fact the there are oo tools and strategies at your disposal now that never were before. dig in to the delegates and the various eventing models, chained constructors, virtual and overridden methods. go beyond if statements, while loops and language syntax similarities and embrace your new polymorphic self.
perhaps, you’ve been a very successful developer for a very long time, i’m not saying that you have to change how you write all of your code. i am however, suggesting that you should realize that you have more tools in your toolbox than you might be aware of, and that if you learn how to use those tools, you will be more successful using the right tool for the right job.
go beyond the fundamentals. a friend of mine teaches object oriented analysis and design for smu, he once told me that if you have to use an if statement in your code then you’re not doing oo – it’s now procedural. i think he meant his statement in jest, (although, he actually doesn’t allow his students to use if statements past the third section in his course) but the point was well taken. beyond language semantics lie’s a whole world of patterns, approaches, and ways of doing oo that can make your life much, much simpler.
pragmatic – make new friends, but keep the old ones..
i’m not that much of an oo purist. i actually do feel that sometimes a well laid our “procedural” method can be easier to read, and communicate the intent of the code, but it’s a balancing act. every time you write an if/switch statement you fork the flow of your code and increase it’s complexity, reducing your ability to maintain and test it. an oo purest might create a polymorphic object at this point and hop over to it instead of writing that if statement. i’m not saying that one way is better than the other. like i said, writing readable/maintainable/testable code is a balancing act.
beware of being “too” oo . it’s easy to get too pure on your oo. i’ve worked on systems where i had to fight through 7 to 8 levels of inheritance debugging and fighting all the way to figure out what in the world the system was doing. i’ve seen developers take 30 lines of procedural code and convert them in to 60 or 70 unique classes spread out over 6 projects. you can have too much of a good thing!
is it ok to be “procedural” sometimes… can you be too oo?
the truth is, you want to use the right tool for the right job. the code on top is probably pretty readable and is fine… *right now* it won’t be too long and you’re going to want to start moving it closer to something like on the bottom. why? with every scenario, and rule that gets applied to shipping orders, this code becomes less readable and harder to test. it won’t be long until you’re going to want to start validating (unit testing) your various order processes independent of each other. you’re going to want to break apart the coordination of an order, determination (the factory) of an order, from how it is actually handled… as you do that, your code will start to become more solid … but we’ll save that for the next post.
interview question alert
some of the related questions that i often ask:
q: what’s the difference between an interface and an abstract class? when would you use one over the other? what implications does that have?
a: interfaces define the contract with zero implementation, abstract classes can have some implementation, but for the most part don’t. you can inherit from one base class, but implement as many interfaces as you’d like to. abstract classes can be versioned (if you’re going to strongly name them) but interfaces cannot be versioned. bonus answer: whenever you create an abstract class, you should always have an interface to go with it, then code your dependencies to the interface, not the abstract class.
q: “polymorphic” is a big oo word that developers like to throw around. give a practical example of when you’ve used polymorphic behaviors in your code.
a: anytime you use an abstract factory you’re implementing polymorphic principals. so the “processor.process()” method above would be an example of polymorphism, so would most interface based development.
i hope this post has inspired you at some level to keep honing your development skills. i know that it was heavier on the “why” and low on the “how” specifics… i think that’s part of the point. you’ve got to go tackle some of this yourself. so go on, get out there.
Published at DZone with permission of Caleb Jenkins, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.