Skip to main content

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

Filter by
Sorted by
Tagged with
324 votes
4 answers

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 ...
Nicole's user avatar
  • 28.2k
302 votes
9 answers

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?
Vinoth Kumar C M's user avatar
276 votes
14 answers

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 ...
Slamice's user avatar
  • 2,657
276 votes
17 answers

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 ...
Adam Libuša's user avatar
  • 2,077
250 votes
23 answers

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 ...
241 votes
15 answers

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 ...
mwallace's user avatar
  • 2,464
202 votes
14 answers

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 ...
169 votes
18 answers

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 ...
P B's user avatar
  • 2,379
164 votes
7 answers

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 &...
user1620696's user avatar
  • 4,887
157 votes
15 answers

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

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 ...
Grim's user avatar
  • 903
150 votes
14 answers

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 ...
Spring's user avatar
  • 1,733
137 votes
4 answers

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 ...
Charlie Flowers's user avatar
115 votes
13 answers

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 ...
Dark Templar's user avatar
  • 6,323
113 votes
14 answers

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 ...
pup's user avatar
  • 1,712
109 votes
8 answers

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 ...
Rehan Naqvi's user avatar
  • 1,209
105 votes
10 answers

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 ...
RokL's user avatar
  • 2,431
105 votes
17 answers

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 ...
Gangnus's user avatar
  • 2,817
104 votes
18 answers

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 ...
103 votes
13 answers

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(...
ggrr's user avatar
  • 5,783
101 votes
22 answers

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
9 answers

Is it the correct practice to keep more than 10 years old spaghetti legacy code untouched without refactoring at all in big product development?

I have been in two software product houses for three years in a row. The first is a small company maintaining a fairly small management system with a monolithic legacy code base (almost twenty years). ...
Rui's user avatar
  • 1,865
99 votes
5 answers

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?
GSto's user avatar
  • 8,531
98 votes
4 answers

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 ...
RJB's user avatar
  • 2,110
96 votes
15 answers

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 ...
94 votes
4 answers

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 ...
mohsenJsh's user avatar
  • 1,315
91 votes
10 answers

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 ...
IntelliData's user avatar
86 votes
22 answers

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

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 ...
Dennis's user avatar
  • 8,247
84 votes
6 answers

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 ...
Anyname Donotcare's user avatar
83 votes
12 answers

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 & ...
ProjectDiversion's user avatar
82 votes
12 answers

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 ...
niko's user avatar
  • 2,119
81 votes
11 answers

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 ...
Matthew James Briggs's user avatar
80 votes
2 answers

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 ...
Spoike's user avatar
  • 14.8k
79 votes
10 answers

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 ...
Fireburn's user avatar
  • 889
78 votes
10 answers

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, ...
ChrisW's user avatar
  • 3,417
78 votes
14 answers

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 ...
dsimcha's user avatar
  • 17.2k
76 votes
11 answers

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 ...
Alexander's user avatar
  • 1,159
73 votes
11 answers

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". ...
Christopher's user avatar
  • 2,039
71 votes
14 answers

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();...
JSBձոգչ's user avatar
  • 1,440
71 votes
8 answers

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 ...
Derek Allums's user avatar
67 votes
14 answers

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-...
Random42's user avatar
  • 10.5k
66 votes
8 answers

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 ...
derphil's user avatar
  • 869
66 votes
11 answers

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 ...
kullalok's user avatar
  • 1,075
66 votes
1 answer

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 ...
Gherman's user avatar
  • 945
64 votes
12 answers

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: ...
tobiak777's user avatar
  • 797
64 votes
10 answers

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....
AJJ's user avatar
  • 2,998
62 votes
8 answers

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 ...
Kolyunya's user avatar
  • 965
60 votes
8 answers

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 ...
honestduane's user avatar
59 votes
9 answers

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(...
Pubby's user avatar
  • 3,390

2 3 4 5