Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Structuring Your Code!
You have completed Structuring Your Code!
Preview
Keep your code free from repeats.
DRY, or donβt repeat yourself, is another popular acronym reminding you to keep your code free of repeats whenever possible.
Refactoring is the process by which you improve existing code.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Repeating yourself is a common
mistake new programmers make.
0:00
It can be easy when creating a project
0:05
to not notice you've created the same
variable twice, or repeated some steps.
0:07
Every coder creates repeat
code at one time or another.
0:13
DRY or don't repeat yourself is
a great acronym to keep in mind as you
0:19
code to help you remember to review
your work and check for those repeats.
0:24
The best way to see how DRY
works is to see it in action.
0:30
Let's tackle some repeats,
open the DRY file and let's dive in.
0:34
In this file are two
lists with items inside.
0:39
Below, I'm converting the price
into numbers instead of strings for
0:43
both fruits and vegetables.
0:48
Lastly, I'm adding another
key value pair to each.
0:50
There's a lot of repeating going on here.
0:55
One way to tackle this is to use for
loops.
0:58
Let's try it and
see what our code looks like.
1:01
For fruit in fruits,
1:09
Tab this over and
we can remove the number and the s.
1:15
And delete the extras.
1:22
For vegetable in vegetables,
1:28
same thing, tab over, remove the index and
1:32
the s and remove the excess.
1:39
For fruit in fruits,
1:50
Remove the index and s, remove the excess,
1:57
And one more time for
2:03
vegetable in vegetables, tab,
2:06
index and s, remove the excess.
2:11
Already a lot cleaner, right?
2:18
Well, this is definitely better, our code
here is still repeating the same tasks.
2:21
We're repeating the tasks to translate
the price string into a number and
2:27
the task to add a location.
2:32
Let's make these two tasks into functions.
2:34
The first we can call convert_price.
2:38
It will take the list we want
to work with as a parameter,
2:48
then we can use the same for
loop, To convert the price.
2:52
We just need to change out fruits
to list and fruit to item.
3:02
Item and item.
3:10
Now all we need to do is get rid
of the vegetable version, and
3:17
call our function twice
with our two lists.
3:22
Let's do the same thing for our second
function and call it add_location
3:38
Now, we're not repeating for loops either.
4:13
Most of the time when I see repeated code,
4:17
it means I need to convert
it into a function.
4:20
You've tackled creating more dry code,
awesome.
4:24
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up