by disillusionist · 1y ago · 0 comments
One nice unexpected side effect is that I end up with more consistency in variable naming when using a param object in order to benefit from object definition shortcuts.
ie I will usually write something like
const firstName = getFirstName(); doTheThing({ firstName });
rather than const fName = getFirstName(); doTheThing({ firstName: fName });string combineName(string first, string last);
Okay, I assume the result is the full name, but I don't really know that like I would if the return had a name in the header. (The function can be getFullName, yes, but that's so simple it doesn't matter).
function combineNames({ first, last }) {
const fullName = `${first} ${last}`;
return { fullName };
}
const { fullName } = combineNames({first: 'John', last: 'Doe' }); const [state, setState] = useState(initialState)
instead of const {state, setState} = useState(initialState) type FullName = string;
function combineName(first: string, last: string): FullName;
Also documentation comment for function is what specifically solves this - describes what function does and returns.Then pair it with destructuring assignment!
`const { fullName } = getName({ firstName, lastName )}`
A more complex example will probably return a more complex type that is more self-documenting. If the type is a single scalar value then I don't see the value of adding another name here.
In fact, just forget that it is a function that does something. Treat it as the thing it returns.
string fullName( string firstName, string lastName )
But in the example from the OP, there isn’t really a primary argument. You're dealing with multiple values that carry equal weight, which makes positional arguments harder to reason about at a glance.