-1

I frequently find myself pulling up helper methods in order to make my code better, but end up with duplicate method names. Is there a standard way to name such methods without getting duplicate names?

Example of code before:

function hello(suffix) {
  if (typeof suffix === 'string') {
    return `Hello ${suffix}`
  } else {
    return 'Invalid entry'
  }
}

Becomes:

function hello(suffix) {
  const isSuffixString = isSuffixString(suffix) // can't do in JavaScript
  if (isSuffixString) {
    return `Hello ${suffix}`
  } else {
    return 'Invalid entry'
  }
}

Of course this is contrived and isn't very useful, but it illustrates the point.

I could call the helper getIsSuffixString, for example, but it feels like too much.

I just find that the helper method name and the ensuing variable often should have the same name.

closed as primarily opinion-based by gnat, Jörg W Mittag, Ben Cottrell, BobDalgleish, Flater Jun 11 at 9:59

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • It really depends on how many of these you have in a given method or section of code, but I find myself trying to have the method name as some variant of "TryParse" (TryParseSuffix in the example case), then if I need to use the true/false beyond the initial check I would make the variable something like isValidSuffix. For the comment in the example "can't do in javascript", I forget what you can/can't do from that perspective, but are there any other limitations in what you would look for in an answer? – eparham7861 Jun 9 at 16:14
3

First, in the shown case, it makes no sense to introduce an explaining variable since using the function directly is equally readable, so you could write:

function hello(suffix) {
  if (isSuffixString(suffix)) {
    return `Hello ${suffix}`
  } else {
    return 'Invalid entry'
  }
}

However, if you need the result of a function call more than once, it may be useful to store the value of a function call to avoid multiple calls, even if it does not add anything in readability. In a case-sensitive language, one could make use of the convention for beginning method names with an upper case letter, but local variables with a lower case letter:

const isSuffixString = IsSuffixString(suffix)

Unfortunately, there are lots of environments where the usual convention is to use lower case letters for both, functions and local variables. In those languages, I would recommend to use a more descriptive name for what the functions does, and a slightly more abbreviated name for local variables, like:

const isSuffixString = isTypeofSuffixEqualToString(suffix)

And of course, it would also work the other way round, but that's up to your personal taste. Use whatever your peer reviewer tells you he/she understands best.

  • Thanks, I think the latter example is probably the best way to go about it. – Adam Thompson Jun 9 at 22:31
0

This is probably a matter of personal tastes.

I have several verb prefixes that I use for functions. In your case I'd use check. For me, a function like check...() must always return true or false (or raise an exception), and must be idempotent.

So I would write,

const checkedIsSuffixString = checkIsSuffixString(suffix)
if (checkedIsSuffixString) {
    // suffix has been checked and is a string.
    // (This comment is clearly redundant)
    ...
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.