English subtitles

← Cola Wars 1 - Solution

Get Embed Code
13 Languages

Showing Revision 3 created 05/24/2016 by Udacity Robot.

  1. So the first thing that I asked you,
  2. is whether udacicolaVotes is a local
    variable or a global variable.
  3. And you should have determined
    that it was a local variable.
  4. Well, to figure this out, you go to
    where the variable is declared, which is
  5. here, and you know it's declared,
    because the data type is right here.
  6. And you see is it declared Inside of a
    method, meaning between between the two
  7. curly braces here, or
    is it declared outside of the method?
  8. Now, if it's declared
    inside of a method,
  9. that means that it's a local variable,
  10. we can see very clearly that it is
    in fact declared inside of a method.
  11. So it's a local variable.
  12. Well, what does that mean?
  13. We should also be asking ourselves,
  14. if I declared something as
    a local variable, should I have?
  15. Let's think about it in this case.
  16. Here's our phone.
  17. And we've been talking about udacicola,
  18. let's only focus on when we
    click the udacicola button.
  19. When we click this button,
    this method starts.
  20. It sets udacicola votes to zero, and
    then it adds one to udacicolaVotes,
  21. which is zero.
  22. So udacicolaVotes becomes one.
  23. Now, because udacicolaVotes is a local
    variable, at the end of this method,
  24. it actually sort of dies,
    or gets deleted.
  25. The fact udacicolaVotes is trashed, or
    gone, at the end of this method because
  26. it's a local variable,
    causes a couple problems.
  27. So why is this a problem?
  28. Well, you would think that if
    three different people voted for
  29. udacicola by clicking on the button,
    that udacicolaVvotes should be three.
  30. Well, what actually happens is the first
    time you click on the button,udacicola
  31. becomes zero.
  32. It gets one added to it, becoming one.
  33. And then it gets trashed.
  34. It doesn't exist anymore.
  35. The second time that somebody
    clicks on udacicolaVotes,
  36. well udacicolaVotes is
    made all over again.
  37. It's declared again.
  38. Because remember, the first time
    it had essentially gotten trashed.
  39. So it needs to be remade.
  40. And when it's remain, it's set to zero.
  41. Now we add one again to it, but
    remember the value is zero.
  42. So udacicolaVotes is still one, even
    though the button's been clicked twice.
  43. And the same thing happens when
    it's clicked the third time,
  44. it still is one and it's actually
    trashed again at the end of this method.
  45. So that's the first problem.
  46. It's not really saving the state
    between button clicks.
  47. About how many people have voted for
  48. But another problem is down here, this
    code's actually going to throw an error.
  49. And the reasoning for that is because
    remember I said that udacicolaVotes
  50. sort of comes into existence or
    its scope begins right here.
  51. And then at the end of
    this voteudacicola method.
  52. It is trash, it's out of existence.
  53. Which means when I come down here,
    udacicolaVotes doesn't really exist.
  54. More technically, we say that
    this variable is out of scope.
  55. Its scope was here It got declared here,
    and it ended here.
  56. But we're trying to use it down here.
  57. And it can't be used.
  58. Having udacicolaVotes be a local was
    not a correct decision in this case.
  59. And I can summarize why
    with these two points.
  60. It's not saving the vote about between
    button click, and it's not in scope for
  61. the show me votes method.
  62. So this will actually cause errors.
  63. So this will actually not compile.
  64. Okay, so let's look at the other
    variable in this program.