Linked Questions
23 questions linked to/from The principle of least knowledge
191
votes
9
answers
154k
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 ...
26
votes
5
answers
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()?
...
29
votes
4
answers
4k
views
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 ...
23
votes
4
answers
6k
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 ...
19
votes
6
answers
5k
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 ...
35
votes
1
answer
8k
views
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();
...
13
votes
4
answers
5k
views
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 ...
14
votes
5
answers
4k
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 ...
4
votes
4
answers
3k
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 ...
10
votes
2
answers
3k
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
3
answers
1k
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 ...
7
votes
2
answers
6k
views
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 ...
8
votes
2
answers
2k
views
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.
...
6
votes
4
answers
499
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
2
answers
638
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 ...