loading...

re: Refactoring: My 6 favorite patterns VIEW POST

FULL DISCUSSION
 
function sayHello({ toName, punctuation, fromName }) {
  return `Hello, ${toName}${punctuation} From, ${fromName}.`
} 

sayHello({ toName, punctuation, fromName });

I would prefer to modify it to below. In this way the signature of method is clear. It also tells what kind of objects are acceptable. It also tells what kind of properties should be bound together as an object. The grouping of properties is domain specific. It is not anonymous.
I am taking clues from Replace Anonymous Function with Expression.

function sayHello(personPair,punctuation) {
  const {toName, fromName} = personPair
  return `Hello, ${toName}${punctuation} From, ${fromName}.`
} 
const personPair = { toName, fromName };
sayHello(personPair, punctuation);

Note: I am not satisfied with personPair as variable name. Feel free to suggest better variable name.

code of conduct - report abuse