11

I work at a web development shop so naturally we deal with user profiles. When dealing with one of our sites I noticed that there was no 'User' class, which struck me as odd since we certainly have users. Instead the site relies on interacting with DataRows (this is C#) returned through static methods with little to no instantiation. I asked my boss about creating a class for users and his response was that since the objects have to be rebuilt so much its often not worth it.

I am relatively new to web development and it does seem like a bit of a waste to have to instantiate objects each time the page is rebuilt but on the other hand I've always found object oriented programming to be useful. So I'm curious for some opinions, how much do you guys use OOP in web development?

3
  • 2
    All the time. Without it, web programming gets increasingly harder to maintain and uses a lot of duplication. Dec 29, 2009 at 19:47
  • 2
    If you only want opinions. this should be community wiki.
    – anon
    Dec 29, 2009 at 19:54
  • Better question for Programmers?
    – user456814
    Jul 18, 2013 at 2:57

6 Answers 6

14

The only time I don't use OOP is when:

  1. I'm creating a simple project to test some logic. This usually leads to creating the right classes...

  2. I'm using Classic ASP (been awhile, thank god).

  3. I'm not programming.

edit 3+ years after posting the above; I'm appending a bit to my answer.

OOP is great and allows us a tremendous amount of flexibility for having multiple systems interacting with the same data / logic. However, there is certainly a situation in which you wouldn't want to bother loading up a lot of objects.. Namely, when you are simply pulling data for tabular display.

Querying a database and getting a simple record set back that is immediately emitted to the browser usually doesn't need OOP involved. As a matter of fact you might want to sidestep OOP completely as tabular data usually involves a roll up of other information (sums of child records) and you normally don't want to pull more data from the database than what you are actually using. ie. if you are only showing the name and email you probably don't want to grab the user name as that is just wasted cycles.

Now, putting information into a DB usually involves making sure that certain business logic is followed. For example that the username follows certain rules. In those situations leveraging an OOP style keeps things a bit more encapsulated and easily transferred between systems.

So, looking at the specific example: I wouldn't bother with more than handing a datatable to a repeater when pulling data; but I would have a user class for when I'm going to create a new one or operate on that user to make sure the business rules are properly followed.

1
  • +1 for the third bullet point :D
    – Paolo
    Dec 29, 2009 at 22:05
8

One question: does the data need to be coupled with function/method calls? If not, OOP is not necessary.

Your best approach might be to find an empty whiteboard, create a high level model using Object Oriented Design, then with Functional Design, then with Procedural. You might surprise yourself (and others) with the results. The same language can be used in vastly different ways depending on the project. As mentioned by @wj. OOP is just a paradigm, don't be afraid to step outside of your comfort zone and design using a different paradigm.

Taking time to design using different paradigms will also help you when you approach your boss to discuss why you should or should not use the current paradigm. Most bosses will appreciate that you spent the time to research before approaching them with an idea -- this isn't to say they'll accept your idea, but being knowledgable going in will potentially get you a few extra minutes of his/her attention.

IMHO (don't take this personally), "Object Oriented Programming" has fallen with the likes of "Web 2.0" -- a buzzword of sorts, which is unfortunate; you now see developers forcing OOP where it would be better suited to use FP or PP.

The best professional advice I can give is to design (high level at first, then dive down) in multiple paradigms (do your best not to be biased -- keep an open mind) and decide which one best addresses the way your application works. In my 15 years experience, 75+% of the time I find OOP to be unnecessary, although my current project is strictly OOP.

A more important/relevant question would be, "Does Object Oriented Design have a place in my current web development?"

3

Although objects make it easier for some programmers to develop, I've read the perfect example of how to build an entire website without OOP. Not once ounce. Check out the last page in a 20 page series entitled Clean PHP:

http://okmaya.com/clean-php/clean-php-step-20/

Super easy to follow, clean way of building an entire website. No confusing OOP, no super nested folder, no crazy spaghetti code to follow for hours... Just simple, clean, and well laid out functions that do EVERYTHING you need, without the use of OOP. And this example has everything from login/registration credentials, an admin section (CMS), even database fixtures to get you started, a search function that uses mapquest API to do zip code / lat-long lookups... I mean it has EVERYTHING for a core project, or website.

Why bother with OOP? Clean, and properly structured procedural code is great!

On the topic of OOP. I remember another fad that everyone thought it was cool, and everyone did it, but then found out that smoking gave you a whole bunch of problems.

Stick to the simple, stick to what you know. Be an expert in PHP and you never have to depend on a framework again. Don't get me started with OOP MVC Frameworks. Interpreted languages for the web were never meant to be OOP. OOP just adds another layer of complexity. Stop being lazy. Use your PHP, and learn how to freakin program!

On the other hand, I can see how making games on a console can be difficult without OOP. But then again, it's apples an oranges. Console games keep their objects in memory until the game exits, or object is destroyed from within game. Think about it... Why do they have a loading bar in front of every level? Now, imagine a web page that has to show you a loading bar every time it loads because it has to create objects from a database. SLLOOOOOOWWWWW central! And once you navigate away from this page, you have to start all over again.

Web pages are applications within themselves. It's like rebuilding your drag racer each time you go to the starting line, only to take it apart at the finish line. WTFridge? Seriously? Hey, super geniuses who think OOP is sooo cool... Keep your damn OOP out of my websites!

Just saying, this is from my 10+ years experience with web development, you know when we used to code pages in HTML, one by one?

1
  • You are correct that there is absolutely a case for ignoring OOP altogether for web sites. Especially when utilizing a language that is based on a functional style of coding. For most sites this is the simplest and best approach. However, more and more sites are no longer coded in a vacuum. Meaning that the site is only one interaction with the core logic or data. There may be services (windows or cron), mobile apps, desktop apps, etc that all share commonality. In these cases leveraging OOP procedures will lead to a more maintainable system. So +1 for finding a reason to keep OOP out.
    – NotMe
    May 22, 2013 at 17:10
3

OOP is nothing more than a programming paradigm !! but his importance is that hi is THE actual paradigm in use implying that all modern knowledge and best practices in software engineering will be expressed following this style of programming ...

A good example in your case (web development) is the Core J2EE Patterns.

alt text
(source: sun.com)

2
  • Woah, imagine that system without OOP... Dec 29, 2009 at 21:27
  • 1
    There would be data sets (arrays) and functions operating on them. The system would probably be much simpler as some of the objects would become a single function.
    – Calmarius
    Jun 24, 2012 at 22:45
1

Of course it does. You (and more so your boss) say "rebuilding" like it's a huge chore.

What you mean by "rebuilding" is running the program. Tell your boss that OOP in general is stupid because even in a desktop environment every time somebody runs a piece of software the objects need to be rebuilt so it's not even worth it.

0

Boss's comment is useless. The .net framework consists of objects and nothing else. A "response" is an object, even in "classic ASP" - why would people have implemented it if that were resource ineffective?

Not the answer you're looking for? Browse other questions tagged or ask your own question.