Questions tagged [object-oriented]
A methodology that enables a system to be modeled as a set of objects that can be controlled and manipulated in a modular manner
459
questions
80
votes
2
answers
13k
views
Are there any OO-principles that are practically applicable for Javascript?
Javascript is a prototype-based object oriented language but can become class-based in a variety of ways, either by:
Writing the functions to be used as classes by yourself
Use a nifty class system ...
202
votes
14
answers
140k
views
When are Getters and Setters Justified?
Getters and setters are often criticized as being not proper OO. On the other hand, most OO code I've seen has extensive getters and setters.
When are getters and setters justified? Do you try to ...
46
votes
8
answers
12k
views
What is the real responsibility of a class?
I keep wondering if it is legitimate to use verbs that are based on nouns in OOP.
I came across this brilliant article, though I still disagree with the point it makes.
To explain the problem a bit ...
24
votes
4
answers
12k
views
Make methods that do not depend on instance fields, static?
Recently I started programming in Groovy for a integration testing framework, for a Java project. I use Intellij IDEA with Groovy plug-in and I am surprised to see as a warning for all the methods ...
155
votes
15
answers
48k
views
Where does this concept of "favor composition over inheritance" come from?
In the last few months, the mantra "favor composition over inheritance" seems to have sprung up out of nowhere and become almost some sort of meme within the programming community. And every time I ...
40
votes
4
answers
12k
views
The principle of least knowledge
I understand the motive behind the principle of least knowledge, but I find some disadvantages if I try to apply it in my design.
One of the examples of this principle (actually how not to use it), ...
18
votes
4
answers
2k
views
Staying OO and Testable while working with a database
What are some OOP strategies for working with a database but keeping things unit testable? Say I have a User class and my production environment works against MySQL. I see a couple possible approaches,...
239
votes
15
answers
246k
views
Why do we need private variables?
Why do we need private variables in classes?
Every book on programming I've read says this is a private variable, this is how you define it but stops there.
The wording of these explanations always ...
54
votes
9
answers
20k
views
Explanation on how "Tell, Don't Ask" is considered good OO
This blogpost was posted on Hacker News with several upvotes. Coming from C++, most of these examples seem to go against what I've been taught.
Such as example #2:
Bad:
def check_for_overheating(...
77
votes
14
answers
10k
views
Clarify the Single Responsibility Principle
The Single Responsibility Principle states that a class should do one and only one thing. Some cases are pretty clear cut. Others, though, are difficult because what looks like "one thing" when ...
137
votes
4
answers
31k
views
So what *did* Alan Kay really mean by the term "object-oriented"?
Reportedly, Alan Kay is the inventor of the term "object oriented". And he is often quoted as having said that what we call OO today is not what he meant.
For example, I just found this on ...
59
votes
8
answers
15k
views
How do I prove or disprove "God objects" are wrong?
Problem Summary:
Long story short, I inherited a code base and a development team I am not allowed to replace and the use of God Objects is a big issue. Going forward, I want to have us re-factor ...
42
votes
6
answers
38k
views
Understanding "programming to an interface"
I have come across the term "programming to an interface instead of an implementation" a lot, and I think I kind of understand what it means. But I want to make sure I understand it's benefits and it'...
12
votes
6
answers
4k
views
What are the practical ways to implement the SRP?
Simply what are the practical techniques people use to check if a class violates the single responsibility principle?
I know that a class should have only one reason to change, but that sentence is ...
150
votes
14
answers
119k
views
What is the ideal length of a method for you? [closed]
In object-oriented programming, there is of course no exact rule on the maximum length of a method , but I still found these two quotes somewhat contradicting each other, so I would like to hear what ...
76
votes
11
answers
54k
views
Can't I just use all static methods?
What's the difference between the two UpdateSubject methods below? I felt using static methods is better if you just want to operate on the entities. In which situations should I go with non-static ...
66
votes
8
answers
29k
views
Is ORM an Anti-Pattern? [closed]
I had a very stimulating and interessting discussion with a colleague about ORM and its pros and cons. In my opinion, an ORM is useful only in the rarest cases. At least in my experience.
But I don't ...
58
votes
9
answers
24k
views
Code Smell: Inheritance Abuse [duplicate]
It's been generally accepted in the OO community that one should "favor composition over inheritance". On the other hand, inheritance does provide both polymorphism and a straightforward, terse way of ...
19
votes
3
answers
11k
views
How does strengthening of preconditions and weakening of postconditions violate Liskov substitution principle?
I read that Liskov's substitution principle is violated if :
Preconditions are strengthened, or
Postconditions are weakened
But I don't get fully yet how these two points would violate Liskov ...
276
votes
14
answers
122k
views
Should we avoid object creation in Java?
I was told by a colleague that in Java object creation is the most expensive operation you could perform. So I can only conclude to create as few objects as possible.
This seems somewhat to defeat ...
99
votes
5
answers
31k
views
Functional Programming vs. OOP [closed]
I've heard a lot of talk about using functional languages such as Haskell as of late. What are some of the big differences, pros and cons of functional programming vs. object-oriented programming?
82
votes
12
answers
53k
views
What's the benefit of object-oriented programming over procedural programming?
I'm trying to understand the difference between procedural languages like C and object-oriented languages like C++. I've never used C++, but I've been discussing with my friends on how to ...
59
votes
9
answers
47k
views
Should the methods of a class call its own getters and setters?
Where I work I see lots of classes that do things like this:
public class ClassThatCallsItsOwnGettersAndSetters {
private String field;
public String getField() {
return field;
}...
46
votes
3
answers
34k
views
Programming SOLID Principles
Over time I could understand two parts of SOLID – the “S” and “O”.
“O” – I learned Open Closed Principle with the help of Inheritance and Strategy Pattern.
“S” – I learned Single Responsibility ...
19
votes
6
answers
5k
views
Method chaining vs encapsulation
There is the classic OOP problem of method chaining vs "single-access-point" methods:
main.getA().getB().getC().transmogrify(x, y)
vs
main.getA().transmogrifyMyC(x, y)
The first seems to have the ...
18
votes
3
answers
3k
views
Is there a specific name for the "Square inherits from Rectangle" paradox?
A certain failure of OOP is shown with a class Square inheriting from Rectangle, where logically Square is a specialization of Rectangle and should therefore inherit from it, but everything falls ...
13
votes
1
answer
4k
views
Is wrapping a third party code the only solution to unit test its consumers?
I'm doing unit testing and in one of my classes I need to send a mail from one of the methods, so using constructor injection I inject an instance of Zend_Mail class which is in Zend framework.
Now ...
250
votes
23
answers
216k
views
If immutable objects are good, why do people keep creating mutable objects? [closed]
If immutable objects¹ are good, simple and offer benefits in concurrent programming why do programmers keep creating mutable objects²?
I have four years of experience in Java programming and as I see ...
164
votes
7
answers
113k
views
What really is the "business logic"?
I'm working with web development since 2009, when I started with PHP. When I moved to ASP.NET, I've heard a lot about DDD and OOAD where a lot of focus is given to this "business logic" and &...
80
votes
4
answers
23k
views
Why do many software developers violate the open/closed principle?
Why do many software developers violate the open/closed principle by modifying many things like renaming functions which will break the application after upgrading?
This question jumps to my head ...
53
votes
10
answers
12k
views
Should we avoid custom objects as parameters?
Suppose I have a custom object, Student:
public class Student{
public int _id;
public String name;
public int age;
public float score;
}
And a class, Window, that is used to show ...
48
votes
11
answers
11k
views
Are error variables an anti-pattern or good design?
In order to handle several possible errors that shouldn't halt execution, I have an error variable that clients can check and use to throw exceptions. Is this an Anti-Pattern? Is there a better way to ...
39
votes
4
answers
13k
views
What is the principle of least astonishment?
In programming what is called Principle of Least Astonishment? How is this concept related to designing good APIs? Is this something applicable to only object oriented programming or does it permeate ...
38
votes
3
answers
74k
views
When to use a Singleton and when to use a static class [duplicate]
I've searched about this here and on StackOverflow and found some differences between the two.
But I'm still not sure in what cases one would prefer a Singleton, and in what cases one would choose to ...
8
votes
6
answers
1k
views
Significant amount of the time, I can't think of a reason to have an object instead of a static class. Do objects have more benefits than I think? [closed]
I understand the concept of an object, and as a Java programmer I feel the OO paradigm comes rather naturally to me in practice.
However recently I found myself thinking:
Wait a second, what are ...
297
votes
9
answers
293k
views
Aggregation vs Composition
I understand what composition is in OOP, but I am not able to get a clear idea of what Aggregation is. Can someone explain?
96
votes
15
answers
53k
views
What makes C so popular in the age of OOP? [closed]
I code a lot in both C and C++, but did not expect C to be the second most popular language, slightly behind Java.
TIOBE Programming Community Index
I'm curious as to why, in this age of OOP, C is ...
56
votes
3
answers
70k
views
Which is a better practice - helper methods as instance or static?
This question is subjective but I was just curious how most programmers approach this. The sample below is in pseudo-C# but this should apply to Java, C++, and other OOP languages as well.
Anyway, ...
53
votes
20
answers
22k
views
Does Object Oriented Programming Really Model The Real World? [closed]
I've seen it commonly repeated the object-oriented programming is based on modeling the real world but is it?
It seems to me that is not true of anything outside of the business layer. My GUI classes/...
43
votes
3
answers
62k
views
What is message passing in OO?
I've been studying OO programming, primarily in C++, C# and Java. I thought I had a good grasp on it with my understanding of encapsulation, inheritance and polymorphism.
One frequently referenced ...
29
votes
9
answers
10k
views
What can go wrong if the Liskov substitution principle is violated?
I was following this highly voted question on possible violation of Liskov Substitution principle. I know what the Liskov Substitution principle is, but what is still not clear in my mind is what ...
21
votes
4
answers
4k
views
"Too object-oriented"
I come from a strong OO background, and I have recently started working in an organization which, although the code is written in Java, has a lot less emphasis on good OO design than what I am used to....
18
votes
4
answers
18k
views
Why define a Java object using interface (e.g. Map) rather than implementation (HashMap)
In most Java code, I see people declare Java objects like this:
Map<String, String> hashMap = new HashMap<>();
List<String> list = new ArrayList<>();
instead of:
HashMap<...
16
votes
6
answers
14k
views
Are Get-Set methods a violation of Encapsulation? [duplicate]
Possible Duplicate:
When are Getters and Setters Justified
In an Object oriented framework, one believes there must be strict encapsulation. Hence, internal variables are not to be exposed to ...
15
votes
1
answer
3k
views
How to verify the Liskov substitution principle in an inheritance hierarchy?
Inspired by this answer:
Liskov Substitution Principle
requires
that
Preconditions cannot be strengthened in a subtype.
Postconditions cannot be weakened in a subtype.
Invariants of ...
7
votes
4
answers
6k
views
When or why should one use getters/setters for class properties instead of simply making them public properties?
I program primarily in ColdFusion but this is a general OOP question.
Is there any benefit to using:
getProp() {
return prop;
}
setProp(val) {
prop = val;
}
As opposed to simply
obj = ...
5
votes
5
answers
7k
views
What are the advantages/disadvantages of using objects as parameters to other object methods?
I'm mostly thinking of PHP here, but if you want to refer to other languages that's fine.
An object has a method that requires data contained in another class of object.
Should I extract that data ...
0
votes
3
answers
445
views
Tell one, but ask the others?
Consider we have three classes which want to collaborate, then, where is the behaviour?
I guess it can only be in one of the three classes or in a fourth one acting than as a procuedural connector-...
150
votes
8
answers
24k
views
Are bad programming practices typical within the software industry? [closed]
I just started my first job as a software developer over a month ago. Everything I have learned about OOP, SOLID, DRY, YAGNI, design patterns, SRP, etc. can be thrown out the window.
They use C# .NET ...
113
votes
14
answers
31k
views
Should I avoid private methods if I perform TDD?
I'm just now learning TDD. It's my understanding that private methods are untestable and shouldn't be worried about because the public API will provide enough information for verifying an object's ...