Skip to main content
The 2024 Developer Survey results are live! See the results

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
7 votes
8 answers
4k views

Is "avoid extra null pointer risk" a reason to avoid "introduce parameter objects"?

According to Should we avoid custom objects as parameters?, I know I can group related parameters to improve readability of the function, eg: Original version: public void showSchedule(long startDate,...
1 vote
1 answer
258 views

Mapping complex objects to other similar complex objects

I am working on two applications that serve the same purpose. The first application is more feature rich and its types are more complex, but uses old technologies and will be retired. It will ...
3 votes
3 answers
2k views

optional data in the constructor, bad practice?

Currently I have a model class that represents a user. This class has a constructor that takes an object with all user properties, used for example, when creating the user. In this case I instantiate ...
7 votes
6 answers
516 views

Should a Car object be responsible for checking if it is able to run on certain Road objects?

I have 2 kinds of objects in my system, Car and Road. A Car can be of various types and have properties like tire, tire size, engine, etc. A Road can be of multiple types and have properties like ...
5 votes
4 answers
2k views

If class B extends A, can we say that B depends on A?

Let's say we have 2 (Java) classes: class A {} class B extends A {} To me, saying B extends A and B is dependent on A are both true (but not equivalent) in this situation. My colleague, OTOH, says ...
0 votes
1 answer
195 views

Using the objects retrieved at the time of request validation

I hope the question title already answers the subject here. Lets say I have a number of ids that needs to be validated in a request. Example: request uri: [POST] /album/{album_id}/song Request ...
16 votes
7 answers
43k views

What is the better way to escape from too many if/else-if from the following code snippet?

I am trying to write a servlet which does task based on the "action" value passed it to as input. Here is the sample of which public class SampleClass extends HttpServlet { public static void ...
1 vote
1 answer
826 views

Can a class factory also save the object to a database?

How can a factory also save the object in the database? I want to create an object that consists of other objects. Pseudocode: firstObject = db.get....; secondObject = db.get....; expectedObject = ...
9 votes
4 answers
10k views

What is the most-widely accepted naming convention for date properties? [closed]

What is a widely accepted naming convention for date variables or properties in an Object-based, strongly typed language like C# (and by extension, for date database columns)? Do you use the word &...
4 votes
1 answer
5k views

Where should variables be declared

Considering I have a for loop in a method of a class. Should the incremented variable be declared as member of the class, or should it be declared in the method it uses it(or even in the for loop, ...
62 votes
8 answers
28k 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 ...
10 votes
2 answers
12k views

Static factory method in base class

An increasingly popular definition of factory method is: a static method of a class that returns an object of that class' type. But unlike a constructor, the actual object it returns might be an ...
7 votes
2 answers
397 views

Difficulty making this class open-closed

Here is my problem: I want to read input from different HID devices such as a gamepad, racing well, joystick, etc. Pretty much any game controller. The issue is that they all have different inputs. ...
6 votes
4 answers
1k views

How to avoid init methods when 2 objects need the reference of each other?

According to https://softwareengineering.stackexchange.com/a/334994/432039, I know init is a code smell and should be avoided, and one of the solutions is to use a builder to hold the state first ...
39 votes
7 answers
9k views

Clarify the Open/Closed Principle

As I have had it explained, the open/closed principle states that once written code should not be modified (aside from bug fixes). But if my business rules change shouldn't I modify the code ...
4 votes
6 answers
2k views

The ID of an object is null at start. Does that make the object state invalid and violate encapsulation?

This question is related to How should an `Employee` class be designed? In the above question, to uniquely identify an employee, each Employee object has an id field as shown below class Employee { ...
2 votes
2 answers
3k views

Module with globals or Class with attributes?

Currently I'm working with a lot of modules where the original developers used global variables to control states and to exchange important information between functions, like so: STATE_VAR = 0 def ...
3 votes
3 answers
237 views

Handle hierarchical relationships between large number of enums

I am working on a C# project and I have a somewhat large number of labels (~100) that have some sort of relationships between one another. Here is a minimal dummy example that illustrates this: ...
-1 votes
1 answer
3k views

If I have 2 different implementations of the same interface but with the same methods should I have 2 interfaces? [closed]

I have 2 different implementations of the same interface, but each one of those objects have different implementations, however they have the same methods, should I create one interface for each type ...
25 votes
15 answers
18k views

How far can you push Object Oriented Programming?

A getter is a failure to design an object. It violates encapsulation which is a core principle of object oriented programing. Now please tell me, how do you design a libraries hash table collection ...
-2 votes
1 answer
115 views

Defining functions inside vs outside a class

Say I have a class with a function do_thing that is comprised of multiple steps, which themselves segregate into functions (first_process and second_process). At what point would this be considered ...
2 votes
6 answers
337 views

Do private members/methods inherently violate the open-closed principle?

Doesnt private members and methods inherently violate the open-closed principle? Given that private, protected and public modifiers are supported. I came across this many times in codebases where ...
2 votes
1 answer
126 views

Refactoring Processor classes

I am writing some python 3 bioinformatics software and was wondering about the best way to write it in an OOP format. I am pretty sure a lot of my classes are violating the SRP principle, but I'm not ...
6 votes
2 answers
3k views

Trouble with circular dependency in state machine design

I am trying to develop the structure for a basic state machine that can also take in input and produce output. I've hit a bit of a mental block in trying to figure out how to model the relationship ...
15 votes
9 answers
5k views

In OOP, what counts as a "getter"

Note: I'm not looking for opinions on whether the authors of the article below are right or wrong. Mainly I'm looking for the exact definition of what they mean by getters, especially since I know ...
24 votes
5 answers
11k views

Is method overriding always a violation of Liskov Substitution Principle?

Overriding a method originally defined in the super class, by definition means this method will do different things when invoked on an object of the base class or an object of the subclass. So does ...
8 votes
7 answers
1k views

Is "avoid feature envy" violating "open closed principle"?

After reading What is a" feature envy" code and why is it considered a code smell?" I know the following code is suffering from "feature envy": public class Garden{ ...
0 votes
2 answers
121 views

What is a good architecture / design pattern for giving multiple shared attributes in different combinations?

I have a need for many different objects to have various combinations of attributes. For a demonstrative example, a flaming dog would have a dog attribute, a flame attribute, and a tail attribute, ...
13 votes
6 answers
10k views

Why use a special "Name" class (instead of just a string) for representing object names in C++?

Suppose we have an Instance class in a C++ program, which has a GUID/UUID, name, parents, children, and other properties which can be saved to or loaded from an XML file. The intuitive approach for ...
84 votes
6 answers
25k 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 ...
31 votes
10 answers
2k views

Should an object know its own ID?

obj.id seems fairly common and also seems to fall within the range of something an object could know about itself. I find myself asking why should my object know its own id? It doesn't seem to have a ...
29 votes
14 answers
10k views

Avoiding throw because we are not sure the exceptions will always be caught

I'm a junior in my company, and one of the coding rules they have is: "a constructor object must never fail" (i.e., never throw). So what if I give them an invalid parameter? Then, the ...
1 vote
1 answer
137 views

How to implement DMG (Game boy) cpu's register using OOP patterns/principles to max code reusability?

I-m looking to learn better use of OOP principles/patterns so I decided to start implementing at least the basics of a GB emulator (technical part is widely covered on diff sites). So I started with ...
24 votes
4 answers
38k views

Service layer returns DTO to controller but need it to return model for other services

Following this post https://stackoverflow.com/questions/21554977/should-services-always-return-dtos-or-can-they-also-return-domain-models and best practices in Software Arch suggestions by Martin ...
3 votes
4 answers
221 views

Does interface segregation principle apply to configuration data holders?

If you have a class representing your applicative config file. Instead of injecting that config class everywhere, would it be good application of interface segregation principle to expose several ...
-2 votes
4 answers
218 views

How do access modifiers actually help programmers? [closed]

So a 'fundamental' OOP principle is being able to control the access to various methods and fields from other parts of the program by making them public or private. The reason is so that you cannot ...
-3 votes
2 answers
174 views

Polymorphism with variable default argument count

I'm in the process of writing a library in Python, and I've run into a design problem concerning the use of polymorphism. I have an ABC with abstract method 'foo': class A(ABC): @abstractmethod ...
0 votes
2 answers
226 views

How to deal with boolean or enum variables used to decide code flow?

I am working on algorithm implemented in C++ that maintains several enum types. Say 3 to 4 enum types each with at least 4 different values. Plus the code maintains several boolean variables. The code ...
0 votes
2 answers
150 views

How to model a player playing a Blackjack game in OOP? [closed]

I am trying to write a blackjack simulation using OOP. I have written it in a non OOP way before, but now that I found out JavaScript has private properties now, I try to get familiar with it and ...
0 votes
1 answer
299 views

Validation in a simple DDD example

I'm doing a simple project that represents a CI Pipeline in a low scale. So I'm trying to model "the best way possible" to practice DDD. Basically I have the following information: ...
1 vote
2 answers
415 views

MVC-like architecture: Model-Controller communication of results/status

I'm struggling on designing a solid architecture for my project. In particular, I don't know how to handle the communication between the models and the controllers. My goals are: Following the ...
0 votes
3 answers
332 views

How to refactor "init()" into "physically make them two separate classes"?

According to https://softwareengineering.stackexchange.com/a/334994/432039, I know "init()" method is a code smell, and "physically make them two separate classes" is a way to ...
14 votes
5 answers
5k views

How to "Tell, don't ask" when 2 objects involves in the condition and the decision at the same time?

According to Explanation on how "Tell, Don't Ask" is considered good OO, I know the following is bad: if(a.isX){ a.doY(); } public class A{ public boolean isX; public void ...
0 votes
1 answer
160 views

Function objects with no state shouldn't be object oriented? [closed]

My question relates to this topic here: Are classes with only a single (public) method a problem? There I read in the comments often something like that: It is no longer object oriented. Because ...
0 votes
2 answers
394 views

How to restrict the construction of a domain object to an external service?

I have this object RelativeFoo{int relativeCode, Origin relativeTo} And I want to map it to this other object AbsoluteFoo{int absoluteCode} In order to do this, I need to use a service whose ...
5 votes
2 answers
575 views

Associations and References in UML Class Diagram

Is it possible to have an association without explicitly indicating the reference attributes in the participating classes in the UML class diagram? For example, consider this example : We can see ...
21 votes
4 answers
10k views

How to handle failure cases in C++ class constructor?

I have a CPP class whose constructor does some operations. Some of these operations may fail. I know that constructors do not return anything. My questions are, Is it allowed to do some operations ...
59 votes
9 answers
22k 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(...
-3 votes
1 answer
105 views

Are methods part of objects? or only fields are? [closed]

I wanna be sure if objects "contain" methods just as they contain their fields. Also, when you use the term "object", are you talking specifically about its fields?
1 vote
5 answers
626 views

How are strings simultaneously objects and primitive data types in C#?

In C#, strings can be used like objects with methods, properties, and other features of objects. At the same time, strings are treated the same as primitive data types like int or float in numerous ...

1
2 3 4 5
68