Linked Questions

191 votes
9 answers

Why should I use a factory class instead of direct object construction?

I have seen the history of several С# and Java class library projects on GitHub and CodePlex, and I see a trend of switching to factory classes as opposed to direct object instantiation. Why should I ...
rufanov's user avatar
  • 1,963
27 votes
5 answers

Is it a good practice to avoid constants by using getters?

Is it a good practice to replace constants used outside of classes by getters? As an example, is it better to use if User.getRole().getCode() == Role.CODE_ADMIN or if User.getRole().isCodeAdmin()? ...
goto's user avatar
  • 389
29 votes
4 answers

Law of Demeter and over-wide interfaces

The Law of Demeter makes sense in some obvious cases. # better dog.walk() # worse dog.legs().front().left().move() dog.legs().back().right().move() # etc. But in other cases it seems to lead to an ...
samfrances's user avatar
  • 1,085
23 votes
4 answers

Is code like this a "train wreck" (in violation of Law of Demeter)?

Browsing through some code I've written, I came across the following construct which got me thinking. At a first glance, it seems clean enough. Yes, in the actual code the getLocation() method has a ...
user's user avatar
  • 2,713
19 votes
6 answers

Method chaining vs encapsulation

There is the classic OOP problem of method chaining vs "single-access-point" methods: main.getA().getB().getC().transmogrify(x, y) vs main.getA().transmogrifyMyC(x, y) The first seems to have the ...
Oak's user avatar
  • 5,265
35 votes
1 answer

Is this a good scenario to violate the Law of Demeter?

I have this code in some part of an application: long sum1 = new Multiples().ofAny(new long[] { 3, 5 }).until(32768).sum(); long sum2 = new Multiples().ofAll(new long[] { 3, 5 }).until(32768).sum(); ...
user avatar
13 votes
4 answers

Is there a clean way to model methods that only make sense depending on the current state of the object?

Let's say that I have a class called Mission. This class is intended to represent a mission requested by a professor working in a faculty. Mission has a private enum field representing whether the ...
Mehdi Charife's user avatar
14 votes
5 answers

According to Demeter's law, is a class allowed to return one of its members?

I have three questions concerning Demeter's law. Apart from classes which were specifically appointed to return objects - such as factory and builder classes - is it okay for a method to return an ...
user2180613's user avatar
  • 1,792
4 votes
4 answers

Composition over Inheritance, why not both?

I have this out of context scenario, where what I think is good practices leaves me in a situation of both implementing an interface, and using composition to do the implementation. Imagine the ...
Chris Wohlert's user avatar
10 votes
2 answers

Is it poor practice to call methods through multiple objects? [duplicate]

I have code that looks like this: tr.t.findIndexSmoothed(arg0.getX(), arg0.getY()); "tr" and "t" are objects. Is it bad practice to reach all the way down the object hierarchy to call methods? The ...
Azar's user avatar
  • 335
7 votes
3 answers

Does this violate the Law of Demeter?

Let's say I have a class SelectableEntity<T extends Entity> which has three methods, select, deselect, isSelected and count. To take a somewhat contrived example, let's say I'm building an ...
NRaf's user avatar
  • 311
7 votes
2 answers

Is the builder pattern appropriate to use to update Objects in a Service layer?

Currently, in our service layer, we pass an id as well as a new updated value, something akin to updatePersonName(Person person, Name name) which, in turn, calls the corresponding repository ...
Joe's user avatar
  • 405
8 votes
2 answers

Why can't Entities leave the inner layers in the Clean Architecture?

I was reading about the clean architecture, and I don't get this part: Don't use your Entity objects as data structures to pass around in the outer layers. Make separate data model objects for that. ...
Luís Soares's user avatar
6 votes
4 answers

What are the most effective techniques to stop a codebase from becoming difficult to maintain as it grows? [closed]

I am a software engineer of 10 years and wondered what other people have found effective in stopping a codebase from degrading over time. For example, some of the issues I have noticed are: large ...
Dizzle's user avatar
  • 185
4 votes
2 answers

Injecting a dependency that is a tree of dependencies

Is it a pattern or antipattern to inject a dependency that is a tree of dependencies? For example: public class ClassExample { private IContainer _container; public ClassExample(IContainer ...
Răzvan Flavius Panda's user avatar

15 30 50 per page