vendredi 11 octobre 2019

Design patterns: Should repetitive function calls, to different modules, be moved to their own "abstraction"?

As a JS developer i often find my self struggling with the question, whether certain "procedures" should be moved to their own layer. For example:

const localStorageUser = jwtService.userExistsInStorage();//Returns a user object if available
if (localStorageUser) {//If so, "login" from it
    // debugger;
    store.dispatch(userActions.setUserData(localStorageUser))//Set the user object in the Redux store.
    AjaxService.setHeader('token', localStorageUser.data.token);//Set the token header for every ajax request.

    if (localStorageUser.data.shortcuts) {
        store.dispatch(navigationActions.setNavigation(localStorageUser.data.shortcuts));
    } else {
        store.dispatch(navigationActions.resetNavigation());
    }
}

This is some code from a top-level file in a React app. As you can see, it calls various functions on initialization, when the app loads.

Let's say, that i might find my self one day repeating this code, in a different part of the app(some automatic re-login). Is it "customary" to put such code in its own class? I mean, doing so would totally violate the Single responsibility principle, being that this class/module would be highly coupled with many other classes and responsibilities.

This is just one example(perhaps not the best one), but this dilemma arises often. How do experienced developers approach this issue?

Aucun commentaire:

Enregistrer un commentaire