Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Take your code to the next level by reviewing your work and optimizing your code.
KISS, or keep it simple silly, is a popular acronym to remember that simple is usually better.
Refactoring is the process by which you improve existing code.
If you've been reading up on
how to improve your code,
0:00
you've probably seen
this acronym all ready.
0:04
KISS or keep it simple silly, helps
developers write code that is easier for
0:07
others to understand,
including future you.
0:12
It's a common problem for
developers to come back to their
0:16
own projects in the future, and
not be able to read their own code.
0:20
One important note,
KISS doesn't always mean less code,
0:25
it just means more simple code.
0:31
You can do some pretty cool and
complicated things with programming.
0:34
But if it takes a paragraph of code
comments to describe what's happening,
0:38
your code may be too complicated.
0:42
And there may be times when you can't
simplify a complicated piece of code, and
0:45
that's okay.
0:49
Just tackle the areas you can, and
0:50
add comments to help
describe what's happening.
0:53
Let's check this out with some code.
0:56
If you haven't already,
open the KISS file to follow along.
1:00
Here we have some code
that needs tweaking.
1:04
I've listed out a few common ways
developers overcomplicate their code.
1:07
The first complication that comes to
my attention, is over commenting.
1:13
Don't get me wrong, I love code comments.
1:18
And I wish developers use them more,
but you can go too far.
1:22
Use code comments to describe elements of
your code that aren't easy to understand,
1:27
just by reading through your code.
1:32
This will make it easier for
other developers to understand, and
1:34
use code that you've written.
1:38
Let's go through and remove
the unnecessary code comments in our file.
1:41
Line 4, can go.
1:46
It states what we already see,
that this variable holds the number 5.
1:50
I don't think that this is a piece of
code, that would be too complicated for
1:55
another developer to figure out.
2:00
The next set of code comments can be
easily remedied by improving the name of
2:02
the function.
2:06
Naming function is something descriptive,
is a good habit to get into.
2:07
Let's remove the comments, and
2:12
rename the function to something
like check greater than 10.
2:14
This is much more descriptive.
2:18
Inside of this function, is another set
of code comments that aren't necessary.
2:34
Since the code isn't complicated
enough to keep them.
2:39
These are the kinds of comments I might
write when building a project, and
2:43
then remove once it's finished.
2:47
Really the only code comments
I might keep, is for
2:53
the larger go_to_movies function.
2:57
Since this function is larger,
it will make it easier for
3:00
others to know what this function does,
right off the back.
3:03
I just need to remove the first line,
since it's redundant.
3:07
The rest of the comments can go.
3:14
Nice work.
3:31
Let's tackle the next hurdle.
3:33
Back at the top in our check
greater than 10 function,
3:35
we can simplify this logic
into one line like this.
3:40
Magic, right?
3:53
Don't forget that comparisons already
give you a true or false response.
3:54
So you can just return the value instead.
4:01
Comparison logic can be powerful,
but also easily forgotten.
4:04
Next, let's tackle the large code block
inside of the go_to _movies function.
4:09
When it comes to functions,
4:15
it's typically easier to commit to one
task per function for readability.
4:17
Now, there may be times
when this isn't possible,
4:23
but that's where comments can
come into play to help you out.
4:27
Let's break the info
here into two functions.
4:31
One to choose our movie.
4:34
And the second,
to get our snacks Then go_to_movies,
4:39
can utilize these new functions and
print out the message.
4:47
Let's call the first function,
random_movie, and return the chosen movie.
4:52
We can cut.
5:08
And paste, And then return our movie.
5:15
And go to movie,
we can now call this function and
5:22
save the result in a variable
called movie choice.
5:25
The second function can
be called buy_snacks.
5:35
It will need a money parameter.
5:47
Let's move the logic
into this new function,
5:50
and then return our purchase snacks.
5:53
Inside of go_to_movies, we can use
a variable called purchased_snacks.
6:22
To save the value of
the buy_snacks function.
6:32
Don't forget to pass in the money
argument, to your new function.
6:37
Now, this code block is
a lot more readable.
6:43
The last section here,
includes nested for loops.
6:56
Nesting loops may not seem like
a big deal with small data sets.
7:01
But the larger your data set gets,
7:05
the longer this double
looping is going to take.
7:08
When you run this in the console.
7:11
You can see that it first grabs
one of the dictionaries, and
7:19
then each key value pair.
7:24
Can you imagine if you had
to work through 1000 pets,
7:26
this double looping could take a while and
would really slow down your program.
7:30
Let's keep this to one loop and
print out each attribute, Here instead.
7:36
It's common to write more code than you
need, especially when new to programming.
8:08
That's why it's always good to
review your code and old projects.
8:14
Great job tackling some common ways
to make your code more simple.
8:18
You need to sign up for Treehouse in order to download course files.
Sign up