-1

So far my weakest side has been proper code structuring and organization. I wanted to know how I can properly apply separation of concerns to the following code to make it more organized.

I have a function deleteCurrentList().

It does two things: deletes the currently selected list and navigates back to the previous page.

function deleteCurrentList() {
     deleteList(current_list_id);
     navigate("..");
}

From my understanding, this is bad code because

  • A: It does more than one thing.
  • B: Its name doesn't describe fully what it does.

So one idea that I had in mind is to rename this function to deleteCurrentListAndNavBack(), but that is a long name, and it still doesn't solve the issue of the function doing more than one thing.

So I wanted to know, what should I do in this situation?

How do I split these two actions while still being able to delete the list and navigate back in the same sequence?

2 Answers2

1

The long name at least tells you what the function does. So that is an improvement.

Now let me guess that with the current list deleted you can’t stay on the same page and you have to navigate somewhere.

So maybe call the function “closeCurrentPage” whose single responsibility it is to close the current page plus of course take all actions necessary for it. And necessary actions are deleting related data (but the caller doesn’t need to know it’s a list), and navigating from the page that is closed to some other page.

gnasher729
  • 49,096
1

If the deleteCurrentList is bound on any event then I recommend renaming it to one of onHover, onClick, etc. That will describe that the sequence of operations is done on the corresponding event. In that case, it will be more callback than a function and it is alright to do more than one action.

function onEvent() {
     deleteList(current_list_id);
     navigate("..");
}