All Questions
Tagged with code-duplication oop
24 questions
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
-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 ...
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 ...
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()
...
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)
...
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.
...
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,...
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(){
...
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 "...
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:
...
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 ...
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)
...
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<?>[] ...
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 ...
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 ...
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 ...