A few weeks ago I received a message forwarded onto me from someone in the Triangle Area ColdFusion User Group about frameworks and object oriented programming. The person who forwarded the message on to me asked for my input, which I figured I would give here on the Alagad blog.
I am having some trouble at my new job explaining why Frameworks, MVC and OOP style application development in ColdFusion is preferable to development without them. People here are interested but don’t really understand why these are such a great thing.
The counter argument I am hearing are things like: OOP is a bunch of extra work and that these things will take the rapid out of our rapid application development. Frameworks make things too complicated and difficult to troubleshoot and hurt application performance in a major way. MVC doesn’t make sense in a web application.
I am asking for the ColdFusion community to help me clarify the benefits of OO (or tell me that OO is stupid and I should forget it).
Do any of you know of any good breeze/connect presentations or articles that could help me explain the costs and benefits of MVC-FW-OOP?
Thanks for your responses!
David, asks one very important question. Specifically, why is Object Oriented Programming a good thing?
He goes on to provide a few counter arguments (amongst many more that have gone unsaid). I’ll pick these two to highlight:
- It’s Harder
- It’s Complicated and Creates More Work
The question and counter arguments can not be answered categorically. This is because the exact properties that some people hate, other people love.
Two of the answers as to why I use it would, in direct contrast, be:
- It’s Easier
- It’s Less Complicated and Saves Me Work
“What?” you say. “How can this be true?!” I’m glad you asked.
Object Oriented Programming requires a shift in thought from procedural programming. Actually, that’s the only thing it requires (besides the ability to create classes in your programming language). The problem is that this change in thought process is hard.
Rather than focusing on the goals of a process, Object Oriented Programmers focus more on what a particular object, or component of the system, is responsible for. There are thousands of examples of this on the web, but I’ll endeavor to create my own here:
Let’s say we have a problem where a website user needs to be able to send an email which uses a recipient-specific template and is composed of relevant data.
The procedural mindset says we have a specific set of steps we need to proceed through to accomplish the goal once the user has clicked or otherwise initiated the process:
- Figure out who is sending the message
- Figure out who is getting the message
- Figure out what template to use
- Figure out what data to use
- Compose email message
- Send email message
Of course, each of these steps is really broken down into several smaller steps in code. Even with ColdFusion it’d be hard to make this example in 6 real lines of code.
Meanwhile, the object oriented mind will use techniques it has developed through time and experience to sift out various groups of related functionality not just from this particular goal, but from the entire project in general.
For example, we might see that sending an email is something we do in several places on the site. We might also note that we’re using the same templates and data in several places on the site.
If we’re doing the same thing over and over, why rewrite that logic each time? In ColdFusion we have several well known techniques available that enable us to reuse application logic. Off the top of my head we have cfincludes, custom tags, the ability to create function libraries, not to mention ColdFusion Components.
If I were not using CFCs, I might make a custom tag responsible for creating and sending a templated email email. Perhaps the programmer would pass a query to it and the email address for the recipient. I can then reuse that custom-tag anywhere on my site that I wish. The component becomes responsible for getting the job done while the programmer is not concerned with how it is done (or, should I say, the procedure followed to get the job done). We encapsulate a complex action into one custom tag.
The problem with custom tags is that we only have one entry point. We call the tag and provide arguments. We can’t easily change how the tag does its job without changing the tag itself.
Let it suffice to say that I, as an experienced object oriented programmer, understand that if I wrap my functionality up into a CFC that I can more easily modify how it functions while, at the same time, lowering the risk that if I make changes I will break code that uses this component. So long as my input and output remain the same I can change how code does its job to my hearts content.
But, beyond that, objects have many other interesting properties that can be pontificated about for many, many, hours. In the end, I believe that objects make my code simpler. Mainly because I can focus a given object on doing one thing and one thing well. It’s simpler to write one component than an entire system.
Once my CFC is written it is nice, clean, and concise to use, and could care less how it works.
<cfset TemplatedMessage.send(reportData, "firstname.lastname@example.org") />
What’s simpler than that?
Like object oriented programming, frameworks makes things simpler. Frankly, the problem of how to handle website requests and responses has been solved. It has been for years. Why reinvent the wheel for each project?
Beyond traditional web frameworks there are also a plethora of other frameworks designed to make your live easier: configuration frameworks, unit testing frameworks, data persistence frameworks, validation frameworks, and much, much, much, more. Again, these things are already done. They work. If I’m already working with CFCs it’s essentially child’s play to pick these things up and run with them too. This saves me work, it saves me time, and it saves me and my clients money.
To be honest, I’ve only scratched the surface of why I think Object Oriented Programming is easier than procedural programming. I could ramble on for quite some time. The fact of the matter is that it’s easy because I think like this every day. Meanwhile, for the people who argue against it, Object Oriented Programming is hard because they don’t think that way.
At the risk of perplexing people, I’ll say that I find procedural programming hard. I don’t like to do it. It’s too unstructured and hard to follow and debug. It’s hard to do any form of meaningful quality assurance. It’s hard to work with a team in on procedural programs. I just don’t see the advantage and don’t understand why people promote it so enthusiastically.