The SOLID Principles in Real Life
You're likely familiar with the SOLID principles. Here's a breakdown of the SOLID principles in a real-world setting.
Join the DZone community and get the full member experience.Join For Free
hopefully, you're familiar with the solid principles , particularly if you program in object oriented languages. the wisdom contained therein (mostly) isn't limited to object oriented languages, but such languages were the intended target.
if you're not familiar and don't have time to read the linked wikipedia page, solid is a mnemonic acronym for five principles of object oriented programming or, as i hinted, really just programming in general (except, perhaps for the liskov substitution principle). these concepts have been around since at least the early 2000s and have truly stood the test of time.
what you get by following them is code that's a lot more likely to be maintainable. these design guidelines, properly followed, will tend to steer you toward writing clean code.
what i'd like to do is offer real life analogs of the principles. i'd imagine that this may make them easier to remember, but i think it can also serve to drive the points home in the first place and help encourage the "aha" moment if you haven't yet had them. and, even if you have, it never hurts to have a visual to help reinforce the concept or to explain it someone else -- even someone non-technical, potentially.
s is for single responsibility principle
the single responsibility principle (srp) asserts that a class or module should do one thing only. now, this is kind of subjective, so the principle is reinforced with the heuristic that the class or module should have only one reason to change.
by way of counter-example, consider a class that opens a connection to the database, pulls out some table data, and writes the data to a file. this class has multiple reasons to change: adoption of a new database, modified file output format, deciding to use an orm, etc. in terms of the srp, we'd say that this class is doing too much.
in your day to day life, picture those "duck" vehicles you see occasionally in some lakeside towns. they're street legal and water-capable, so a duck tour affords you the unique and surreal experience of being in a 'car' that gets to the edge of the water and just keeps going. fun, right?
and yet, you don't see a whole lot of them. there are millions of families out there that own both cars and boats, and there are very few families that buy these ducks. do you know why? it's most likely because no one wants to be unable to drive to work because their boat rudder is broken. ducks are fun, but they're also a great example of the pitfalls that the srp can help you avoid.
o is for open/closed principle
the open/closed principle states that code entities should be open for extension, but closed for modification. to put this more concretely, you should write a class that does what it needs to flawlessly and not assuming that people should come in and change it later. it's closed for modification, but it can be extended by, for instance, inheriting from it and overriding or extending certain behaviors. an example of running afoul of the open-closed principle would be to have a switch statement somewhere that you needed to go in and add to every time you wanted to add a menu option to your application.
a great example of this in real life is sitting in your pocket in the form of a smartphone. all such phones have app stores and these app stores let you extend the base functionality of the phone. sure, it ships with the basics: camera operation, actual calls, text messages, etc. but via the app store, you can extend the phone's capabilities to allow you to manage your todo list, play inane video games, and even serve as a flashlight or wireless access point.
the mechanism that allows you to do this is purely one of extension, however. it's not as though apple, google, and microsoft put the os source code up on github and invite you to dive in and start building games and flashlight functionality. rather, they make the core phone functionality closed for modification and they open it to an extension .
l is for liskov substitution principle
the liskov substitution principle (lsp) is the one here that is most unique to object-oriented programming. the lsp says, basically, that any child type of a parent type should be able to stand in for that parent without things blowing up.
in other words, if you have a class, animal, with a makenoise() method, then any subclass of animal should reasonably implement makenoise(). cats should meow, dogs should bark, etc. what you wouldn't do is define a mutemouse class that throws idontactuallymakenoiseexception. this violates the lsp, and the argument would be that this class has no business inheriting from animal.
to picture this, imagine cooking yourself a stew. if you're anything like me, you'd only put things in there that were edible because you would want to eat the stew without picking through each bite, asking yourself repeatedly, "is this edible?"
i is for interface segregation principle
the interface segregation principle (isp) says that you should favor many, smaller, client-specific interfaces over one larger, more monolithic interface. in short, you don't want to force clients to depend on things they don't actually need. imagine your code consuming some big, fat interface and having to re-compile/deploy with annoying frequency because some method you don't even care about got a new signature.
to picture this in the real world, think of going down to your local corner restaurant and checking out the menu. you'll see all of the normal menu mainstays, and then something that's just called "soup of the day." why do they do this? because the soup changes a lot and there's no sense reprinting the menus every day. clients that don't care about the soup needn't even be concerned, and clients that do use a different interface -- asking the server.
d is for dependency inversion
the dependency inversion principle (dip) encourages you to write code that depends upon abstractions rather than upon concrete details. you can recognize this in the code you read by looking for a class or method that takes something generic like "stream" and performs operations on it, as opposed to instantiating a specific filestream or stringstream or whatever. this gives the code in question a lot more flexibility -- you can swap in anything that conforms to the stream abstraction and it will still work.
to visualize this in your day to day, go down to your local store and pay for something with a credit card. the clerk doesn't examine your card and get out the "visa machine" after seeing that your card is a visa. he just takes your card, whatever it is, and swipes it. both you and the clerk depend on the credit card abstraction without worrying about specifics.
and, that's solid!
hopefully, these visualizations help you. if you're always keeping solid in the back of your mind while writing code, you're going to make whoever maintains that code a lot happier. and, if you have an easy way to picture and remember the principles, you're a lot more likely to keep them in mind.
Published at DZone with permission of , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.