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

PHP Integrating PHP with Databases Limiting Records in SQL Filtering Data by Category

Steve Seebart
PLUS
Steve Seebart
Courses Plus Student 11,897 Points

Duplicated code in the *_catalog_array functions

The functions full_catagory_array, category_catalog_array and random_catalog_array all have a lot of common code, differing mainly in the SQL query string. Perhaps this is addressed in later videos, but would it be a good practice to write a single function that accepts the query as a parameter. If not, why not?

1 Answer

Geraldo Hernandez
Geraldo Hernandez
7,515 Points

I feel that combining those three functions into a single larger function would add complexity to the code. Earlier we were taught to continually refactor our code to improve its readability. The three functions have distinct query actions on the database, and even category_catalog_array requires accepting a string category parameter while the others don't accept any.

Looking at Alena's code at this point in the course, I think that we could improve it by refactoring single_item_array since the 2nd half of the function executes a 2nd query for additional people based on their role. We could call a function within single_item_array and improve readability.