-2

According to Is it OK to split long functions and methods into smaller ones even though they won't be called by anything else?, I can split long functions into smaller functions even if they are used once only, but my question is , how should I name extracted functions? For example, I have code like that:

var changeProfilePic=function(){
  //upload image to server, get response "response1"
  //submit upload result to another server
}

how should I name the extracted functions? Should I name it as number:

var changeProfilePic=function(){
  changeProfilePic1();
  changeProfilePic2();
}

changeProfilePic1(){
  //upload image to server, get response
}

changeProfilePic2(){
  //submit upload result to another server
}

or name it relate to the task:

var changeProfilePic=function(){
  uploadImage();
  submitUploadResult();
}

uploadImage(){
  //upload image to server, get response "response1"
}

submitUploadResult(){
  //submit upload result to another server
}

?

  • 1
    If you answer yourself why splitting functions in small ones will be better for you - you will get answer your question. – Fabio May 9 '18 at 5:24
3

By considering future use of your code,

you should always give the function name according to the action you are doing in to it. For. ex. If you are uploading image then the name should be uploadImage(), if you are retrieving data, then it should be retrieveData() or to submit result function name should be submitUploadedResult().

So in future, if new developer sees name of your function, without seeing code inside the function, he/she will get to know for which purpose your code is written.

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.