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 Simplify Repetitive Tasks with Loops The Refactor Challenge, Part 2

jsdevtom
jsdevtom
16,963 Points

Are all of those functions necessary? If yes, why?

I understand returning

Math.floor(Math.random() * 256 );

in a function because of 'DRY'. But is it efficient? Sure we have to copy and paste the code three times otherwise, but it's still shorter. And if we were going to change the function in the future, it would be useful to change it once instead of three times. But this does not appear to me as being something that needs to be amended... ever. Additionally, it seems to be a lot of hassle for not a lot of reward.

Is this not making the code unnecessarily longer at the cost of speed? Under which conditions would a developer make this trade off (speed vs functions)?

Thank you in advance.

1 Answer

jared eiseman
jared eiseman
29,023 Points

Are they all "necessary"? Maybe not. I mean... the code works without declaring individual functions for each aspect.

One of the big reasons for doing it though, is readability. For something like this project, it's relatively simple... but say I have an application that needs to do a host of things. I need to pull data from two different database sources, then parse it and format it all to be appended to an HTML page. Something like this could easily become very, very long, and thus, difficult to read and follow. Abstracting some of it away into seperate functions that are named well could very well help in understanding what it is you, or someone else looking at your code are reading. Particularly useful if you're coming back to your code after a long period of time, when it's not particularly fresh in your memory.

Another reason is reusability. Again, in something like this, the app is relatively simple, so it might not be all that important to do this. But I think something the lesson is trying to get across is that it's a best practice because as your code gets more in depth, and complex, you may find yourself needing to do a similar task, tens, hundreds, maybe even thousands of times. Personally, I'd rather write one line, then scroll through thousands of lines of code to find the place where i need to copy from to then scroll back and paste. Or worse, have to rewrite a complex chunk of code I've already written.

Hope this helps.

jsdevtom
jsdevtom
16,963 Points

Excellent explanation, thank you

Adding to jared eiseman's answer, using divers functions also makes development (in just about any programming language) of large, real world projects, much easier.