Transcript for:
Understanding Variables and Code Comments

hello in this third and final part of variables we're going to talk about initialization of our variables scope and a little bit about comments so let's start off with talking about initialization of variables so variables are this area in memory that hold a value like this and at the moment we can see that my variable location has not been initialized now what really happens in the computer is that maybe it's holding a number you can't have it hold nothing so variables which are initialized still hold a value if you're going to be writing an number into that location it holds something maybe it's zero maybe it's 28. it's just garbage we don't know what's already there it's whatever previously was there from some other use we had of it or what the operating system did with it or who knows what so it's garbage values already in memory here's a brief program that can show us what's going to happen with that now short is another type of number it's like an integer it's just about half the size in memory and so i've got here g1 through g8 i'm declaring all these variables on one line it's not a great way to do it but i didn't want it to take up a whole slide and i'm now going to print them out i'm using set w to print them out here in a nice column four columns two rows and when i run this i get these values on my computer at least i did once and if i ran it again i'd probably get completely different values they may come back up as all zeros or they could come back up as something else it all depends on what has been going on before this happens in my program and so these just hold garbage so uninitialized variables hold garbage you don't know what it is and that's a problem if you start to say increment values so you still create a variable number of pink elephants but you don't initialize it to zero and then you see one you enter increment it by one well you don't know what you've got because you could had garbage there before there might have been a thousand just sitting there in the garbage state so we are always going to initialize our variables you should always initialize your variables give it a value this is true for all i'd say the primitive types like int and double something like string that is an object initializes itself and that's a subtle difference here for the moment we'll just do that initialization on things like ins and doubles now there's in fact two ways you can do an initialization you can either do it with end height equals zero for example or what equals whatever i'm looking for or i can use this second way it's like end width and then i'm putting in the curly braces and this is called uniform initializers using a uniform initializer it turns out there are some subtle benefits to doing it the second way the second way is more modern newer and so try it out get in the habit of using it you'll find very often people will use the first i'm probably going to stick with the first way because it's traditional but both ways work and they do approximately the same thing now c plus plus does not require us to initialize our variables but it is a good and safe practice to do so i'm initializing i need to initialize exactly once in this case if i try to do it twice well here i'm trying to define a variable twice so here i'm defining it twice i'm saying int height equals one and then i'm doing integers one again it's not going to work i can't name it twice it'd be like have two having two children both named bob this will give you a compile error so you can use uniform initializes if you like they're good here's an example of using uniform initializers just to kind of show us what's possible so i've got here a couple of constants that i'm doing again constants all uppercase and there you go so i'm going to use this uniform initializer syntax i could have just said equals 0 or equals 3 but i'm doing it here just to show it's different i can use it down here on my integer variable and triangles is zero in fact i could have done it here as well by setting up the total sides is equal to this but instead i chose equals now in fact i didn't actually i changed my example i don't actually need these brackets but i can put brackets in that doesn't affect matter so i can get rid of the brackets if i wanted to i can switch to universal initializers by putting in a squiggly brace again it doesn't make a difference it's going to in this case be identical one thing i want to point out here is i'm using the set w to give myself a nice lined up output on the output over here one thing i also need to do is because set w on the width notice how these initial values change based on the strings are changing i could try and compute each one of these exactly but that's a bit of a onerous task so instead what i'm going to do is you'll see here is i've lined up my output my output lines up nicely by me padding in a whole bunch of extra spaces to make it all look the same and so now that my output strings line up the same i can tell that my variable here is going to line up the same that's just a little trick i did note that because i'm using set w i have to also include hash include i o manip because set w is a manipulator i've got a hash include that i may have forgotten to highlight that earlier if you want to play with this example it's in uniform initializer.cpp one idea that keeps coming back is scope and we're gonna just touch on it briefly here later on we'll talk a lot more about scope with functions the idea of scope is just the area of the program the region of a program where a variable exists the region of a program where a variable exists so here we go i've got a program i've got height well height starts existing here and it in fact exists all the way down into the end of my program likewise width exists here and it exists all the way to the end of our program but you'll note in my program here i'm trying to use width before i've declared it and this is going to give me an error because i can't use it before i've been declared it's been declared now a review question what type of error would this be we saw before three different types of errors can you name them did you what are they the compile time runtime and logic errors so which is this well it's not going to compile so it's a compile time error we'll see much more on scope later on and finally we're going to talk about comments so good comments in our code will tell you the purpose of the code so here's an example i've got two comments uh double rate 0.12 i'm going to set to 0.12 or maybe an alternate version would be double rate 0.12 set the current tax rate now we can only have one of these statements because we're defining the same variable in each but which comment is better well the first one is like obviously true but completely useless the second one tells us some information we couldn't get by just looking at the code now i would argue though that we're better off to do is name the variable not rate but tax rate or current tax rate if it matters so rather than having a comment sometimes it's better to improve the readability of your code this is a big factor that will come into play later on in your education maybe in second year courses i start telling people to write fewer comments not more to make the code better on its own now a rule of thumb for this class is about every three or four lines of code you should have a comment to explain what you're doing as you get better at coding you need fewer and fewer comments but early on the comments really help us push that forward there's two commenting styles two ways you can kind of spell it in c plus plus the first is good for a single line it's when we put this double backslash or double slash pardon me here the tuple slash starts everything from that point and onward in the code on that line of code is a comment so i can say insert meaningful comment here and then have the rest of my code do something i could have also put the comment down here at the end of the line although that's not very good it's not very readable so we're actually just going to put our comments on lines whole lines if you've got a lot to say you're trying to explain something like okay you probably want maybe the comment is something like you probably thought the code should do this but it actually can't because there's a subtle issue in the tax law that the following is true so we have to code it this way so that the lawyers are happy well that's a long statement in order to do that you can put a multi-line comment in you start it off with a slash star you end it with a star slash and everything in the middle is a comment you can have that on just one line multiple lines however you like it all the way between it everything is a comment now an important thing to do is when you change your code change the comments to match there's nothing like saying set to be the current tax rate and the variable is named la previous tax rate and you're wondering like okay why is this true what's going on here you want to make sure you're updating the comments to match the code otherwise an incorrect comment is worse than no comment at all why why is an incorrect comment worse than no comment at all well it's worse because it lies to you it tells you something that is untrue rather than simply telling you nothing here's a quick uh review question you might want to give it a try to we might look at this in some of the lectures write a program that reads two numbers from the keyboard make it say the number of patients waiting and the numbers or number of worst nurses working in this medical center then calculate and display how many patients each nurse sees and how many are left over assuming each nurse sees an equal number of patients calculate the total number of people that are at the health center calculate and display how long it will take until the nurse any nurse has a break from seeing patients you can do this because now you see how much time all of the nurses are seeing different patients once they've gotten through all those base patients there'll be some remainder patients that some nurses will see but at least one nurse will have a time for a break assume there's 10 minutes per patient you could display the value in minutes or if you wanted to switch it to hours and minutes is a more complicated way of doing it line up all of your outputs on screen so it display everything you calculated to the screen and use good variable names to make it clear what's going on so in this we saw the importance of having variable initialization and we saw the importance of having meaningful comments and how that can improve our code thank you very much happy coding