29.07.2016 Views

front-end-developer_1_

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Front-End-Developer - Level 1<br />

You might be getting tired of me pointing out the indentation, but I'm going to do it again,<br />

because it's something worth taking your time to do right: everything inside the forEach()<br />

method is indented in two spaces, because it's code that's inside the callback function, and<br />

everything in a function gets indented so that it's obvious what's in the function and what's<br />

outside it.<br />

I'm going to <strong>end</strong> this lesson with a short lecture. One of the best definitions of "good code"<br />

I've heard comes from Corey Haines, a <strong>developer</strong> who helps train other <strong>developer</strong>s. The one<br />

thing that's certain about software, according to Corey, is that it changes. There will always<br />

be new features to add and bugs to fix. Often, the people working on the software will<br />

change, and even if they don't, the original <strong>developer</strong>s will need to modify code they wrote<br />

months or years ago, code that they sometimes can't even remember writing.<br />

One of things I really like about the new, looping version of the Madlibs JavaScript is that<br />

there's no repetition in the code, which makes it easy to change. Before, we did the same<br />

thing 6 times: get the value of the input, then put in in the appropriate blank space. If we<br />

want to change something - like, I don't know, capitalize all the text before inserting it onto<br />

the page (obviously this is contrived, but hopefully you get the idea) - then we have to<br />

change all 6 places. In this small example, the only room for error is maybe forgetting to<br />

update one line, although that would be embarrassing. But as your programs get more<br />

complex, you'll see that, if you have repetitive code, it becomes very difficult to maintain, and<br />

you will regularly update one place and fail to update another - or worse yet, update them in<br />

different ways that cause incompatibilities down the road.<br />

There's a principle in software development called Don't Repeat Yourself, or DRY. DRY code<br />

is easy to change, because you only have to make any change in one place. One way we<br />

DRY up our code is by taking repetitive bits of code and extracting them into a function.<br />

Another way is by taking something we do several times and by turning it into a loop.<br />

Whenever you finish writing some code, you should always look back to see if there is any<br />

way you can DRY it up.<br />

Also, remember how I harped on about using descriptive variable names? "Easy to change"<br />

doesn't just mean clean code - if you can't figure out what a variable is for or what a function<br />

does based on its name, then it's harder to change it down the road, when you might not<br />

remember exactly how your code works (or when somebody else is trying to change your<br />

code).<br />

Summary<br />

Simple loop with forEach() :<br />

Looping<br />

138

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!