Functional Layouts, the best number of functions

Functional Quantity

Today, we are going to be talking about some best cases for functional layout and some ways to optimize those functions.

From my school days, there was no real conversation about either best cases or optimizations, so what I am going to be talking through will be mostly my own experiences and what I have gleaned from different books about programming. Most individuals believe that functions should be extremely little (down to a single line of code if possible), while others have a concept that a function should contain every aspect of the code so that they don’t have to write functions that are never going to be used again. Let me give you an example of each.

To do this we will use a single VBScript written two ways, this script opens a text file, and for each line in that text file, each line is a customer and a project name delimited by a pipe, it then converts the spaces to underscores (for easier managing for interfacing with a GitLab) and then ensures that the customer folder exists within the existing Projects Folder and then ensures that the project folder exists within the customer folder. If either of those folders don’t exist, then it will create the folder within that location.

To Many Functions

Pros

  • Each of the functions are incredibly small, in fact the largest function contains 5 lines of code.
  • Most of the functions are designed in a manner that would allow for an easy process to create unit tests against.
  • Most of the functions are extremely portable, they could be picked up and moved to another project with relative ease.

Cons

  • Way to long, there are 86 lines of code for a relatively easy project such as this. Plus, who actually read all 86 lines of code to figure out what was going on?
  • Most of the functions are one time wonders, used once within the project and not used again.
  • This could run slower… for this particular example, the Scripting.FileSystemObject took no time to instantiate, I have witnessed some objects take anywhere from .01 seconds to even 3 seconds to load into memory so this could become a very long winded event.

To Few Functions

Pros

  • Should be faster because we only had to instantiate the File System Object once, and then use it multiple times
  • There are no functions that are only called once for the entire project. For instance, the first example had a function called ProcessCustomerProjectFile, which was only used once, and not very effectively.
  • Everything that is done is done in one spot, no back and forth throughout the code to locate what each function is supposed to do
  • No reliance on code that might be altered.
  • Shorter, only 50 lines of code

Cons

  • Duplicate code. The replace spaces with underscores is used twice, the CreateFolder process is duplicated, etc. This is a ripe environment for one-off errors.
  • Impossible to unit test. In order to test it, you have to run the entire thing, if any part of it fails then it becomes a tracing nightmare.
  • Hard to read. because everything is happening, when another programmer is coming against this, they will have to read each and every line of code in that function and remember every variable, every transaction, every nuance of events that is going on to understand what this function is expecting to do.
  • Variable Span (VS) is large. So the VS is the description of the number of lines between when it is created and the final usage. For instance, FSO is created on line 6, but the last time it is used is line 45, so it’s VS is 39 (Last Line – First Line or 45 – 6). that is a lot of lines of code to have something interact with FSO and break it and also a lot of lines of code that the next developer will have to remember what the acronym FSO means. in the previous example the most that the VS possibly could be was 5. The VS for CustomerProject is 21 (it is created on line 10 and the last instance is on line 31).
  • Unable to be re-used. In essence, there isn’t a piece of this code that can easily be picked up and used again in a different area. it is completely dialed into the specific instance of the application.

Summary

Between these two extremes of a small project, it is easy to see where both camps can cause problems and go into the abyss of code analysis nightmare land that I find myself getting stuck in from other developers. To me, there is a smooth in the middle where if there is code that two or more subsequent lines of code that are duplicated, I will refactor that to become a single function that will become self-sufficient. If I find that my VLS is starting to grow long, or my function is starting to get past 30 lines of code (yes, that is just an arbitrary number that I have), I will refactor the code to make it easier to read. If I find that there is a section of code that is “strange” then I will refactor that code to make the function name logical statement of what is expected to happen (and probably document the holy living garbage out of it). After I have refactored it to a point that I am happy with, I will then take a quick once over on performance and see if it is running quickly. I will usually have three tests, a single run test, and medium size test and a what the heck am I thinking test to see if it is hindered. If it is, then I will optimize it. If I start running into questionable items (such as things I am not absolutely certain on), I will refactor that to a separate function so that it can run in a unit test. At this point, I see how Unit Tests can be really awesome utilities that will ensure that if you get what you expect, then it is happy; however, I have had to many “what was the user thinking” inputs that would crash my code that would never be caught within a Unit Test to make Unit Testing my ultimate goal.

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

22 comments

  1. Like!! I blog frequently and I really thank you for your content. The article has truly peaked my interest.

  2. Thank you for your own work on this web site. My daughter takes pleasure in participating in research and it is easy to understand why. Many of us know all about the powerful tactic you present vital items by means of your web site and therefore increase participation from the others on that subject matter while our daughter has been learning so much. Take advantage of the remaining portion of the year. Your conducting a pretty cool job.

  3. Thanks for your own effort on this web site. My niece really loves conducting investigations and it’s simple to grasp why. All of us learn all relating to the compelling tactic you render functional ideas on the website and in addition inspire response from other people on the idea so our own child is always discovering a lot of things. Take advantage of the rest of the year. You’re performing a stunning job.

  4. I wish to express my appreciation to the writer just for rescuing me from this particular situation. As a result of browsing through the the web and finding concepts that were not helpful, I thought my entire life was gone. Living without the presence of strategies to the issues you’ve sorted out all through this review is a serious case, as well as the ones which could have in a negative way damaged my career if I hadn’t come across your site. The ability and kindness in taking care of all the details was useful. I don’t know what I would have done if I had not discovered such a subject like this. I am able to now relish my future. Thanks very much for this reliable and amazing help. I won’t be reluctant to propose your web site to anyone who should get recommendations about this issue.

  5. I precisely needed to appreciate you once again. I’m not certain the things that I could possibly have implemented in the absence of these basics shown by you regarding such question. It has been the frustrating condition for me personally, but observing the very well-written style you handled it took me to weep with fulfillment. I am just happier for your assistance and thus sincerely hope you recognize what an amazing job you’re carrying out instructing other individuals by way of your web site. Probably you’ve never encountered any of us.

  6. I not to mention my guys have already been reviewing the good information and facts on your site while at once developed an awful feeling I never expressed respect to the web site owner for those secrets. Most of the men are actually as a consequence passionate to read through all of them and have now actually been loving them. Many thanks for getting indeed accommodating and also for picking out these kinds of exceptional useful guides millions of individuals are really desirous to discover. My honest regret for not expressing appreciation to sooner.

  7. I would like to voice my passion for your kindness supporting those individuals that really need assistance with in this matter. Your special commitment to getting the message all-around turned out to be particularly interesting and have in most cases permitted regular people like me to get to their goals. This valuable information entails a whole lot to me and even more to my office colleagues. Thanks a lot; from everyone of us.

  8. I want to express some appreciation to you for bailing me out of this circumstance. Just after looking throughout the world-wide-web and coming across thoughts which are not pleasant, I was thinking my entire life was gone. Being alive without the solutions to the issues you’ve fixed by means of your good report is a serious case, as well as the kind that could have in a negative way affected my entire career if I had not come across your blog. Your good ability and kindness in playing with all the details was helpful. I am not sure what I would have done if I had not discovered such a thing like this. It’s possible to at this moment look ahead to my future. Thanks so much for your high quality and amazing guide. I won’t hesitate to suggest your site to anybody who needs and wants counselling on this issue.

  9. I precisely had to appreciate you once more. I do not know the things that I might have tried without these points revealed by you relating to such a subject. Previously it was a real intimidating situation in my opinion, however , witnessing this well-written manner you solved it forced me to jump with fulfillment. Extremely happier for this information and in addition hope that you really know what a powerful job that you’re undertaking instructing the others all through your websites. I am certain you’ve never got to know any of us.

  10. My spouse and i were ecstatic when Peter managed to conclude his investigations while using the precious recommendations he received using your web pages. It’s not at all simplistic to simply possibly be giving out helpful tips which some other people have been selling. And we know we’ve got you to appreciate for that. The explanations you made, the simple web site navigation, the relationships your site help engender – it’s got many terrific, and it’s aiding our son in addition to us feel that that subject matter is exciting, and that is truly pressing. Thank you for the whole thing!

  11. Thank you so much for giving everyone an extraordinarily pleasant opportunity to read in detail from this blog. It’s always very nice and packed with a lot of fun for me personally and my office peers to search your site nearly thrice in one week to find out the new guides you will have. And lastly, I’m also always motivated concerning the wonderful thoughts served by you. Some 3 areas in this post are honestly the very best I have had.

  12. I have to express my passion for your generosity for persons who actually need help with in this question. Your special commitment to passing the message up and down has been extremely interesting and have without exception empowered others like me to get to their ambitions. The insightful suggestions indicates this much a person like me and extremely more to my mates. Thank you; from all of us.

  13. I have to get across my appreciation for your kindness supporting men and women that really want guidance on this one field. Your special dedication to passing the solution all-around ended up being exceptionally good and has empowered employees like me to reach their dreams. Your new interesting useful information means a great deal to me and still more to my colleagues. Regards; from each one of us.

  14. Pingback: WOW Blog
  15. Its not my first time to payy a quick visit thissite, i am visiting this site dailly and take fastidious information from here every day.

  16. Hello would you mind sharing which blog platform you’re using? I’m going to start my own blog soon but I’m having a hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I’m looking for something completely unique. P.S My apologies for being off-topic but I had to ask!

  17. You actually make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and extremely broad for me. I am looking forward for your next post, I’ll try to get the hang of it!

Leave a Reply

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