Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
1 vote
1 answer
87 views

Seeing no way to avoid (deeper) inheritance tree without producing code duplication

I am rather new to OOP and I can't wrap my head around a problem I have frequently encountered by now. I hope, I understand the reasoning behind why composition is favorable to inheritance (the deeper ...
obsssso's user avatar
  • 11
0 votes
2 answers
178 views

How to use an interface/something similar to clean up duplicate code in Java

I have output functions that output data from different objects to their corresponding files. Most of these functions follow the same pattern, the only differences are the objects being used and by ...
thePandasFriend's user avatar
4 votes
1 answer
945 views

How can I avoid duplicate code with the same method body but different return types in Java?

The Idea: When I was using hibernate I saw that everytime I had to write some sort of code. So I moved them to another method as wrapper. Where there will be functional interface as argument so that I ...
Farhat Shahir's user avatar
0 votes
1 answer
917 views

How to avoid duplicate code if two classes extending different classes

I have two classes which extend two different base classes. Those classes setup custom environment. The are three methods common in both the classes: prepare(), setup() and teardown(). Each method ...
ivoriik's user avatar
  • 165
1 vote
1 answer
247 views

Swift: Sharing logic tied to state when composition isn't a good option

Let's say you have two classes conforming to a protocol and you want some logic to be shared between them. In languages like Java, you'd typically create an abstract class with the shared logic and ...
Grant Palmer's user avatar
1 vote
2 answers
62 views

How to use two near-identical classes that do not share the same parent class?

I have 2 near-identical classes that ideally should share the same parent class but don't (because they come from separate libraries whose source code I cannot change). To illustrate in an example, I ...
mchen's user avatar
  • 10.1k
1 vote
3 answers
1k views

Should we have similar DTOs?

I have 2 dtos that are used differently, but they have similar fields. Eg, MyIdESDto uses info from MyIdDto. The latter object is built with data from the DB, while MyIdESDto is built from the ...
xeon123's user avatar
  • 899
5 votes
3 answers
3k views

How can I add Levels to my game with less duplication of code?

I am designing a game with multiple levels. I have a setup class that sets up the board based on the argument it receives, which indicates which level it should set up. Here is the class: public ...
nhouser9's user avatar
  • 6,780
1 vote
2 answers
160 views

improvement of program through inheritance

I have written a program that reads in from a file and store the information in my own made collection class. My program works fine however i was wondering if there is anything i could do to improve ...
Saad's user avatar
  • 397
-1 votes
2 answers
160 views

Java OOD and code duplication

I started creating a basic roleplaying game, and now I work on the basics. I have a code duplication for creating new characters and for existed character, which is a very bad things. I'll explain my ...
Niminim's user avatar
  • 668
0 votes
2 answers
90 views

How to reduce code duplication with private variables setting without using inheritance?

I have classes MonthTimeCard and MonthReport. They use same input field for choosing month, so the validation is the same and initialisation is the same. class MonthTimeCard { private function ...
Dariux's user avatar
  • 4,233
1 vote
1 answer
130 views

Avoiding duplicate code while handling exception

For my application, I need to use two slightly different algorithms for persisting an entity: def persistRandomAlias(self, alias): self.em.persist(alias) try: self.em.flush() ...
vikingr's user avatar
  • 123
2 votes
3 answers
1k views

Java: identical Objects, how to avoid duplicated code

I've a proyect that call various webservices using an external library. This library give me objects like this: public static class ObjA { @XmlElement(name = "counter", required = true) ...
moretti.fabio's user avatar
3 votes
4 answers
993 views

Code smell - if/else construct

I have a list of 9 elements. The first three represent positions, the next velocities, the next forces. Sometimes I require forces from the array, other times velocities, and other times positions. ...
user1654183's user avatar
  • 4,565
3 votes
4 answers
1k views

Avoiding duplicate code when performing operation on different object properties

I have recently run into a problem which has had me thinking in circles. Assume that I have an object of type O with properties O.A and O.B. Also assume that I have a collection of instances of type O,...
jtlovetteiii's user avatar
1 vote
1 answer
47 views

'Should' instance variables or properties ever share getter/setters

Say I have a JavaScript object: myObject = { oneArray: [], otherArray: [], setOneArray: function(){ /*some code that sets the contents*/ } setOtherArray: function(){ ...
user avatar
0 votes
4 answers
307 views

Java OOP optimising code

I am working on my Java assignment - Minesweeper game clone. I have two almost identical (only text label and text frame differ) methods gameWon() and gameLost() which are responsible for showing "...
Julio Bordeaux's user avatar
3 votes
2 answers
3k views

Patterns to remove duplicated code

A Java project I've been working on integrates with several RDBMSs. The most obvious way for us to reduce the code duplication between the way we handle them is to make a type hierarchy like: ...
Dan's user avatar
  • 7,687
0 votes
2 answers
134 views

Encapsulating related fields into a companion object/class and avoiding duplication

My application uses a properties-file to load several properties. Every instance of the application has 3 environment-related parameters - one of them is a property, the other two are computed based ...
teo's user avatar
  • 1,403
0 votes
5 answers
209 views

How to avoid code duplication here?

I have two flavours of a method in a class, one with an extra parameter first one: public override void CalcV(IV iv) { initializations otherOperations for (int i=0; i < NUM; ++i) ...
jambodev's user avatar
  • 351
1 vote
2 answers
93 views

Method and Constructor<?> both inherit from Member, both having a getExceptionTypes() method. How to avoid code duplication in this scenario?

I have both these methods in my code base, which I'd like to merge in some way to avoid code duplication: protected IJavaType[] getExceptionTypes(Method method) { Class<?>[] ...
devoured elysium's user avatar
0 votes
4 answers
256 views

How to handle the same code path with different parameters?

I've encountered a situation where, trying not to have to modify the underlying source (it's not really "my" code at that level; otherwise, I'd love to modify it), I have two almost identical code ...
palswim's user avatar
  • 12.1k
1 vote
2 answers
179 views

Similar logic but different classes (avoid duplication)

I've got four similar class structures that are generated from XSDs, each one is a different version of an API. The thing is, I have classes that operate on all these different class structures, but ...
Gaisorama's user avatar
1 vote
3 answers
208 views

How to prevent multiple classes for the same business object?

A lot of the time I will have a Business object that has a property for a user index or a set of indexes for some data. When I display this object in a form or some other view I need the users full ...
Greg's user avatar
  • 7,383