Romain Deneau
Aug 28, 2017 · 1 min read

My mistake. It’s a known code smell but in C#, mainly because default values are constant which is hard coded into compiled calling assemblies:

But even in JavaScript, default values should be used with care.

→ Does the function really perform the same task when called with all parameters vs with parameters without default value? Introducing an overload with another name may convey a clearer intent. E.g.:

With default parameters:

const createUser = (name = '', surname = '') => ({ name, surname });
const user = createUser();

With overloads, in TypeScript in order to have argument type without default value and built-in required arguments:

const createUser = (name: string, surname: string) => ({ name, surname });
const createEmptyUser = () => createUser('', '');
const user = createEmptyUser();

→ What should be done to calling sites when a default value is changed?

  • Nothing: let always use the default value?
  • Refactor to use the old default value?
  • The answer may be difficulted to get in some tricky cases. With overloads with an appropiated name, it should be simpler.

→ Some code smells may appeared:

)
    Romain Deneau

    Written by

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade