Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

Mental flag for when to create a function?

I'm wondering if there's a way that anyone has trained themselves mentally to recognize when code should be moved to a function?

In this video for example, it never occurred to me to create the getStudentReport function because it didn't seem to have any effect in terms of the DRY programming principle. It seems like it was more for the sake of separating the code into separate logical blocks of functionality. This totally makes sense... but I'm wondering if there's a way to scan through like... my existing pet projects that I've made and quickly recognize when it makes sense to separate code into its own function just for the sake of separation if there's no obvious DRY benefit.

Like, are there rules of thumb or best practices for this?

1 Answer

In general a function should do just one thing. For example: if your function asks for a series of integers, sorts them in order, prints the ordered list, and then prints the highest value; all of that is too much for one function to do and it may be best to split it into several functions, with each task having its own function.

Makes sense.