Questions tagged [object-oriented-design]
Object-oriented design is the process of planning a system of interacting objects for the purpose of solving a software problem.
1,695
questions
245
votes
15
answers
165k
views
Is it wrong to use a boolean parameter to determine behavior?
I have seen a practice from time to time that "feels" wrong, but I can't quite articulate what is wrong about it. Or maybe it's just my prejudice. Here goes:
A developer defines a method with a ...
215
votes
16
answers
32k
views
Do we really need OO languages to manage software complexity?
This is going to be a very non-technical, soft question and I am not sure if this is the right platform. But I am a beginning CS student so I hope you guys tolerate it.
In the first semester we were ...
168
votes
17
answers
290k
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 ...
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 &...
152
votes
14
answers
22k
views
Did the Gang of Four thoroughly explore "Pattern Space"?
Ever since I first learned about the Gang of Four (GoF) design patterns, at least 10 years ago, I am having the impression that these 23 patterns should be only a small sample of something much larger ...
152
votes
12
answers
27k
views
Is this a violation of the Liskov Substitution Principle?
Say we have a list of Task entities, and a ProjectTask sub type. Tasks can be closed at any time, except ProjectTasks which cannot be closed once they have a status of Started. The UI should ensure ...
123
votes
12
answers
31k
views
Why would Square inheriting from Rectangle be problematic if we override the SetWidth and SetHeight methods?
If a Square is a type of Rectangle, than why can't a Square inherit from a Rectangle? Or why is it a bad design?
I have heard people say:
If you made Square derive from Rectangle, then a Square ...
110
votes
13
answers
21k
views
How do you justify more code being written by following clean code practices?
Moderator note
This question has already had seventeen answers posted to it. Before you post a new answer, please read the existing answers and make sure your viewpoint isn't already adequately ...
104
votes
9
answers
20k
views
Why should I use dependency injection?
I am having a hard time looking for resources on why I should use dependency injection. Most of the resources that I see explains that it just passes an instance of an object to another instance of an ...
99
votes
9
answers
19k
views
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). ...
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 ...
81
votes
11
answers
20k
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 ...
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 ...
74
votes
9
answers
27k
views
Are classes with only a single (public) method a problem?
I am currently working on a software project that performs compression and indexing on video surveillance footage. The compression works by splitting background and foreground objects, then saving the ...
73
votes
10
answers
8k
views
My proposed design is usually worse than my colleague's - how do I get better? [closed]
I have been programming for couple of years and am generally good when it comes to fixing problems and creating small-to-medium scripts, however, I'm generally not good at designing large scale ...
68
votes
8
answers
14k
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 ...
65
votes
12
answers
12k
views
Having a flag to indicate if we should throw errors
I recently started working at a place with some much older developers (around 50+ years old). They have worked on critical applications dealing with aviation where the system could not go down. As a ...
59
votes
5
answers
31k
views
Using public final rather than private getters
I see most immutable POJOs written like this:
public class MyObject {
private final String foo;
private final int bar;
public MyObject(String foo, int bar) {
this.foo = foo;
...
56
votes
11
answers
4k
views
What is meant by, "A user shouldn't decide whether it is an Admin or not. The Privileges or Security system should."
The example used in the question pass bare minimum data to a function touches on the best way to determine whether the user is an administrator or not. One common answer was:
user.isAdmin()
This ...
55
votes
7
answers
23k
views
Why is chaining setters unconventional?
Having chaining implemented on beans is very handy: no need for overloading constructors, mega constructors, factories, and gives you increased readability. I can't think of any downsides, unless you ...
53
votes
3
answers
72k
views
Where to put business logic in MVC design?
I have created a simple MVC Java application that adds records through data forms to a database.
My app collects data, it also validates it and stores it. This is because the data is being sourced ...
52
votes
3
answers
114k
views
How to solve circular dependency?
I have three classes that are circular dependant to each other:
TestExecuter execute requests of TestScenario and save a report file using ReportGenerator class.
So:
TestExecuter depends on ...
51
votes
4
answers
16k
views
Exception propagation: When should I catch exceptions?
MethodA calls an MethodB which in turn calls MethodC.
There is NO exception handling in MethodB or MethodC. But there is exception handling in MethodA.
In MethodC an exception occurs.
Now, that ...
50
votes
7
answers
10k
views
Why is inheritance bad in a Person-Student model?
I've just started learning about Inheritance vs Composition and it's kind of tricky for me to get my head around it for some reason.
I have these classes:
Person
class Person
{
public string Name {...
49
votes
3
answers
8k
views
Using a "Pass-through (God) Service" is bad, right? [duplicate]
My team has developed a new service layer in our application. They created a bunch of services that implement their interfaces (E.g., ICustomerService, IUserService, etc). That's pretty good so far.
...
45
votes
6
answers
14k
views
Passing functions into other functions as parameters, bad practice?
We've been in the process of changing how our AS3 application talks to our back end and we're in the process of implementing a REST system to replace our old one.
Sadly the developer who started the ...
43
votes
10
answers
6k
views
How do unit tests facilitate design?
Our colleague promotes writing unit tests as actually helping us to refine our design and refactor things, but I do not see how. If I am loading a CSV file and parse it, how is unit test (validating ...
43
votes
5
answers
13k
views
Why inherit a class without adding properties?
I found an inheritance tree in our (rather large) code base that goes something like this:
public class NamedEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
public ...
42
votes
8
answers
11k
views
Class that does not represent anything - is it correct?
I am just designing my application and I am not sure if I understand SOLID and OOP correctly. Classes should do 1 thing and do it well but from the other hand they should represent real objects we ...
42
votes
9
answers
4k
views
Programming for future use of interfaces
I have a colleague sitting next to me who designed an interface like this:
public interface IEventGetter {
public List<FooType> getFooList(String fooName, Date start, Date end)
...
42
votes
2
answers
57k
views
Why is inheritance generally viewed as a bad thing by OOP proponents [duplicate]
I keep hearing the phrase "Favour Composition over Inheritance" from GoF, which is being annoyingly mentioned repeatedly by my friend, who thinks it is a valid blanket statement. But is it ...
41
votes
6
answers
10k
views
Should I create a class if my function is complex and has a lot of variables?
This question is somewhat language-agnostic, but not completely, since Object Oriented Programming (OOP) is different in, for example, Java, which doesn't have first-class functions, than it is in ...
40
votes
6
answers
30k
views
Why avoid Java Inheritance "Extends"
Jame Gosling said
“You should avoid implementation inheritance whenever possible.”
and instead, use interface inheritance.
But why? How can we avoid inheriting the structure of an object using ...
39
votes
13
answers
13k
views
Is it wrong to use a boolean parameter to determine values?
According to Is it wrong to use a boolean parameter to determine behavior?, I know the importance of avoid using boolean parameters to determine a behaviour, eg:
original version
public void ...
38
votes
7
answers
15k
views
Should I still follow "programming to an interface not implementation" even if I think using concrete class members is the simpler solution?
According to Understanding "programming to an interface", as I understand, I think I should depend on abstract class only. However, in some case, for example, Student:
public class Student {...
38
votes
2
answers
44k
views
How to design a scalable notification system? [closed]
I need to write a notification system manager.
Here is my requirements:
I need to be able to send a Notification on different platforms, which may be totally different (for exemple, I need to be ...
36
votes
9
answers
9k
views
When writing object-oriented code, should I always be following a design pattern?
Is there a conceivable design pattern for any object-oriented program? I ask this because recently I saw an implementation of a Door class with a Lock. It was part of a test and the answer said that ...
36
votes
8
answers
3k
views
Is OOP becoming easier or harder? [closed]
When the concepts of Object Oriented Programming were introduced to programmers years back it looks interesting and programming was cleaner. OOP was like this
Stock stock = new Stock();
stock.addItem(...
34
votes
10
answers
25k
views
Interfaces on an abstract class
My coworker and I have different opinions on the relationship between base classes and interfaces. I'm of the belief that a class should not implement an interface unless that class can be used when ...
32
votes
6
answers
6k
views
How do I test a system where the objects are difficult to mock?
I am working with the following system:
Network Data Feed -> Third Party Nio Library -> My Objects via adapter pattern
We recently had an issue where I updated the version of the library I was ...
31
votes
3
answers
3k
views
Is a "start", "run" or "execute" method a good practice?
I'm currently working on a code base that has many classes that implement a Start method.
This seems like two-phase construction to me, which I had always considered a bad practice. I can't tell the ...
31
votes
17
answers
8k
views
What is the value in hiding the details through abstractions? Isn't there value in transparency?
Background
I am not a big fan of abstraction. I will admit that one can benefit from adaptability, portability and re-usability of interfaces etc. There is real benefit there, and I don't wish to ...
31
votes
6
answers
13k
views
Can you implement "object-oriented" programming without the class keyword?
Say we want to provide an abstraction of an "account" in a bank. Here's one approach, using a function object in Python:
def account():
"""Return a dispatch dictionary representing a bank account....
31
votes
10
answers
6k
views
Does logging inside a class violate the SRP?
I wrote a class that takes a Logger class as one of its arguments:
class QueryHandler:
def __init__(self, query: Query, logger: Logger) -> None:
self.query = query
self.logger =...
31
votes
9
answers
29k
views
Are init() methods a code smell?
Is there any purpose for declaring an init() method for a type?
I'm not asking whether we should prefer init() over a constructor or how to avoid declaring init().
I'm asking if there is any ...
30
votes
7
answers
13k
views
Is it a code smell to set a flag in a loop to use it later?
I have a piece of code where I iterate a map until a certain condition is true and then later on use that condition to do some more stuff.
Example:
Map<BigInteger, List<String>> map = ...
30
votes
5
answers
7k
views
Do unit tests sometimes break encapsulation? [duplicate]
I very often hear the following: "If you want to test private methods, you'd better put that in another class and expose it."
While sometimes that's the case and we have a hiding concept ...
30
votes
10
answers
5k
views
How would Functional Programming proponents answer this statement in Code Complete?
On page 839 of the second edition, Steve McConnell is discussing all the ways that programmers can "conquer complexity" in big programs. His tips culminate with this statement:
"Object-oriented ...
29
votes
6
answers
8k
views
Is there a design pattern to remove the need to check for flags?
I'm going to save some string payload in the database. I have two global configurations:
encryption
compression
These can be enabled or disabled using the configuration in a way that either only one ...
29
votes
6
answers
9k
views
Visitor Pattern: what's the point of the `accept` method?
I'm trying to fully understand the visitor pattern. What I've learnt so far (correct me if I'm wrong) is:
It's about adding operations to classes, without modifying the source code of those classes. ...