When is an object-oriented approach inappropriate?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 8947 Views - Last Post: 02 October 2013 - 06:58 PM

#1 Elemental_1_1  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 114
  • Joined: 22-January 09

When is an object-oriented approach inappropriate?

Posted 20 August 2013 - 04:48 AM

Java has really been my door into programming; I learnt the basics of logic, functions (methods) and control statements through it and subsequently became comfortable in the language. As my experience grew, I began to branch out in order to understand the implications of what I was writing, rather than just mindlessly following tutorials or snippets. I learnt about what object oriented programming really was, and what it was good for (encapsulation - still a bit iffy on the significance of this, inheritance, interfacing, polymorphism - haven't really delved into this much yet). But now I am wanting to broaden my knowledge of languages, as I am beginning to realise that I will need more than just Java experience to get into the field due to the dominance of C++. My immediate thoughts are- "Okay, well C++ has object-oriented features, so I can just translate my ideas from Java to C++". The problem with this is that C++ has many non-object-oriented features (procedural/functional programming? I haven't researched much of the former). Due to my "upbringing" in Java, I basically think in objects when solving problems, and in C++ this may not be the most appropriate way to solve said problems.

So this brings me to my question: When is an object-oriented approach inappropriate?
And a supplementary question: How to I shift my mindset from object-oriented to "whatever is best for the task"?

TL;DR - Refer to above questions

Is This A Good Question/Topic? 0
  • +

Replies To: When is an object-oriented approach inappropriate?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,292
  • Joined: 12-June 08

Re: When is an object-oriented approach inappropriate?

Posted 20 August 2013 - 07:40 AM

Quote

So this brings me to my question: When is an object-oriented approach inappropriate?

When existing code is not OOP.. or when classes would be over kill (say basic data migration apps)... etc.


Quote

And a supplementary question: How to I shift my mindset from object-oriented to "whatever is best for the task"?

Do projects and when you think you are burning too much time making complex states for a simple CRUD or ninja app do a postmortem on the process and start collecting commonalities of when you should have seen "too complex for oop".

http://doc.cat-v.org...roperties_of_OO
Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5937
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: When is an object-oriented approach inappropriate?

Posted 20 August 2013 - 09:25 AM

"When is an object-oriented approach inappropriate?"

This really depends on what you consider "an object-oriented approach?" Program design is complex. It is very easy to over engineer in OO, but you can dial that down and still be doing good OO.

Some things, by their nature, are procedural. Most anything you'll find in an algorithms book, for instance. There is just some code that doesn't make sense in an instance. In any hierarchical design, you make judgement calls and some code often doesn't have a clearly appropriate home.


'How to I shift my mindset from object-oriented to "whatever is best for the task"?'

Different tools. OO is THE methodology for Java. Try Scala instead. Try C, C++, Python, C#, F#, Scheme, Erlang... Some languages will be slightly different than Java, other radically so.

C# would be the most gentle route. The language started out as a Java clone for .NET. Since that time, both languages have evolved. While Java has never wavered from it's pure OO vision, C# has managed embrace almost every programming paradigm since. Just using lambdas and type inference will free your mind and get you ready for other new adventures.

Then again, if you're stuck in Java think, try a Lisp. That should break your Java focus real good.
Was This Post Helpful? 1
  • +
  • -

#4 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 528
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: When is an object-oriented approach inappropriate?

Posted 19 September 2013 - 10:20 AM

Start designing. Is there a lot of the same thing with different variables like arrays? Then you should maybe make a class.

Does that class have a lot of different responses based on state? Maybe you need a new class, an interface, and a factory.

The question shouldn't be whether or not oo is right approach but ask is this the correct level of abstraction for my domain.
Was This Post Helpful? 1
  • +
  • -

#5 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 865
  • View blog
  • Posts: 2,657
  • Joined: 29-July 11

Re: When is an object-oriented approach inappropriate?

Posted 19 September 2013 - 02:38 PM

The object oriented approach is never inappropriate. OO design principles allow your code to be more modular, extensible, and reusable.
Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: When is an object-oriented approach inappropriate?

Posted 20 September 2013 - 06:57 AM

I think it all depends on the scale of your project.

If you are developing a very small program, then the OOP approach can be considered to be overkill. We see it quite a lot here. A small program to get some input from the user, do something with it and then output some kind of result. They often have it all shoved in main, which works fine but it's not OOP. We may suggest that they build some classes around it to separate responsibilities - one to get input and output from/to the user, one to do the calculations/logic or whatever. They could do that but it's pretty overkill for the small project they are working on. And in many cases you could relate overkill to being inappropriate.

But on any reasonably sized project I don't think the OO principles can every really be inappropriate. As farrell said they promote reuse and extensibility.

The trouble with OOP is that it can sometimes get too easy to overdo it. It's one thing having well defined classes/interfaces, but it's a another to have a class for absolutely everything, extract interfaces out of every feasible case and have inappropriate source files all over the place.

But I suppose that's not really OOP as a whole being inappropriate - that's the concepts of certain OOP principles being applied in an inappropriate manner.
Was This Post Helpful? 0
  • +
  • -

#7 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: When is an object-oriented approach inappropriate?

Posted 21 September 2013 - 03:19 PM

Although I agree with Ryano121 that OOP is overkill for small tasks like in-process-out, I feel that it's a perfect opportunity to practice OO structures on a small scale. That's especially important for Java students.

The only projects I'd advocate using only main is on the level of HelloWorld.java, otherwise you're only learning bad practice.
Was This Post Helpful? 0
  • +
  • -

#8 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 865
  • View blog
  • Posts: 2,657
  • Joined: 29-July 11

Re: When is an object-oriented approach inappropriate?

Posted 22 September 2013 - 11:54 PM

Everything in Java is OO. You cannot use Java without it.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: When is an object-oriented approach inappropriate?

Posted 23 September 2013 - 03:23 AM

Sometime I tend to put a wrapper around the basic type (integer, double, string) so that value has a meaning, that can be checked by the type-checker at compile time. It also make is harder to transpose the wrong values the parameter to a function or method is keyed to (a more) specific type, rather than just a double, eg (Litres)
Was This Post Helpful? 0
  • +
  • -

#10 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: When is an object-oriented approach inappropriate?

Posted 24 September 2013 - 09:15 AM

Quote

Everything in Java is OO. You cannot use Java without it.


But that doesn't mean the design has to be OO. We see non OO Java in the forums all the time. It's similar to how you can implement an OO design in C.
Was This Post Helpful? 2
  • +
  • -

#11 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 865
  • View blog
  • Posts: 2,657
  • Joined: 29-July 11

Re: When is an object-oriented approach inappropriate?

Posted 24 September 2013 - 05:03 PM

Nah. All design in Java is OO. There is no way to use Java without it. Even throwing your entire program into main is OO. You guys are confusing class design and creation with OO.

This post has been edited by farrell2k: 24 September 2013 - 05:03 PM

Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5937
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: When is an object-oriented approach inappropriate?

Posted 24 September 2013 - 06:59 PM

View Postfarrell2k, on 24 September 2013 - 08:03 PM, said:

Nah. All design in Java is OO. There is no way to use Java without it.


I do hope you're begin facetious... Java enforces a class based paradigm. The framework itself is mostly OO and Java strongly encourages OO.

However, no language can prevent the programmer from writing crap. And any language paradigm, no matter how strongly enforced, can be subverted by a programmer with enough will or ignorance to do so.

In Java, if it's all in main, it's not OO. Period. The procedural soup might take advantage of objects available in the environment, but that doesn't enforce OO design by proxy.
Was This Post Helpful? 3
  • +
  • -

#13 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8029
  • View blog
  • Posts: 13,741
  • Joined: 19-March 11

Re: When is an object-oriented approach inappropriate?

Posted 26 September 2013 - 07:07 AM

View Postfarrell2k, on 24 September 2013 - 07:03 PM, said:

Nah. All design in Java is OO. There is no way to use Java without it. Even throwing your entire program into main is OO. You guys are confusing class design and creation with OO.



I have to agree with baavgai and cfoley on this one. "Object oriented" is really about your approach to the programming, not just your choice of language. It is a fact that you can't write a Java program without writing a class, which implies that any Java program can be used as an object by someone else. To me, this doesn't imply tht the original program is written in an object-oriented style. It's also a fact that it would be difficult to write a significant java program without making use of some objects - for example, your program would not be able to communicate to the world unless you used some object somewhere. However, this also seems unconvincing to me, since the design paradigm of a program doesn't depend on the syntax of the expressions used to send data to stdout, but instead on the interactions of elements of the program.
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8029
  • View blog
  • Posts: 13,741
  • Joined: 19-March 11

Re: When is an object-oriented approach inappropriate?

Posted 26 September 2013 - 07:26 AM

View PostElemental_1_1, on 20 August 2013 - 06:48 AM, said:

So this brings me to my question: When is an object-oriented approach inappropriate?
And a supplementary question: How to I shift my mindset from object-oriented to "whatever is best for the task"?



If your project doesn't require composing new classes, you probably shouldn't compose new classes for it. However, this is not always as simple as that statement makes it seem.

As an example, take the Euler problems. Some of these take no input outside of a few defined constants, and can be easily solved with a straight-through composition of instructions.


Problem 7, for example:

Quote

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?


can be solved in a perfectly simple and hopefully obvious way without any new classes.

On the other hand, since there are many problems in that set which require generating primes, it would be a reasonable decision to write the prime generation code as a class, to facilitate reuse. Similarly, some problems require loading data from a file. If you're just solving the single problem, you might want to just write that in a procedural fashion. On the other hand, if you're solving seven such problems, you're probably going to regret that decision somewhere along the line. (If that regret doesn't start kicking in by the third time you write it, recalibrate your programming instincts)

Fortunately, if you have good programming habits it's usually a simple matter to rewrite a more procedural program into a more OO one. This is usually my approach in python: I generally start out sketching solutions to the immediate problem in the interpreter, and copying successful experiments over into a .py file. When I have a solution to a whole problem I sometimes recast it as a class, depending on how I'm going to use it. To continue with the above example, I would write the basic prime generation first, and then decide whether to embed it in a class.
Was This Post Helpful? 1
  • +
  • -

#15 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 865
  • View blog
  • Posts: 2,657
  • Joined: 29-July 11

Re: When is an object-oriented approach inappropriate?

Posted 02 October 2013 - 06:36 PM

View Postbaavgai, on 25 September 2013 - 01:59 AM, said:

In Java, if it's all in main, it's not OO. Period. The procedural soup might take advantage of objects available in the environment, but that doesn't enforce OO design by proxy.


Sure it is. The simple fact that you have to create or use an object to get anything done demonstrates that you cannot use java in a non-OO fashion. You guys are still confusing OO with OO design.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2