Linked Questions

162
votes
9answers
123k views

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 ...
25
votes
5answers
4k views

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()? ...
23
votes
4answers
5k views

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 ...
17
votes
6answers
4k views

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 ...
13
votes
5answers
2k views

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 ...
9
votes
2answers
2k views

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 ...
7
votes
3answers
766 views

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 ...
2
votes
4answers
1k views

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 ...
6
votes
4answers
368 views

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 ...
4
votes
2answers
267 views

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 ...
4
votes
1answer
306 views

Dealing with a large interface

I'm working on a program that solves a certain type of systems of equations. The main data objects are Equation, Variable, Solution. Then I have this interface, which represents all things that I want ...
3
votes
3answers
240 views

Injecting only the minimum required into a constructor or function - is there common terminology for this principle?

Probably best explained with an example: In a .NET C# project, we have a library class for which several function accept an HttpContext as one of the arguments. The thing is, that none of the ...
1
vote
2answers
360 views

Best practices regarding value objects

I had a few questions around value objects in DDD. I had a class ReportDefinition (basically a schema for a specific report that users can create) with the following properties (in Java): UUID id; ...
0
votes
1answer
99 views

One-field interfaces

I have an interface with only one title field: protocol Artist { var title: String { get } } (A) Should I pass the whole object as I did here: class Album { func setArtist(_ artist: Artist) ...
-1
votes
1answer
138 views

Dependency injection - passing domain objects to underlying dependencies with NInject [closed]

I'm working on a larger project. I hope I understood DI well and based-on that I'm able to do this: // domain object holding configuration ata var input = new GeneratorInput { Constructions = new ...

15 30 50 per page