If parameter X determines the return value Y, which function name would be the most appropriate?

Asked 1 weeks ago, Updated 1 weeks ago, 11 views

If there is a function that determines the return value X according to which parameter Y, which name would be most appropriate?

Naming is the hardest. ☹️


2022-09-19 23:33

4 Answers

I usually make it in the form of verb X(Y) when applicable. The fact that we make x with y values is important, but I think we can express it by the name of the parameter. Here are some examples.

2022-09-19 23:33

You can decide according to what the return value y is.

For example,

I want to get a random value. rand()

I want to get a sine value sin()

Instead of deciding what y is, we usually use the name f() as a function to simply enter x to get y.

2022-09-19 23:33

I like putting in the underbar.

The module I use rarely uses the underbar as a function name.


2022-09-19 23:33

I'd like to give you a slightly different opinion.

If you think about it, "The task of getting Y by putting X" is actually a feature that applies to any function. If so, it may actually be a very unhelpful statement to put it bluntly. So I think in terms of function/method naming, I think it's better to use the purpose of using the function, the reason for its existence, and use-oriented naming in a larger context.

For example, in real life, these codes often come out.

this.gameIsDraw = function (scoreA, scoreB) {
    return scoreA === scoreB;
// I'm going to...
this.answersAreSame = function (answerA, answerB) {
    return answerA === answerB;

When we saw this code, we were like, "Hey! How come there are so many functions to compare rivers with two values? The reason why we can't refactorize and say, "Together," is that the purpose and purpose of the two functions seem different to anyone. The content may be the same, but well, it's actually a coincidence. Derived from the design to do two different tasks: 'Check if the answer is tied' and 'Check if the answer is overlapped'.

In that sense, because it's a function that simply adds X to get Y, I think we should focus more on what the function does elsewhere than on naming it mechanically with X and Y. If you think like that, you don't have to worry about getXfromY or switchY at least. I don't know what other people think.

2022-09-19 23:33

If you have any answers or tips

© 2022 pinfo. All rights reserved.