Functional Naming Conventions

Function Naming Conventions

Writing a function has it’s own challenges, today lets look at one of them I like to call “Who are you” and see if we can come up with an easy way to name our functions so that they are not only memorable but also expressive and self-documenting.

When I write a function, typically I am doing it because I figured that I needed to build something that would either be called many times or has some complex code that needs to be done, and more often then not, I am deep in the middle of a “brain dump” trying to get what is in my head down onto the screen through my slow fingers (at least they are slower then my head). This leads me to usually come up with some very lame and unacceptable functional names like “GetData” or “Calculate” or other garbage names that you can think of. I then have to stop myself and think through some of those constructs to figure out exactly what the function does.

The first thing that I will ask myself when attempting to name a function is, “What is this method going to do?” Simply enough, it can be one of three types, it can be either an action method, a retrieval method or actional retrieval method. What is the difference you might ask and why is it important? If I am expecting to return something it must be named something that describes what it is returning and also named in such a way that it will tell the person in the next office/cubicle what exactly is going to happen. It’s name should be the contract you have with the program. Take this simple function for example.

I left the function name blank at this point so that we could discuss the possibilities without a “tainted” thought. Looking at the code, it verifies that the Category Sales and Total Sales are Numeric values, then it ensures that Total Sales doesn’t equal zero and then calculates the percentage. Since the entire first part is just to ensure that there is no “monkey business” the rest of the sentance becomes the name of the function. Not only does this document exactly what the function does, but it documents exactly what is happening within the function itself (other languages use things like return, which wouldn’t give that little extra boost of moral).

Now lets try a different one, this one is a Actional-Retrieval.

Assuming that Customer Contact Information is an object, it appears that this will search for the Customer Contact Information, if it doesn’t find it will run the Create Customer Contact Information, that fairly describes to me…

This seems simple enough, how about just an action method?

This checks the current Maximum Temperature, if it is less then the parameter Degrees Temperature, it will assign Degrees Temperature to Maximum Temperature, I would go with CheckMaximumTemperature, but that doesn’t really describe it, nor does UpdateMaximumTemperature; however, AssignNewMaximumTemperature does describe what the action is, we are just validating that Maximum Temperature isn’t assigned to a lower value.

As we are creating these descriptions, when the function name becomes to long, it becomes more obvious that our function is doing to much and is becoming overloaded and should be refactored (or separated) into different functions, for instance UpdateMaximumTemperatureAndRefreshDisplaySettings is probably not a good function, this should probably be UpdateMaximumTemperature and RefreshDisplaySettingsToMaximumTemperature makes more sense.

Until next time, Happy Coding and may you be blessed!

15 comments

  1. I have to voice my affection for your kindness giving support to persons who have the need for assistance with this topic. Your real dedication to passing the solution all through turned out to be definitely functional and has encouraged those like me to reach their targets. Your new useful guidelines implies a whole lot a person like me and much more to my office workers. Regards; from each one of us.

  2. I must express some thanks to you just for rescuing me from this type of circumstance. After surfing throughout the the net and meeting suggestions which are not productive, I thought my life was well over. Living without the answers to the difficulties you have resolved as a result of your good write-up is a critical case, and the ones that could have negatively damaged my career if I hadn’t noticed the website. That natural talent and kindness in touching every item was crucial. I’m not sure what I would’ve done if I hadn’t come across such a thing like this. I can also now look ahead to my future. Thanks so much for this high quality and effective guide. I won’t think twice to endorse the blog to any person who would need assistance about this subject.

  3. My spouse and i felt really thrilled Edward could do his studies with the ideas he acquired through your site. It’s not at all simplistic just to always be releasing techniques which usually some others could have been making money from. And we also figure out we’ve got the blog owner to be grateful to for that. All the illustrations you have made, the straightforward website navigation, the friendships you will aid to engender – it is most superb, and it’s really leading our son and us consider that the content is fun, which is certainly especially important. Thank you for all!

  4. My wife and i ended up being so comfortable when John could round up his analysis through your ideas he grabbed from your own site. It is now and again perplexing to simply choose to be releasing instructions which often people today might have been making money from. We fully understand we’ve got the website owner to give thanks to for this. All of the illustrations you’ve made, the easy site navigation, the relationships your site make it possible to foster – it’s everything incredible, and it’s leading our son and our family believe that this idea is pleasurable, and that is truly indispensable. Thank you for all the pieces!

  5. I must show my admiration for your kindness supporting those individuals that really want assistance with this important issue. Your real commitment to getting the solution all through was extraordinarily productive and has in every case allowed ladies much like me to reach their endeavors. Your valuable tips and hints means a whole lot to me and additionally to my office colleagues. Thanks a lot; from everyone of us.

  6. I truly wanted to type a simple word to be able to thank you for those awesome hints you are sharing at this site. My considerable internet investigation has at the end of the day been paid with extremely good tips to go over with my good friends. I would repeat that most of us visitors actually are undoubtedly lucky to exist in a fabulous website with so many brilliant professionals with very helpful tips. I feel somewhat lucky to have come across your entire weblog and look forward to some more excellent moments reading here. Thanks a lot again for a lot of things.

  7. A lot of thanks for your whole efforts on this website. Debby takes pleasure in conducting internet research and it’s really easy to see why. A number of us know all relating to the dynamic means you make efficient secrets on this blog and inspire contribution from other ones about this idea and my simple princess is in fact starting to learn a lot. Take advantage of the rest of the year. You’re conducting a wonderful job.

  8. Needed to send you that little remark just to give many thanks yet again relating to the amazing tactics you’ve contributed on this site. It’s so incredibly generous of you to provide publicly what exactly some people would have distributed for an electronic book to make some cash for themselves, principally considering that you might have tried it if you decided. The pointers in addition acted to be a easy way to realize that other people have the identical keenness just like my very own to know the truth lots more in regard to this condition. I’m sure there are a lot more fun instances ahead for people who see your blog post.

  9. Thanks for all of the hard work on this site. Ellie loves setting aside time for investigations and it’s really easy to see why. I know all about the lively mode you offer priceless secrets by means of this web blog and as well as boost contribution from some others on this subject then our daughter is in fact starting to learn a whole lot. Take advantage of the rest of the new year. You are always doing a useful job.

  10. My wife and i felt excited that Ervin managed to finish up his analysis out of the precious recommendations he had from your weblog. It’s not at all simplistic to just be making a gift of points which other people could have been trying to sell. And we all keep in mind we’ve got you to give thanks to because of that. The most important explanations you made, the straightforward web site menu, the relationships your site give support to create – it’s got everything exceptional, and it’s facilitating our son and the family consider that this subject is awesome, and that’s pretty mandatory. Thanks for the whole thing!

  11. I precisely desired to say thanks again. I am not sure what I would have implemented in the absence of these methods revealed by you relating to that subject matter. Completely was a very challenging concern in my position, nevertheless discovering the skilled tactic you managed that took me to cry with fulfillment. Now i am thankful for the help as well as wish you recognize what an amazing job you are providing educating some other people using your website. Most probably you have never come across any of us.

  12. I want to show my thanks to the writer just for rescuing me from this type of problem. As a result of checking through the the net and obtaining methods that were not powerful, I was thinking my life was well over. Being alive without the approaches to the issues you have fixed all through the blog post is a crucial case, and the ones that could have badly affected my career if I had not come across the website. Your actual training and kindness in handling every part was excellent. I’m not sure what I would have done if I had not encountered such a thing like this. I can also at this moment look ahead to my future. Thank you so much for the expert and amazing help. I won’t be reluctant to recommend the blog to anyone who should receive assistance on this matter.

  13. I just wanted to type a quick comment to appreciate you for the unique steps you are placing here. My particularly long internet search has at the end of the day been paid with pleasant concept to go over with my visitors. I ‘d suppose that we visitors actually are rather blessed to live in a perfect site with very many awesome professionals with interesting guidelines. I feel pretty privileged to have seen the web site and look forward to really more exciting minutes reading here. Thanks a lot again for everything.

  14. My spouse and i felt contented Chris managed to conclude his researching with the ideas he received out of your web page. It is now and again perplexing to simply always be giving away guidance which usually people today could have been trying to sell. And we also fully understand we’ve got the writer to appreciate for that. All the illustrations you’ve made, the straightforward web site menu, the friendships you help to instill – it is mostly impressive, and it’s really letting our son in addition to our family recognize that the topic is entertaining, which is extremely pressing. Thank you for all!

  15. I just wanted to type a simple message so as to say thanks to you for all of the stunning pointers you are giving on this website. My particularly long internet lookup has at the end of the day been rewarded with extremely good points to talk about with my best friends. I ‘d admit that we readers are very much fortunate to be in a notable network with very many special people with helpful pointers. I feel very grateful to have encountered your web site and look forward to many more enjoyable minutes reading here. Thanks once more for a lot of things.

Leave a Reply

Your email address will not be published. Required fields are marked *