7 Replies - 927 Views - Last Post: 01 October 2012 - 01:19 PM

#1 The Architect 2.0  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 351
  • Joined: 22-May 08

Function LOC and Helper Functions

Posted 11 September 2012 - 11:46 PM

There are times when I'm making a prototype mini project and I end up having a 100+-line method (usually for testing an 'algorithm'). at the point, I think of breaking the function up into smaller helper functions. However, it always seems like I end up with helper functions that will only be called by that original function.

Does anyone else run into this problem? Should I just accept the very specialized helpers? Or should I only create reusable helper functions?


also, a minorly related question:

when I write helper functions, I've always been very hesitant to reference class members directly. Rather, I make the calling function pass in a reference to the class member. It just feels 'safer' to me just in case someone else calls my helper function without realizing it has side-effects (such as in the case when the class member serves as a MRU value).

NOTE: i'm doing work in C# right now, but I imagine this applies to most OOP languages.

Is This A Good Question/Topic? 0
  • +

Replies To: Function LOC and Helper Functions

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1910
  • View blog
  • Posts: 3,954
  • Joined: 11-December 07

Re: Function LOC and Helper Functions

Posted 12 September 2012 - 12:24 AM

A well-designed function, method or subroutine should do one job and do it well. It's not really based on lines of code although people will try and give you metrics for that. It should be small enough that you can hold it all in your head at the same time, small enough that its short name is an adequate description and small enough that you can quickly and easily write unit tests.

There are a lot more properties that good methods have, but these seem directly relevant to your question.

Good reasons for making a method or moving existing code into a method include the following (any one of these is a good indicator you should have a method)

You want to call the code from more than 1 place.
You have a piece of code that does a "thing".
Your existing method is getting long and complicated.
You have a complicated expression (often boolean) that would be clearer if it was referred to by name.
There is some logic you are unsure about or something in the specification wasn't clear (best to avoid altogether, but if you can't then isolate the uncertain part so you know where you will have to come back to.)

Again, not a complete list but food for thought.
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Function LOC and Helper Functions

Posted 12 September 2012 - 02:01 PM

For some examples look at my snippets section, especially the extension method ones. They're can of / sort of design to work together.
Was This Post Helpful? 0
  • +
  • -

#4 The Architect 2.0  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 351
  • Joined: 22-May 08

Re: Function LOC and Helper Functions

Posted 12 September 2012 - 04:17 PM

View Postcfoley, on 12 September 2012 - 01:24 AM, said:

A well-designed function, method or subroutine should do one job and do it well. It's not really based on lines of code although people will try and give you metrics for that.


I'll give you an example from the budget calculator program I'm currently working on:

there are 3 main methods of the 'BudgetManager' class.

AddIncomeItem(String name, DateTime dueDate, double value, TimeSpan repeatFrequency)
AddExpenseItem(String name, DateTime dueDate, double value, TimeSpan repeatFrequency)
CalculateBudget(TimeSpan budgetDuration)

NOTE: the real code is slightly more complex than that, but those signatures are good enough for an example.

The caller will call the Add...() methods first and then run the CalculateBudget method as many times as they wish(to see what their monthly, annual, etc budgets will look like).


At design time, 'CalculateBudget' seemed like a 'single action' and, to me, it still does.

There are quite a few steps inside that 'single action', however:
1. Get the earliest IncomeItem, the budget start date, and add the budgetDuration to it to get the budget end date.
2. Get all IncomeItems and generate each iteration till the budget end date.
3. Get all ExpenseItems and generate each iteration till the budget end date.
4. Loop through each budget interval and run the algorithm.

So, my 'single action' method can be decomposed into at least 4 separate methods. However, I am very sure these methods will only be called by the 'CalculateBudget' method. I'm pretty sure it's a bad idea to expose each method to the caller and expect them to chain everything properly.

So, given this API, how would you structure the code? unless the API is a little too ridiculous...

EDIT: I know the code will work no matter how it is organized, I'm just wondering if my method of creating 'single-use' helper methods creates problems with the project that I can easily avoid. Also, I'm just curious how other people do it.

This post has been edited by The Architect 2.0: 12 September 2012 - 04:19 PM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3183
  • View blog
  • Posts: 9,647
  • Joined: 05-May 12

Re: Function LOC and Helper Functions

Posted 13 September 2012 - 12:27 AM

I think that you are forgetting that in most object oriented programming languages, you can mark some functions as private. The 4 separate methods can be marked as private and it is only CalculateBudget() that is marked public.

If you are stuck in C land, you still have the option of only having CalculateBudget() in the public header file, and the 4 functions in the .c file and marked as static.
Was This Post Helpful? 0
  • +
  • -

#6 The Architect 2.0  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 351
  • Joined: 22-May 08

Re: Function LOC and Helper Functions

Posted 13 September 2012 - 03:13 AM

I've been referring to private helper functions the entire time. I'm looking for opinions on how to structure class methods.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3183
  • View blog
  • Posts: 9,647
  • Joined: 05-May 12

Re: Function LOC and Helper Functions

Posted 18 September 2012 - 04:03 PM

If they are private functions, then this should never be a worry:
I'm pretty sure it's a bad idea to expose each method to the caller and expect them to chain everything properly.


The only caller will be the public facing function which should know about the internal details of your class. Since, as you said, the functions are private, there is no external caller that needs to know about correct calling sequence.
Was This Post Helpful? 0
  • +
  • -

#8 The Architect 2.0  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 351
  • Joined: 22-May 08

Re: Function LOC and Helper Functions

Posted 01 October 2012 - 01:19 PM

http://vimeo.com/49484333?

ah, lucky me. the speaker directly addresses my question.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1