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
3,337
questions
320
votes
4
answers
119k
views
Should package names be singular or plural?
Often, in libraries especially, packages contains classes that are organized around a single concept. Examples: xml, sql, user, config, db. I think we all feel pretty naturally that these packages ...
295
votes
9
answers
291k
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?
276
votes
17
answers
26k
views
Why have private fields, isn't protected enough?
Is the visibility private of class fields/properties/attributes useful? In OOP, sooner or later, you are going to make a subclass of a class and in that case, it is good to understand and be able to ...
275
votes
14
answers
121k
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 ...
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 ...
239
votes
15
answers
245k
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 ...
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 ...
168
votes
17
answers
287k
views
Is it better to return NULL or empty values from functions/methods where the return value is not present?
I am looking for a recommendation here. I am struggling with whether it is better to return NULL or an empty value from a method when the return value is not present or cannot be determined.
Take ...
163
votes
7
answers
111k
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 &...
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 ...
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 ...
150
votes
14
answers
118k
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 ...
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 ...
113
votes
14
answers
30k
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 ...
112
votes
13
answers
61k
views
Why is C not considered an 'object-oriented' language?
It seems that C has its own quasi-objects such as 'structs' that can be considered as objects (in the high-level way that we would normally think).
And also, C files themselves are basically separate ...
105
votes
18
answers
67k
views
Is it poor programming practice to pass parameters as Objects? [duplicate]
So, we've got a guy who likes to write methods that take Objects as parameters, so they can be 'very flexible.' Then, internally, he either does direct casting, reflection or method overloading to ...
105
votes
17
answers
20k
views
Is encapsulation still one of the elephants OOP stands on?
Encapsulation tells me to make all or almost all fields private and expose these by getters/setters. But now libraries such as Lombok appear which allow us to expose all private fields by one short ...
105
votes
8
answers
162k
views
Why have private static methods?
I just wanted to clear up a question I have. What is the point of having a private static method as opposed to a normal method with private visibility?
I would have thought an advantage to having a ...
104
votes
10
answers
15k
views
Zero behavior objects in OOP - my design dilemma
The basic idea behind OOP is that data and behavior (upon that data) are inseparable and they are coupled by the idea of an object of a class. Object have data and methods that work with that (and ...
103
votes
13
answers
24k
views
Should we eliminate local variables if we can?
For example, to keep a CPU on in Android, I can use code like this:
PowerManager powerManager = (PowerManager)getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(...
101
votes
22
answers
57k
views
Why is OOP difficult? [closed]
When I started using an object-oriented language (Java), I pretty much just went "Cool" and started coding. I've never really thought about it until only recently after having read lots of questions ...
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?
98
votes
4
answers
30k
views
Rich Domain Models -- how, exactly, does behavior fit in?
In the debate of Rich vs. Anemic domain models, the internet is full of philosophical advice but short on authoritative examples. The objective of this question is to find definitive guidelines and ...
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 ...
91
votes
10
answers
53k
views
How do you avoid getters and setters?
I'm having something of a hard time with designing classes in an oo way. I've read that objects expose their behavior, not their data; therefore, rather than using getter/setters to modify data, the ...
87
votes
4
answers
60k
views
Is it bad practice that a controller calls a repository instead of a service?
Is it bad practice that a controller calls a repository instead of a service?
To explain more:
I figure out that in good design controllers call services and services use repositories.
But sometimes ...
86
votes
22
answers
13k
views
Is OOP hard because it is not natural?
One can often hear that OOP naturally corresponds to the way people think about the world. But I would strongly disagree with this statement: We (or at least I) conceptualize the world in terms of ...
85
votes
14
answers
14k
views
Do objects in OOP have to represent an entity?
Does an object have to represent an entity?
By an entity I mean something like a Product, Motor, a ParkingLot etc, a physical, or even a clear-cut non-physical conceptual object -- something that ...
83
votes
12
answers
28k
views
What is the utility and advantage of getters & setters especially when they are merely used to read and assign values to properties of an object? [closed]
I’m still really new to learning to program. Just learning the syntax for a few programming languages at the moment.
The courses I viewed for C# and Java touched only very briefly on getters & ...
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 ...
81
votes
11
answers
19k
views
Why is base-for-all-objects discouraged in C++
Stroustrup says "Don’t immediately invent a unique base for all of your classes (an Object class). Typically, you can do better without it for many/most classes." (The C++ Programming Language Fourth ...
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 ...
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 ...
79
votes
10
answers
16k
views
How functional programming achieves "No runtime exceptions"
How does a functional programming language, such as Elm, achieve "No runtime exceptions"?
Coming from an OOP background, runtime exceptions have been part of whatever framework that is based ...
78
votes
10
answers
42k
views
What is a proper use of downcasting?
Downcasting means casting from a base class (or interface) to a subclass or leaf class.
An example of a downcast might be if you cast from System.Object to some other type.
Downcasting is unpopular, ...
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 ...
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 ...
73
votes
11
answers
14k
views
What benefit do we get by thinking of objects as "sending messages to each other"?
I have read that in OOP, we think of objects as "sending messages to each other", for example if we did car1.stop(), we say that "we sent the message stop() to the car1 object".
...
72
votes
14
answers
6k
views
Name for this antipattern? Fields as local variables [closed]
In some code I'm reviewing, I'm seeing stuff that's the moral equivalent of the following:
public class Foo
{
private Bar bar;
public MethodA()
{
bar = new Bar();
bar.A();...
66
votes
14
answers
37k
views
Isn't MVC anti OOP?
The main idea behind OOP is to unify data and behavior in a single entity - the object. In procedural programming there is data and separately algorithms modifying the data.
In the Model-View-...
66
votes
8
answers
13k
views
When to *not* use SOLID principles
Coming from a math background, counterexamples are equally, if not more, helpful to me for understanding concepts than examples. I've seen many, many examples of when and how to use the SOLID ...
66
votes
1
answer
12k
views
How are mixins or traits better than plain multiple inheritance?
C++ has plain multiple inheritance, many language designs forbid it as dangerous. But some languages like Ruby and PHP use strange syntax to do the same thing and call it mixins or traits. I heard ...
65
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 ...
65
votes
11
answers
64k
views
Why is it good to split a program into multiple classes? [closed]
I'm still a student in high school (entering 10th grade), and I have yet to take an actual computer course in school. Everything I've done so far is through books. Those books have taught me concepts ...
64
votes
12
answers
13k
views
Does this class design violate the single responsibility principle?
Today I had an argument with someone.
I was explaining the benefits of having a rich domain model as opposed to an anemic domain model. And I demoed my point with a simple class looking like that:
...
64
votes
10
answers
32k
views
Do you generally send objects or their member variables into functions?
Which is generally accepted practice between these two cases:
function insertIntoDatabase(Account account, Otherthing thing) {
database.insertMethod(account.getId(), thing.getId(), thing....
61
votes
8
answers
26k
views
LSP vs OCP / Liskov Substitution VS Open Close
I am trying to understand the SOLID principles of OOP and I've come to the conclusion that LSP and OCP have some similarities (if not to say more).
the open/closed principle states "software ...
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;
}...
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 ...
58
votes
19
answers
22k
views
Does OOP fulfill the promise of code reuse? What alternatives are there to achieve code reuse?
Perhaps the greatest promise of using object-oriented paradigm is the code reuse. Some dispute that this was achieved. Why was it (not) achieved?
Does code reuse as OOP defines it, make projects more ...