0

Suppose, we have a create method:

public void create(MyEntity entity, Foo foo, Bar bar)

This method creates entity. Now, we need a method to create entity and lock it. And I see two solutions here - either overloading or another method:

#1 Overloading

public void create(MyEntity entity, Foo foo, Bar bar, boolean shouldLock)

When shouldLock == false this method is equal to method create(MyEntity entity, Foo foo, Bar bar). However, this method has the following advantage. Instead of

if (shouldLock) {
    createAndLock(entity, foo, bar);
} else {
    create(entity, foo, bar);
}

we can use

create(entity, foo, bar, shouldLock);

#2 Another method

public void createAndLock(MyEntity entity, Foo foo, Bar bar)

The advantage of this solution is clear and explicit name without extra arguments.

The question - what solution should I use to create a good API using best practices?

0