<p>Since I started studying object-oriented programming, I frequently read articles/blogs saying  functions are better, or not all problems should be modeled as objects. From your personal programming adventures, when do you think a problem is better solved by OOP?</p>

[{'answer_id': 6705, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 3, 'selected': False, 'text': "<p>I think it fits best when you are modeling something cohesive with state and associated actions on those states.  I guess that's kind of vague, but I'm not sure there is a perfect answer here.</p>\n\n<p>The thing about OOP is that it lets you encapsulate and abstract data and information away, which is a real boon in building a large system.  You can do the same with other paradigms, but it seems OOP is especially helpful in this category.</p>\n\n<p>It also kind of depends on the language you are using.  If it is a language with rich OOP support, you should probably use that to your advantage.  If it doesn't, then you may need to find other mechanisms to help break up the problem into smaller, easily testable pieces.</p>\n"}, {'answer_id': 6745, 'author': 'mbillard', 'author_id': 810, 'author_profile': 'https://Stackoverflow.com/users/810', 'pm_score': 3, 'selected': False, 'text': "<p>I am sold to OOP.</p>\n\n<p>Anytime you can define a concept for a problem, it can probably be wrapped in an object.</p>\n\n<p>The problem with OOP is that some people overused it and made their code even more difficult to understand. If you are careful about what you put in objects and what you put in services (static classes) you will benefit from using objects.</p>\n\n<p>Just don't put something that doesn't belong to an object in the object because you need your object to do something new that you didn't think of initially, refactor and find the best way to add that functionality.</p>\n"}, {'answer_id': 6748, 'author': 'Xetius', 'author_id': 274, 'author_profile': 'https://Stackoverflow.com/users/274', 'pm_score': 6, 'selected': True, 'text': "<p>There is no hard and fast rule.  A problem is better solved with OOP when you are better at solving problems and thinking in an OO mentality.  Object Orientation is just another tool which has come along through trying to make computing a better tool for solving problems.  </p>\n\n<p>However, it can allow for better code reuse, and can also lead to neater code.  But quite often these highly praised qualities are, in-relity, of little real value.  Applying OO techniques to an existing functional application could really cause a lot of problems.  The skill lies in learning many different techniques and applying the most appropriate to the problem at hand.</p>\n\n<p>OO is often quoted as a Nirvana-like solution to the software development, however there are many times when it is not appropriate to be applied to the issue at hand.  It can, quite often, lead to over-engineering of a problem to reach the perfect solution, when often it is really not necessary.</p>\n\n<p>In essence, OOP is not really Object Oriented Programming, but mapping Object Oriented Thinking to a programming language capable of supporting OO Techniques.  OO techniques can be supported by languages which are not inherently OO, and there are techniques you can use within functional languages to take advantage of the benefits.</p>\n\n<p>As an example, I have been developing OO software for about 20 years now, so I tend to think in OO terms when solving problems, irrespective of the language I am writing in.  Currently I am implementing polymorphism using Perl 5.6, which does not natively support it.  I have chosen to do this as it will make maintenance and extension of the code a simple configuration task, rather than a development issue.</p>\n\n<p>Not sure if this is clear.  There are people who are hard in the OO court, and there are people who are hard in the Functional court.  And then there are people who have tried both and try to take the best from each.  Neither is perfect, but both have some very good traits that you can utilise no matter what the language.</p>\n\n<p>If you are trying to learn OOP, don't just concentrate on OOP, but try to utilise Object Oriented Analysis and general OO principles to the whole spectrum of the problem solution.</p>\n"}, {'answer_id': 6781, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 1, 'selected': False, 'text': '<p>Some places where OO isn\'t so good are where you\'re dealing with "Sets" of data like in SQL. OO tends to make set based operations more difficult because it isn\'t really designed to optimally take the intersection of two sets or the superset of two sets.</p>\n\n<p>Also, there are times when a functional approach would make more sense such as this example taken from <a href="http://msdn.microsoft.com/en-us/magazine/cc164244.aspx" rel="nofollow noreferrer">MSDN</a>:</p>\n\n<blockquote>\n  <p>Consider, for example, writing a program to convert an XML document into a different form of data. While it would certainly be possible to write a C# program that parsed through the XML document and applied a variety of if statements to determine what actions to take at different points in the document, an arguably superior approach is to write the transformation as an eXtensible Stylesheet Language Transformation (XSLT) program. Not surprisingly, XSLT has a large streak of functionalism inside of it</p>\n</blockquote>\n'}, {'answer_id': 6783, 'author': 'Chris Roberts', 'author_id': 475, 'author_profile': 'https://Stackoverflow.com/users/475', 'pm_score': 1, 'selected': False, 'text': "<p>I find it helps to think of a given problem in terms of 'things'.</p>\n\n<p>If the problem can be thought of as having one or more 'things', where each 'thing' has a number of attributes or pieces of information that refer to its state, and a number of operations that can be performed on it - then OOP is probably the way to go!</p>\n"}, {'answer_id': 101351, 'author': 'bruceatk', 'author_id': 791, 'author_profile': 'https://Stackoverflow.com/users/791', 'pm_score': 3, 'selected': False, 'text': '<p>I\'m an old timer, but have also programmed OOP for a long time. I am personally against using OOP just to use it.  I prefer objects to have specific reasons for existing, that they model something concrete, and that they make sense.</p>\n\n<p>The problem that I have with a lot of the newer developers is that they have no concept of the resources that they are consuming with the code that they create.  When dealing with a large amount of data and accessing databases the "perfect" object model may be the worst thing you can do for performance and resources.</p>\n\n<p>My bottom line is if it makes sense as an object then program it as an object, as long as you consider the performance/resource impact of the implementation of your object model.</p>\n'}, {'answer_id': 101397, 'author': 'RS Conley', 'author_id': 7890, 'author_profile': 'https://Stackoverflow.com/users/7890', 'pm_score': 1, 'selected': False, 'text': '<p>The key to learning Object Oriented Programming is learning about Design Pattern. By learning about design patterns you can see better when classes are needed and when they are not. Like anything else used in programming the use of classes and other features of OOP languages depends on your design and requirements. Like algorithms Design patterns are a higher level concept. </p>\n\n<p>A Design Pattern plays similar role to that of algorithms for traditional programming languages. A design pattern tells you how create and combine object to perform some useful task. Like the best algorithms the best design patterns are general enough to be application to a variety of common problems. </p>\n'}, {'answer_id': 198273, 'author': 'fluffels', 'author_id': 12828, 'author_profile': 'https://Stackoverflow.com/users/12828', 'pm_score': 1, 'selected': False, 'text': '<p>In my opinion it is more a question about you as a person. Certain people think better in functional terms and others prefer classes and objects. I would say that OOP is better suited when it matches your internal (subjective) mental model of the world.</p>\n'}, {'answer_id': 511838, 'author': 'alepuzio', 'author_id': 45745, 'author_profile': 'https://Stackoverflow.com/users/45745', 'pm_score': 0, 'selected': False, 'text': '<p>It depends by the problem: the OOP paradigm is useful in designing distribuited systems or framework with a lot of entity living during the actions of the user (example: web application).</p>\n\n<p>But if you have a math problem you will prefer a functional language (LISP); for a performance-critical systems you will use ADA or C, etc etc.</p>\n\n<p>The language OOP is useful because too it use probabily the garbage collector (automatic use of memory) in the run of program: you you program in C a lot of time you must debug and correct manually a problem of memory.</p>\n'}, {'answer_id': 798328, 'author': 'Rob Scott', 'author_id': 94707, 'author_profile': 'https://Stackoverflow.com/users/94707', 'pm_score': 1, 'selected': False, 'text': '<p>Object oriented code and procedural code have different extensibility points.  Object oriented solutions make it easier to add new classes without modifying existing functions (see the Open-Closed Principle), while procedural code allows you to add functions without modifying existing data structures.  Quite often different parts of a system require different approaches depending upon the type of change that is anticipated.</p>\n'}, {'answer_id': 4052449, 'author': 'stonemetal', 'author_id': 92601, 'author_profile': 'https://Stackoverflow.com/users/92601', 'pm_score': 0, 'selected': False, 'text': '<p>OOP is useful when you have things. A socket, a button, a file.  If you end a class in er it is almost always a function that is pretending to be a class. TestRunner more than likely should be a function that runs tests(and probably named run tests).</p>\n'}, {'answer_id': 4113319, 'author': 'Michael Langford', 'author_id': 340612, 'author_profile': 'https://Stackoverflow.com/users/340612', 'pm_score': 3, 'selected': False, 'text': '<p>There are 5 criteria whether you should favor Object Oriented over Object Based,Functional or Procedural code. Remember all of these styles are available in all languages, they\'re <em>styles</em>. All of these are written in a style of "Should I favor OO in this situation?"</p>\n\n<p>The system is very complex and has over approximately 9k LOC (Just an arbitrary level). -- As systems get more complex, the benefits gained by encapsulating complexity go up quite a bit. With OO, as opposed to the other techniques, you tend to encapsulate more and more of the complexity, which is very valuable at this level. Object Based or procedural should be favored before this. (This is not advocating a particular language mind you. <a href="http://www.planetpdf.com/codecuts/pdfs/ooc.pdf" rel="noreferrer">OO C</a> fits these features more than OO C++ in my mind, a language with a notorious reputation for leaky abstractions and an ability to eat shops with even 1 mediocre/obstinate programmer for lunch).</p>\n\n<p>Your code is not operations on data (i.e. Database based or math/analysis based). Database based code is often more easily represented via procedural style. Analysis based code is often easier represented in a functional style.</p>\n\n<p>Your model is a simulation of something (OO excels at simulations).</p>\n\n<p>You\'re doing something for which the object based subtype dispatch of OO is valuable (aka, you need to send a message to all objects of a certain type and various subtypes and get an appropriate, but different, reaction out of all of them).</p>\n\n<p>Your app is not multi-threaded, especially in a non-worker task method type of codebase. OO is quite problematic in programs which are multithreaded and require different threads to do different tasks. If your program is structured with one or two main threads and many worker threads doing the same thing, the muddled control flow of OO programs is easier to handle, as all of the worker threads will be isolated in what they touch and can be considered as a monolithic section of code. Consider any other paradigm actually. Functional excels at multithreading (lack of side effects is a huge boon), and object based programming can give you boons with some of the encapsulation of OO, however with more traceable procedural code in critical sections of your codebase. Procedural of course excels in this arena as well.</p>\n'}, {'answer_id': 4114028, 'author': 'rbrc', 'author_id': 481785, 'author_profile': 'https://Stackoverflow.com/users/481785', 'pm_score': 0, 'selected': False, 'text': "<p>Personally, I think OOP is practically a necessity for any large application.  I can't imagine having a program over 100k lines of code without using OOP, it would be a maintenance and design nightmare.</p>\n"}, {'answer_id': 4248852, 'author': 'etm1109', 'author_id': 516500, 'author_profile': 'https://Stackoverflow.com/users/516500', 'pm_score': -1, 'selected': False, 'text': "<p>I tell you when OOP is bad.  </p>\n\n<p>When the architect writes really complicated, non-documented OOP code.  Leaves half way through the project.  And many of his common code pieces he used across various project has missing code.  Thank god for .NET Reflector.</p>\n\n<p>And the organization was not running Visual Source Safe or Subversion.</p>\n\n<p>And I'm sorry.  2 pages of code to login is rather ridiculous even if it is cutely OOPed.... </p>\n"}, {'answer_id': 4341469, 'author': 'Ruan Mendes', 'author_id': 227299, 'author_profile': 'https://Stackoverflow.com/users/227299', 'pm_score': 1, 'selected': False, 'text': '<p>OO allows for logic related to an object to be placed within a single place (the class, or object) so that it can be decoupled and easier to debug and maintain. </p>\n\n<p>What I have observed, is that every app is a combination of OO and procedural code, where the procedural code is the glue that binds all your objects together (at the very least, the code in your main function).  The more you can turn your procedural code into OO, the easier it will be to maintain yor code.</p>\n'}, {'answer_id': 8324758, 'author': 'Ashok N', 'author_id': 1073169, 'author_profile': 'https://Stackoverflow.com/users/1073169', 'pm_score': 1, 'selected': False, 'text': '<p>Why OOP is used for programming:</p>\n\n<ol>\n<li>Its flexibility – OOP is really flexible in terms of use implementations.</li>\n<li>It can reduce your source codes by more than 99.9% – it may sound like I’m over exaggerating, but it is true.</li>\n<li>It’s much easier in implementing security – We all know that security is one of the vital requirements when it comes to web development.  Using OOP can ease the security implementations in your web projects.</li>\n<li>It makes the coding more organized – We all know that a Clean Program is a Clean Coding. Using OOP instead of procedural makes things more organized and systematized (obviously).</li>\n<li>It helps your team to work with each other easily – I know some of you had/have experienced team projects and some of you guys know that it’s important to have the same method, implementations, algorithm etc etc etc</li>\n</ol>\n'}]
['https://Stackoverflow.com/questions/6703', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/718/']