JavaScript DOM Scripting By Example Improving the Application Code Refactor 2: Readable Branching Logic

Tony Minseok Kim
Tony Minseok Kim
Front End Web Development Techdegree Student 11,682 Points

In real life situation, or workplace situation

So Guil just put all the codes into a function, so that it is more readable and easy to use it over and over again, but I was just wondering when it comes to a real life situation, do you guys usually make functions first or just like Guil, make functions after once you find repeating code lines.

1 Answer

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 9,738 Points

In the "real-world" it's normally a bit of both. In general, you will be using and creating many functions. But there may be some instances where you find a lot of code repeated in multiple places in your codebase. Instead of just leaving them there, in an effort to be DRY (Don't Repeat Yourself), the common code is generally extracted into a new function.