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
Demystify numbers in code by using constants.
This video doesn't have any notes.
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
Let's take a second look
at what we just wrote here.
0:00
See how I'm using the number one here?
0:03
Right now it's obvious why
we're passing one here.
0:06
That's the range we want
the tower to have but
0:09
down here, one is the amount to
decrease the invaders health by.
0:12
It may be obvious now but if we were
to show this code to other programmers,
0:17
it might not be entirely obvious why we
choose to use one both here and here.
0:22
Some might think that I decided to
have the health decreased by one
0:28
because the range is one.
0:32
That wasn't my reasoning but
that isn't very obvious to the reader.
0:34
Numbers like these
are called magic numbers
0:39
because often time when we see
numbers appear in code like this,
0:42
it isn't very obvious why that
particular number is being used.
0:46
We can make this code more descriptive and
0:50
self-documenting by using
constant variables.
0:52
Constant variables
are variables whose values
0:56
can't change after they've been created.
0:59
Any variable can be declared
constant by using the const keyword.
1:01
We can declare them as
method variables like so.
1:06
If declared in a method,
they can only be used within that method.
1:13
Declaring constant variables
inside a method is fairly rare.
1:18
In some cases it makes
sense to do this but
1:22
what's more common is declaring
them inside classes like so.
1:25
This way they can be used in more places.
1:30
If we were to make it public,
any class could access it.
1:34
We don't need that, so
we'll make it private so
1:38
that it can only be used
inside the tower class.
1:40
Remember, a common convention
in C# is to prefix
1:43
private variables with an underscore.
1:46
We'll do that with the constant
private fields too.
1:49
Now instead of typing one here,
we can type range.
1:52
We can do the same thing
with this magic number.
1:57
Let's call it power.
2:01
So up here we'll type private
const int _power = 1 and
2:02
we'll type power here instead of one.
2:09
Now it's clear that we're passing the
tower's range into the in range of method.
2:19
And we're increasing the health of
the invader being fired upon by
2:23
how powerful the tower is.
2:27
It's also more obvious
that even though range and
2:29
power are both equal to one they
have nothing to do with each other.
2:32
By using constant variables we just made
our code even more self documenting.
2:36
In general when you see numbers in code
it's best practice to create appropriately
2:41
named constant variables for them.
2:45
This is especially important when the same
number is being used in various places.
2:48
Another bonus of doing this is if
you want to change the power or
2:53
the range of the tower, we can just change
the value of the constants, instead of
2:56
changing all the various places in
the code where the magic numbers might be.
3:00
It's been a while since we compiled,
3:05
so let's compile again just to make
sure everything's still legit.
3:06
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