If there is some code that needs to be implemented across multiple controllers (lets say, 10), e.g.:
// Inside a controller function
var myField = null;
var response = service.callBackend(function(){
// Other logic
// starts here
if (response) {
myField = {
name: response.name,
value: response.val,
access: access.READ,
};
};
// ends here
// Other logic
});
What is a better approach? To create a myField
in every controller as an object with the check for response
, or extract the logic into separate function?
function helper() {
return {
createReadonlyField: function(content) {
if (!content)
return null;
return {
name: content.name,
value: content.val,
access: access.READ
};
}
};
};
And then in every controller you would call helper function to construct the field:
// Inside a controller function (helper object is passed as Dependency Injection)
var myField = null;
var response = service.callBackend(function(){
// Other logic
myField = helper.createReadonlyField(response);
// Other logic
});
Is the code readability reduced by extracting it to a function?