For a use once only button callback, should the name be more related to the button or the task of the callback?

softwareengineering.stackexchange https://softwareengineering.stackexchange.com/questions/370537

  •  05-02-2021
  •  | 
  •  

Question

Suppose I have UI like it:

<script>
  var someFunction(){
    //some code to validate and then send message
  }
</script>
Message:<br>
<textarea></textarea><br>
<input type="button" id="submit" value="Submit" onclick="someFunction()"/>

Which a submit button calls someFunction() to validate and then send messages to server. I know someFunction is not a good name for name, but I don't know which naming principle is better: Should the name be:

1.Relate to which button to use:

var onSubmitPressed(){
  //some code to validate and then send message
}

2.Relate to the task of the function:

var validateAndSendMessage(){
  //some code to validate and then send message
}

which one is better?

Was it helpful?

Solution

It will be good if you give the name same as the action you will be doing. Like if you want to validate and send message to server then the name should be

validateAndSendMessage()

So whenever if new developer sees your code he will get to know for which purpose this function is written.

Licensed under: CC-BY-SA with attribution
scroll top