2

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?

5

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.

  • Yes! Intent should be communicated. But I would go for validate, since the function should validate and in case of successful validation, the browser should "submit" aka "send the message" – Thomas Junk May 7 '18 at 10:35
  • 1
    The main question was stating to validate and then send messages to server so I kept such name.. If it was only for validate then we can give the name as validate() or formValidation() – Sambhaji Katrajkar May 7 '18 at 11:20

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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