Skip to content

Clean code vs runtime data checking #372

@pathvine

Description

@pathvine

Bad:

function combine(val1, val2) {
  if (
    (typeof val1 === "number" && typeof val2 === "number") ||
    (typeof val1 === "string" && typeof val2 === "string")
  ) {
    return val1 + val2;
  }

  throw new Error("Must be of type String or Number");
}

Good:

function combine(val1, val2) {
  return val1 + val2;
}

The example does promote clean code. However, this example has discarded the important runtime type checking and necessary validations and seems incomplete. I agree that we should have "combine" function to do just the "combine", but if other team members get overly confident that the combine function would perform validation/data type checking as well, wouldn't this promote confusion/bugs instead? Appreciate your insight on this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions