Like Tree3Likes

Should functions be _this_ small?

This is a discussion on Should functions be _this_ small? within the Tech Board forums, part of the Community Boards category; Suppose you are writing a business contact program (or maybe a service, platform, etc). So, the scope of the program, ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Suppose you are writing a business contact program (or maybe a service, platform, etc). So, the scope of the program, its reponsibility, the one thing you want it to do well so that you will have people using the program (and maybe hopefully buying it or paying your subscription fee), is to manage business contacts. However, you will probably break down "managing business contacts" into various features, e.g., intuitive adding of a contact, search functionality, ability to send email, track tweets, social network connections, whatever.

    Does this mean that your program will do many things? Obviously. Yet, in doing all these, it does that one thing that you set out for it to do. There is no contradiction. It is just a matter of levels of abstraction. At another level, if you are going to program the adding of a contact, you may well break it down into different functions, e.g., to perform input validation before adding the contact.

    Likewise, there is no contradiction in the example from post #1: the pay function abstracts away the payment of all employees who need to be paid. The payIfNecessary function abstracts away the payment of a single employee who needs to be paid. If you have the pay function directly perform the payment of a single employee who needs to be paid, then at that level of abstraction (or lack thereof), it does two things: the payment of all employees, and the payment of a single employee. If you abstract the latter away through a function call, then at that level of abstraction, the pay function does one thing: the payment of all employees only, with the payment of a single employee delegated to another function. Hence, someone reading the implementation of the pay function can see that the pay function is correct, on the condition that the function(s) that it calls are correct, without having to go into the details of those functions (unless he/she needs to), due to the abstraction.

    Quote Originally Posted by megafiddle
    But can't everything be broken
    down into smaller "one things"? Is it doing one thing only because you can't see what's
    involved in doing it?
    That is elaborated (negatively, in my opinion), by Robert Martin's calculateAndDeliverPay example function. It breaks down the payment of a single employee further by abstracting away the calculation and delivery of payment, but I argue that this is unnecessary and can incur a mental cost for which there is no real benefit, since the abstractions provided by the functions it calls are already sufficient, in my opinion.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    500
    Ok, thanks. I think I can say then, that my understanding of "one thing" agrees pretty much with what you exemplified.
    I also think that my confusion is maybe related to how "doing something" is defined.

    For example, does a function which contains only a single function call, do anything?

    If I understand correctly, the answer would be "no".

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    It might. For example, maybe you need to pass a callback to some library function that expects the callback to have a particular interface. At the same time, you already have a function that performs the functionality required of the callback, but with a different interface. In that case, you may write a wrapper function that just calls the other function. This new function does do something, i.e., it adapts one function interface to another.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-28-2012, 07:11 AM
  2. A Small Problem Using fgets() and functions
    By Shamshir in forum C Programming
    Replies: 7
    Last Post: 04-07-2011, 11:13 PM
  3. Replies: 3
    Last Post: 03-20-2011, 02:39 PM
  4. Reasons for keeping functions small
    By maththeorylvr in forum C Programming
    Replies: 7
    Last Post: 03-26-2006, 12:38 AM
  5. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21