architecture-samples icon indicating copy to clipboard operation
architecture-samples copied to clipboard

[todo-mvp] Change setLoadingIndicator to two separate functions

Open salmanseifian opened this issue 7 years ago • 1 comments

According to SOLID principles, a function should have only a single responsibility, but the setLoadingIndicator() function has two responsibilities, showing and hiding. I want to break it into two functions such as showLoadingIndicator() and hideLoadingIndicator().

salmanseifian avatar Apr 15 '18 13:04 salmanseifian

In the todo-mvp project, the method:

void setLoadingIndicator(boolean active); is used for both showing and hiding the loading spinner depending on the boolean value.

Why the Issue Was Raised Problem: One method is doing two different things depending on a flag (show vs. hide).

SRP Violation: Ideally, one method should only have one reason to change. Here, setLoadingIndicator(true) means "show" and setLoadingIndicator(false) means "hide", so it’s mixing two responsibilities in one method.

Suggested Fix Instead of:

@Override public void setLoadingIndicator(final boolean active) { if (active) { progressBar.setVisibility(View.VISIBLE); } else { progressBar.setVisibility(View.GONE); } } Option 1 – Split into Two Methods

@Override public void showLoadingIndicator() { progressBar.setVisibility(View.VISIBLE); }

@Override public void hideLoadingIndicator() { progressBar.setVisibility(View.GONE); } Presenter Usage Before:

view.setLoadingIndicator(true); ... view.setLoadingIndicator(false); Presenter Usage After:

view.showLoadingIndicator(); ... view.hideLoadingIndicator(); Pros of Splitting Clearer intent (reading code is easier — no booleans to interpret).

Follows SRP.

Easier to test (can assert which method was called instead of checking a boolean value).

Less chance of calling the wrong logic accidentally.

Cons Slightly more methods in the interface (minor).

Requires updating all callers.

Recommendation: For MVP patterns like todo-mvp, this change improves readability and follows clean code principles, so it’s worth implementing. You just need to update the View interface, Presenter code, and all usages.

VaradGupta23 avatar Aug 08 '25 11:08 VaradGupta23