Coding 101 4 (Transcript)
Show Tease, Shannon Morse: Today
on Coding 101, we are going to flowchart your world and check out If Else
statements.
Netcasts you love
- from people you trust. This is TWiT. Bandwidth for Coding
101 is provided by CacheFly at cachefly.com
.
Father
Robert Ballecer: Coding 101 is brought to you
by JIRA, an Atlassian product. JIRA is the project management solution for
teams planning, building and launching great products. To learn more about JIRA and try it free for
30 days, visit atlassian.com/c101.
And by lynda.com. Learn what you want, when you want, with
access to over 2000 high-quality online courses and training videos. All for one low monthly
price. To try it free for 7 days, visit lynda.com/c101. That’s lynda.com/c101.
Father
Robert: Welcome to coding 101. It’s the TWiT show where we give you the knowledge to enter the wonderful world of the
programmer. I’m Father Robert Ballecer -
Shannon
Morse: and I’m Shannon Morse – and today we’re going to take you on
an amazing adventure of coding - with code monkeys.
Fr.
Robert: That’s right, Shannon. Now we know that the key to learning is repetition, right?
Shannon: That’s
right, it is.
Fr.
Robert: The key to learning is repetition, right?
Shannon: That’s
right, it is.
Fr.
Robert: The key to learning is repetition and that’s why we’re
going to show you what Snubs compiled. Snubs, what are our users thinking of?
Shannon: Well, we still have a few people that are
interested in getting started on Xamarin Studio on the
MAC and getting started on Visual Studio Desktop on the Windows pc. So I’m just going to run you through real
quick again how to open up and get started in each of those, and I’m also going
to show you my While loop, because I actually got it to work.
Fr.
Robert: Wow!
Shannon: It’s so
frustrating. I was making it way too
hard for myself, but I got it to work and I am so excited about this!
Fr.
Robert: That’s normally how
it works, right. I mean, you gum it up –
you may get excellent – but wait a minute, take everything away, and just make
it work.
Shannon: It’s
true.
Fr.
Robert: All right, so show
me, show me what we got.
Shannon: I was so angry, too, I was like “nooo, you gotta work for me!” So
first I had my Mac pc, I almost said windows, Mac pc - wait that’s kind of -
Fr.
Robert: Mac Thing! Mac thingey
Shannon: Mac
thing. So when you first open up Xamarin Studio after you have it installed, it’s going to
look like this. We’re going to go to New.
Click on New, to C#, and Console Project, because that’s what we’re working in right now. And then name your console project. I’m going to call it Episode - -
what episode are we on? Five?
Fr.
Robert: Episode Four
Shannon: Four!
Fr.
Robert: Yeah, now I know that this is an important step, because a
lot of people are finding the trap of picking, like, a template and not that
blank worksheet, and what happens is then the code doesn’t work properly.
Shannon: Yeah and
then yeah, everything is broken. So you
definitely want to make sure that you’re choosing Console Application every time you get into this program. So after
that, you can start writing your own code.
So, the one that I wanted to
show you, and I have it pulled up in my coding 101 doc. So I’m just going to copy and paste it over. There we go - aand paste it. And I’ll put this in the get
hub as well. Okay. So I’m going to
delete a couple of extra little quotes here, because every time you do that . . . .
Fr.
Robert: Yeah, it’s all the rich text stuff you’ve got to get rid of. Yeah, be careful of that folks, because those
quotation marks do count. So if you are
copying and pasting from a rich text editor, anything but notepad essentially,
you’ve got to make sure that those curly quotes don’t make it over.
Shannon: Yes!
Yeah, those are annoying
Shannon: So, if
we take a look at my code, basically up here we have the regular thing that you
see at the beginning of each and every code. We’ll get more into this in the
future, but for now just make sure that it’s there.
So here’s the start of my while
loop. So I started with for, so it’s
a For loop.
Fr.
Robert: For Loop, yep.
Shannon: For
Loop, yes, and then integer is counter = 1, so that’s where I’m going to start
is one, and if the counter is less than or equal to 100, I’m going to make sure
that the counter adds one each time that it does the loop. for (int counter = 1; counter <=
100; counter++)
Fr.
Robert: Right, so going back
to our lesson, that one line is going to initialize the variable that we’re
using as the counter. It’s going to set
the condition, so it’s going to run the loop as long as it’s less than or equal
to 100, and then it sets the step, which is -
Shannon: Yes!
Fr.
Robert: - which is counter
++, or plus one, every time it runs.
Shannon: Yes! And I love that counter ++, it’s so easy. And then down here at the bottom, I just
included this “Console.Write (“The count is currently ___”) so I have
a little space here for the number, so at the end - see it’s just a Write, so
it’s not going to enter down to another line. Down here it says Console.WriteLine(counter). That basically means it’s
going to say “The count is currently whatever the counter number is” and then
after that we use the WriteLine to enter down to another line.
Fr.
Robert: I like what you did there because by using Write on that
first line it leaves the curser on the same line and then after it prints the
number, it drops it down to the next line.
Shannon: Exactly
Fr.
Robert: That’s a very good use of Write and WriteLine.
Shannon: And then
I’m going to go up here and I’m just going to start de-bugging, and if there
are no bugs it will say completed successfully. I pull up my new prompter and it says the count is currently and it starts
at one all the way at the top and it goes down or it adds one at every time up
to 100 right here. At the bottom it
says, “Type enter to exit”. And that’s just the
information right here that I’ve included that just says “type enter to exit”
and it’s in magenta, so that’s how I was able to change the color.
Fr.
Robert: And we got that straight out of Lou’s code. Nice.
Shannon: Yeah
Fr.
Robert: Very cool. Again,
you’ve shown off a very common concept. In fact, Lou told us yesterday that he uses For loops almost exclusively. I said above
60% and the only reason why I’m at 60% is because I deliberately try to use
other loops. For loops tend to be the
most useful.
Shannon: Once
you’ve done it a couple times. This
morning I made it way too complicated for myself, at the beginner language that
I understand. I included an extra Console.Write
in there, and put in some extra user inputs. So I just deleted everything and I gave myself about ten minutes to
think about it and I just said, “You know what? I need to make sure that I’m
making this super easy for myself. I don’t want to make it too complicated.”
Fr.
Robert: Got it. Got it. All right, so
that’s your mac and or course you’re showing Mac code because a lot of our
users But what about on the PC?
Shannon: Yes, so I’m
a little more familiar with the pc, just because I’ve grown up with Windows and
I’ve also been playing a lot more with Visual Studio on the desktop. For windows desktop, that’s the one you want
to make sure you’re downloading, and every time you open this, you’ll go to New
Project, and then choose Visual C# Console Application, then open that and it’s
going to give you the basic information as usual.
So for this one, I copied and
pasted the exact same code that I was using for my mac, so you’ll see the same For
loop, the same “Console.Write (“The count
is currently ___”) and Console.WriteLine(counter), and the name of my variable. And then if I hit start on this one - - - - it
shows me the exact same thing!
Fr.
Robert: So, one set of code, two different computers, the same
output.
Shannon: Yes,
exact same output, of course it looks a little different, because one is a mac
and one is a pc, but basically same thing. Now, this did something funny to me, and I don’t know the answer to this
and maybe somebody out there will. Why
does it open this white window down here, this is so confusing. I don’t know
why it’s doing that.
Fr.
Robert: You know, that might be a good
question to ask our Code Warrior.
Shannon: Yeah, I
agree
Fr.
Robert: He’ll be coming later. At the end of last episode, we asked people to show off some of their
work, right?
Shannon: We did
Fr.
Robert: And you actually had a response.
Shannon: I did,
yes. So I talked about the show on my other show, Hacked 5, and one of my fans
over there introduced me to his ASCII art. So this was on our Google + community, if you haven’t checked that out
yet, go over to Google + and search for coding 101. And this is from Douglas Adams. He made ASCII art with a bunch of WriteLines that basically says “Coding 101 with Shannon
Morse and Father Rob Ballecer”.
Fr.
Robert: Now it wasn’t just that, right? He also gave us his code.
Shannon: He did, he
gave us his code and I love it. And it’s on the second link that we posted. So
this is his code, we’ll put it up and paste it and we’ll include this in the
show notes as well if you want to check it out and do your own drawings. But he
basically just used a whole bunch of WriteLines and a
whole bunch of dashes and parentheses and what not to create this whole line of
his drawing.
Fr.
Robert: Yeah
Shannon: It looks
really cool, it’s not a loop.
Fr.
Robert: It’s not a loop.
Shannon: But it still
uses something that we discussed in episode 2, I believe, to create something
very cool. It’s neat!
Fr.
Robert: Now, the other thing cool thing about that. That didn’t use a For loop – because he wasn’t just doing the same line over and over again. But we
are eventually going to get to a point where we’re going to be able to show you
how using some loops with a raise you’re going to be able to do askii art with just a single function.
Shannon: That’s
cool, I like that idea.
Fr.
Robert: Pretty cool?
Shannon: So you
want to move on to the Ivory Tower.
Fr.
Robert: Let’s jump into the Ivory Tower. Now, Snubs, waaay back in episode - Way back – so long ago – we talked about how we would take
something from the real world and turn it into programming language, remember
that?
Shannon: Yes
Fr.
Robert: Specifically, we were talking about Taking a binary
function and turning it back into decimal – or was it decimal going into binary?
Shannon: We did
both of them
Fr.
Robert: That’s something in the real world that required some sort
of mental process. You had to be able break it down into chunks, right?
Shannon: We had
to do some math
Fr.
Robert: We had to do some math. And Lou wrote us a very nice black box function for us to show off how
that actually works. But now we’re going
to show you how you can write your own decision-making statements.
Shannon: Oh, cool! This will be really handy.
Fr. Robert. Yeah. This is all about the If Else
statements. If you’ve been writing in
Visual Basic, you probably know it as an If Then, I
always call it an If Else. The idea is, it
can choose between a right condition and a wrong condition – a true condition
and a false condition.
Shannon: OOOOH
Fr.
Robert: Which we talked about, right? Relational
expressions.
Shannon: Lou had it in his code from last week.
Fr.
Robert: Yes he did. Now
remember when we talked about relational expressions from last week. We’re talking about those symbols from math
class, remember these?
Shannon: I remember
those.
Fr.
Robert: Less than, greater than, greater than equal to, equal to or
not equal to, correct?
Shannon: Yes
Fr.
Robert: So these are all the relational operators we can use. What will happen, is when you put this inside
of a parentheses, and then put some sort of operand on either side, the condition
will either be true or false.
Shannon: Is the
operand the same as a variable?
Fr.
Robert: No, the operand is just on either side of the operator –
it’s what you do the operation on. Now, we’re
talking about If Then statements.
Shannon: Okay
Fr.
Robert: What an If Then statement will do is, it will look at the
condition and say” If the condition is true, then run this code. If not, then continue on.”
Shannon: OOOH,
okay.
Fr.
Robert: All right, so let’s visualize what this looks like. Again, this is going to be pseudo code. Don’t
try to put this directly into your compiler otherwise it will explode and you
will be mad at me. It looks something
like this. IF – we know when we see
this, we’ve got a condition, right?
Shannon: Yes
Fr.
Robert: If – Then - This - so whatever I write down here, it’s
going to go ahead and write it.
Shannon: Okay
Fr.
Robert: So actually, let’s bring one up here on screen. It’s going to look like, uh, what was my
example, oh yeah that’s right, user input. So I am going to create an integer called user input.
Shannon: Okay
Fr.
Robert: And what that does is that goes ahead and initializes the
memory – it sets aside some memory for user input. Then I’m going to say Console.Write and I’m going to ask it to say “Please enter number”.
Shannon: Okay
Fr.
Robert: And just pretend that I did that without the misspelling.
Shannon: Please
enter number.
Fr.
Robert: Please enter number. Now I need a console read, which we know how to do, Console.Read. And I’m going to go ahead and read into the
user variable that I’ve just created.
Shannon: Ok, okay.
Fr.
Robert: So, user input. Now
I’m going to be able to do operations on this. And the operation that I want to do is an If Then statement. I’m going
to say “If “– now here’s my condition – If user input is less than zero, Then
the Write.Console.Write “Less than zero.”
Shannon: Ah,
okay. Or whatever you want that to say.
Fr.
Robert: Or whatever you want it to say. Oh, my handwriting is absolutely horrible, we really need a better board. But then I could also put an Else statement –
I could Else, and I could put another console statement here saying “Your
number is greater than 0”.
Shannon: Oh, like
an error code.
Fr.
Robert: An error code, right. So, this is a really simply way at
looking at what an If Else does. It
allows me to take an input, and if it’s true, I do one thing, if it’s false, I
do another. Does that make sense?
Shannon: It does. I will have to play with that, because it’s a
bit more complicated.
Fr.
Robert: It’s a bit more complicated and I can use all those
relational operators that I talked about before. So I could use less than, I could use greater
than, less than equal to, greater than equal to, not equal to. They can all fit
inside this condition and let me know whether or not I’m going to run whatever
code I’m going to write in the If Else sections.
Shannon: Let me
review this. First off, we have the user
input. So say I’m doing this on my own
computer. It says, “please enter your
number”, and I put in the number, it’ll be, what, 5, let’s say 5,so then it says read
user input, user input is 5. If 5 is
less than 0, then it would to down to the Else, right?
Fr.
Robert: Well, if 5 is less than zero, it’s going to go ahead and
write. Wait. Let’s say 5 is this. Is 5 less than zero? NO, it’s not, so it’s
not going to write “Less than zero”, it going to go down to the Else and the
Else would be “Greater than zero.”
Shannon: Yes, it
would give me whatever that error is if it was incorrect.
Fr.
Robert: Exactly. So that’s
as simple as we can make the If Else statement. Now before we go crazy on the
code, I want to model this after the real world, because that’s what we did in
episode one, that what really made sense. When we took a problem and we divided in parts so people could
understand it in computer code. So I’m
going to take a problem I think a lot of us have. Snubs, do you have a smart phone?
Shannon: Yes
Fr.
Robert: Wouldn’t it be nice if your smart phone didn’t always ring
when you didn’t want it to?
Shannon: Oh,
yeah.
Fr.
Robert: Yeah.
Shannon: Like
when I’m doing shows.
Fr.
Robert: When you’re doing shows. Okay, that’s a good one. So, how
about this – let’s figure out all the times when you wouldn’t want your phone
to ring. When you are doing your show,
Shannon: When I’m
asleep.
Fr.
Robert: Asleep at night. How
about when you’re driving?
Shannon: When I’m
driving, yeah that’s a good one.
Fr.
Robert: Okay, let’s use those three: in a show, sleeping or
driving. Now, I can’t just go into my
compiler and say if sleeping – don’t ring.
Shannon: It’s not going to know if you’re
sleeping.
Fr.
Robert: Right, but what we can do is we can start to think, okay what
data does the phone have access to? I
can write my code so that it used that data. For example, what sensors do you have on your phone? It has time, calendar
Shannon: Yes, it
has my calendar. Sensors, Oh! It knows
speed. Accelerometer, I think
Fr.
Robert: It has a camera, right?
Shannon: Right,
it has a camera.
Fr.
Robert: Okay, so let’s use those four inputs. We’ve know it has the time, it has the schedule,
it has an accelerometer and it has a camera. I can now write a series of If Then statements to do exactly what we
were talking about. Keeping
that phone from ringing when I don’t want it to. Do you want to see how it’s done?
Shannon: Yes, I
do!
Fr.
Robert: Okay, let’s do it. All right, let’s start with the first example. Okay, the first example
was, when I’m doing the show. Okay, so
we want the phone NOT to ring when doing a show.
So the first thing we’re going
to take a look at is let’s look at the time. We’ll look at time and the schedule. So it’s got my calendar. So the IF
then statement would be: If I’m within the time of a show on the schedule. So I’m going to say If it’s showtime Then Y – Off. Turn off the phone.
Shannon: Okay
Fr.
Robert: If it’s not show time, if it doesn’t find that you have a
show scheduled for the time that it currently is, it’s going to say NO, which
is on. It’s going to be on, it’s going to leave the ringer on. That’s one’s very simple, right?
Shannon: Yeah,
that makes sense.
Fr.
Robert: But let’s look at the second example. Sleeping – I don’t want it to ring when I’m
sleeping.
Shannon: But how
does it know when I’m sleeping?
Fr.
Robert: Exactly, right? Because it’s not just related to time, it’s
not just related to your schedule, because you don’t schedule your sleeping
times. So, I could look at multiple
pieces of data. Let’s look at the first
one. The first one might be time. So, it knows that I only sleep after 10:00. So, is (there’s my If) it after 10:00? If it is after 10, so there’s my YES, I’m
going to hold that off for a second. If
it’s not, then I say go ahead and leave it on. But if it IS after 10, the phone has to do another check. It has to say,
well, it may be after 10, but maybe she’s not asleep yet. So, what other data do I have access to? You said motion, right?
Shannon: camera,
yeah and the motion.
Fr.
Robert: The motion and the camera. Let’s start with the motion. So I
could say okay, If Yes, now I ask, “Has the phone moved in the last 30 minutes”?
If it’s No, if it has not moved in the last 30 minutes, then I know, Oh, okay so
maybe if it’s in one place, so that means continue to the next step. If it has moved in the last 30 minutes, then
I know she is still awake so go ahead and leave it on.
Shannon: Oh,
that’s cool.
Fr.
Robert: But if not, then I need to ask the next thing, let‘s look
at the camera itself. The camera’s going
to say – do you see anything? Is it
light or is it dark? Is there
light? If there’s light, then I say,
well then there is light, so leave it on, because she’s still doing something
even though it’s late at night. If there
is no light, then I turn it off because now I know that it’s after 10:00, I
know that the phone hasn’t moved and I know that the camera isn’t picking up
any light. So therefore she’s asleep, turn off the ringer.
Shannon: That is so cool. That sounds really complicated.
Fr.
Robert: It sounds complicated, but this is how we break down
problems, into something that a computer program can understand. Right, because like I said. You can’t just say turn off phone, but I can
say if you have access to the time, if you have access to the accelerometer, if
you have access to the camera, you can check those three pieces of information,
follow this flow chart and at the end, it will know – do I leave the phone on
or do I turn off the ringer.
Shannon: Of
course this very complicated example would require all that information on your
phone.
Fr.
Robert: It would require all the information on the phone. But this
is just a series of If Else statements, right?
Shannon: Yeah,
that makes sense.
Fr.
Robert: And now let’s just talk about that third example of
driving. You work that out for me. How would I do that?
Shannon: So, if
the car is in motion, if my phone is in motion, turn off the ringer. So that would be Yes it is in motion, so off for the ringer. If it is not in motion, so that would
be that square down at the bottom. If it is not in motion, keep the ringer
on. So that would be a Yes.
Fr.
Robert: Boom, exactly.
Shannon: Could we
say if it’s going far faster than a certain thing?
Fr.
Robert: Sure, why not?
Shannon: Because
if I’m on BART, BART doesn’t drive as fast as if I’m in my car when I’m on the
highway.
Fr.
Robert: Good Point. So, why don’t we say If the motion is detected, so the accelerometer detects motion, then first of all
I know that if doesn’t, go ahead and leave it on. But if it does, okay now check the GPS sensor.
Am I moving faster than 45 mph, because I know the train doesn’t move any faster
than 45 mph.
Shannon: If I’m
on a train, keep it on. Yes. If I’m in my car, it’s going faster, turn it off.
Fr.
Robert: And this is how computer programming works. This is how decision making works.
Shannon: If Else.
Thanks!
Fr.
Robert: All right, folks, now we’re going to be getting to the code
warrior, but before we do that, we’re going to welcome a new sponsor to the
Coding 101.
Shannon: A new
sponsor, ooh!
Fr.
Robert: That’s Atlassian. Have you heard
about Atlassian at all? They are a company that is really dedicated
to helping startups, helping enterprises, helping companies that do coding projects, that do technical projects, that’s their entire
thing, that’s their focus. That is what
their building with JIRA.
Now JIRA is one of the world’s
most popular customizable issue and project management systems. It easily captures
and organizes your workflow so that you can prioritize and take action on
what’s important, while staying up to date with the activity that’s going on
around you. You can easily capture, organize
and prioritize your team’s issues, tasks, features and bugs. Give your team a
simple intuitive interface for collaborating with each other in real time. We’re all about that. We want you to be able to make decisions that
you have to make in real time. They can integrate to a planning document to backlog
your issues and your code repository all on one platform. If you are a coder
you know how important this is, because you don’t want to have to send your
developers to different sites, to different repositories. You want them to have a single pane of glass
that shows them, “This is the project you should be working on”.
JIRA lets you set up any
business process you can imagine so that your team works the way that they
want, and you can define your own issue types to track the information that
matters most to your team. You can stay in the loop with notifications via email,
chat, at mentions or RSS and monitor streams of activities or self-updating reports
and dashboards so that you’re always in the know.
JIRA lets you expand infinitely
in any direction with thousands of JIRA add-ons, including test management,
time tracking project management and hundreds of other uses. If your issues involve code, and here on
Coding 101 all of our issues involve code, JIRA ties everything together from
an initial planning doc to the files and change sets in your code repository. All the way to deployment. In other words, from the
start to the finish.
JIRA’s integration with Git allows teams to follow
their code from development all the way through delivery in one system. And you can even build into JIRA with the
power of Atlassian’s REST API’s.
Jira is flexible and simple
enough for a five-person startup, but powerful and reliable enough for a one
hundred thousand person enterprise. They’re used by over 25000 companies, including putting 70% of the Fortune
100, and even NASA itself, and they sit at the heart of Atlassian’s offerings for managing your entire application development process from concept
to launch.
So here’s what Snubs and I
would like you to do. We want you to go
do atlassian.com/c101 for more information on JIRA. Just check them out. Monthly plans start at $10/month for up to
ten users and you can try it free for thirty days. Remember, atlassian.com/c101 and select “try
it for free”. And we thank Atlassian for their support of Coding 101.
Well Snubs, do you think we’ve
confused the audience enough?
Shannon: Yeah, I
think so.
Fr.
Robert: Yeah I think so. So how about this. Why don’t we welcome our code warrior?
Shannon: Yeah,
let’s do it.
Fr.
Robert: That sounds like a good – Hey Llou can you hear us?
Lou Maresca: Hey guys, how you doing?
Fr.
Robert: It’s Lou Maresca from
Microsoft. How about that? You’re coming back to be our C# Coooode Warrior.
Now Lou, we’re talking all
about If Else statements. We want these decision making buggers to help us
design our code. What do you have for
us?
Lou: Sure. So
last episode, we went over the ski user for some text and printing the text depending
on how many times they wanted it to be printed. And so what we want to try now, is going with the logic that you just
showed, using some If Then statements and some additional logic, we can then
say “Hey do you want to print this on a separate line? Or do you want to keep it on the same line? And
so I think we’ll just really fast - show my screen real quick - We will paste
in the program from last time and go over real quick what that looks like here,
so you’ll see that the first thing os we asked them
what they want to type, we asked them how many times they want to type it.
And then the next thing we want
to do is we want to ask the user if they want it on a separate line or
not. So, same exact thing. Do Console.Write and basically say “hey do you want to write
it on a separate line?” And from there, now we actually again we have to ask
them for it. We do that. So this is the Read line, so they type print
out, then we read the line and then again we get text here. And then we have to convert that to something
we can actually use logic against. We have two options. One, we can do it using
your fancy-schmancy If Then statement. And I do that here
by saying ( We don’t need this right now, it’;s confusing) I can say “hey if this equals Y, then it’s most (I ask them to print
Y or N) if that equals Y, it’s most definitely going to be wanted to be printed
on separate line, so I just set it to true. And I need another variable here that
I can call on separate lines, set it to false.
Shannon: What is
a bool?
Lou: A bool is just another type, it’s
like true or false.
Fr.
Robert: Right. You have an
integer variable, which can only be an integer. Then you have a bool, which can only be true or false. There’s two possibles.
Lou: Another
variable type. So what this is basically doing is, the user types the y or n
and we convert that yes or no text to some kind of a bool,
so we can use it for later. Then we basically just switch around the current
logic that we had. So we had some logic
down here that basically took this – uh, make sure I have the sanitized logic in
here for the number I have up here, get of that. Okay. So then we have some logic in here, where we normally have that For
statement where we loop around and we reapet the
number however many times the user wants, but then we have this If statement, so
we basically say,” if this is true, print it on a different line, but if it’s
false, then write it on the same line”. And
so again, we’re basically just using true or false – real simple logic.
Fr.
Robert: So what you’re doing in this piece of code is you’ve got
that first If statement to look at the y or n entry from the user and all
you’re saying is if it’s yes, then go ahead and set the Boolean variable to say
True, if not set it to say False and then you’re using it again in the loop, saying
“look if they said yes, so if the Boolean expression is true, go ahead and put
it on its own line, if not go ahead and write them one after the other.”
Lou: That’s
right. So we used the If condition, or Boolean logic, twice. We basically said if
this is equal we have to use two equals here, rather than just one equal, that’s
called the Boolean expressions. And we
say hey is this equal to Y, okay, set it to true. And then down here, “If it’s
true, then print it on a separate line.” Exactly what you said. So if we run this. So you say Coding 101, then we say “we print
it 3 times”, then say Y so there’s my separate line. And there we go.
Fr.
Robert: There we go.
Shannon: Run it again when you say no.
Fr.
Robert: Uh-oh, moment of truth, right?
Shannon: Ant it
worked! Now you do have a space in between those – it that
because of the zero in brackets within the parentheses?
Lou: Right,
that little formatter we used here.
Shannon: No what
is that called again?
Lou: It’s
called a formatter, but you can also call it tokenizer,
whichever you one you want, whatever person you’re talking to who might say it
differently. But it’s a formatter, and we’ll put the link in the codes – go
check it out and see how it actually works.
Shannon: Cool.
Fr.
Robert: Now we’ve got a good comment from a member of the chat
room, M5, who says, Padre as an old
fart, my old programs used to run from top to bottom and as sequence, this type
of programming does not seem to work the same way, very confusing. Lou I remember getting this switch from basic
to more object oriented programming. Let’s let the cat out of the bag. We’ve been asking our audience to trust us. Do
you want to describe what goes on when you’re programming with functions and
how they call one another.
Lou: Sure, so
sequential logic and it, there’s a term that they call it, they call it
spaghetti code and what happens it is, over time as you sequentially do things,
you sometimes have to go and do other things at other parts of the program, and
so what ends up happening is you start using Go To statements and these
statements will take you around to different lines in the code, but then you
have to kinda come back and it just becomes all this
mess of code. And so the way they get
around that is two thingShannon: 1)object oriented programming, which
we haven’t gotten to yet, and 2) functions – creating functions where you can basically wrap up
some logic and stick it somewhere else and put a name on it – so put a little
label on it like “generate number”,
something like that, and from then on, in your normal code you can call “generate
number” and it will just do whatever code’s inside that little wrapped up
package that you have over there.
Fr.
Robert: Right, right. It’s
actually a much more efficient way to code, right Shannon, because rather than
having to write out every step that’s possible, I could just say, ”Look if this
happens, call this function, if that happens, call that function. “
Shannon: Yes
Fr.
Robert: If it does this,
then run this sequence – it’s a much more efficient way to code, and it’s a really
good way to reuse code, right Lou?
Lou: That’s
right, exactly. Then you can start
sharing your functions with people and then that’s a small package and then you
can start making object which have a bunch of functions in them and you can
start sharing that with other people and then it becomes much easier to shift
code and reuse code. They call it modulized programming.
Fr.
Robert: Now, Lou, guest 56979 in the chat room also has a really
good question and that is “Lou, what happens if someone types x instead of y or
n?”
Lou: Good
question. My original code actually has
this – again, another sanitizer, where it will basically use a conversion – a
Boolean conversion – and so I’ll show you what that looks like. So, just like we sanitized the number up
here, to say make sure it’s a number, or an integer, we can do the same exact
thing for a bool, where we do down here, we basically
say “use this bool, try to parse it, add the text
that they entered and then we output whether it’s . . . And if it doesn’t parse correctly, say if they
enter an x it’s going to say, “I have no idea what x is”, so it’ll just say
false since it didn’t parse it correctly and then we’re going to put another
put another error statement in here that‘s saying, “What you entered doesn’t
make any sense.” It’s not true or false
or yes or no.
Fr.
Robert: Now I should also note that you could technically use a
series of If Then statements here. You
could say If “y”, and then put within that that If n, then the else is, you
must have entered something else and so therefore I’m just going to . . .
Lou: You
don’t even need to convert it. Right, so we could even do it down here where we
just put this, right? Now it says “if
you entered y, we’ll just use the text and then type it out”, so it’s a little
easier, a little less confusing. But I
like to tend to move toward variables that make more sense in programs, so it’s
a little easier. And this way you can
sanitize with a row it in too if you convert it to
a bool.
Fr.
Robert: You know what, Lou, I want to talk a bit more about this,
because I – you know this, Snubs, because coming from Hacked 5, you understand the danger of a buffer overflow, right?
Shannon: Oh yeah.
Fr.
Robert: I mean that’s essentially how everything gets hacked. And
as a programmer, I know that at the beginning, you just make it want to work. We just want that “Oh, Yay, yippee!”
Shannon: “It
didn’t break!”
Fr.
Robert: “It didn’t break!” But, and Lou I’m going to ask you to explain
this when we get back from this little break, we also have to be careful to
sanitize our input. We need to make sure
that the program’s going to get the input that was expected and that’s
something I think a lot of people get lazy about, right Lou?
Lou: Very
lazy. It happens even in normal companies, to have seen code that’s shift code, or code on the web, where people just
don’t realize they have to check things. They have to verify what people are
entering. They have what they call code injections, sometimes, where users can
inject code in little input to actually remote execute code and it causes problems.
Fr.
Robert: Everyone from the beginning programmer up to the Fortune
5000 company, can have that issue. We call it lazy programming, but honestly it
was probably someone who was just trying to make it work and then just never
went back to make sure they were sanitizing the input.
Now, we’re going to talk about
that, but first I want to take some time to talk about our other sponsor.
Shannon: Yay!
Fr.
Robert: Snubs, have you heard about Lynda?
Shannon: Oh
maybe.
Fr.
Robert: Maybe, just a little
bit? Yeah?
Shannon: They’re
quite nice.
Fr.
Robert: They are nice. They are also the main purveyor of online
knowledge. We love Lynda, because they
are in our game. We want to teach people
and so does Lynda, but the difference is Lynda wants to give you easy to
understand, step-by-step, all range lessons in any bit of knowledge that you
want to attain. Lynda.com is an online learning company that can help anyone learn creative software and business skills to achieve
both personal and professional goals.
With a Lynda.com subscription,
members receive unlimited access to a vast library of current and engaging
tutorials across a wide variety of subjects - everything from creative and software
skills to business negotiation and programming. At Lynda.com, you will learn to code, create and build applications from
the foundations of object-oriented programming in C and C++ to desktop and
mobile apps for today’s popular operating systems. You’ll explore the fundamentals of programming:
building web applications with DOTnet, PHP and MySQL;
and managing data with SQL databases, connecting to cloud services and so much more.
We use lynda.com here on the
show, both in Coding 101 and in Know How, because in trying to achieve the
knowledge we can give to you, sometimes we need a refresher course. So we can
jump into lynda.com and it will refresh my knowledge about C or C++. I can go
back over the lessons and recall the stuff that I’ve forgotten after so many years of disuse. And that’s really one
of the things that I like so much about Lynda.com. They’re not just a place to
learn, they’re a place to re-learn. Now Lynda lets you improve your skills, learn
new software, and keep up with technology. They have over 2000 courses with more added daily. Right now, the popular course topics include
Foundations of Programming, HTML, PHP with MySQL, Objective-C,
Java, JavaScript and WordPress. Their instructors are working professionals at
the top of their fields, and expert teachers. That is so important! You need people who don’t just know the knowledge,
but have actually used it in the real world so they can give you the hints you
need to become a creative, successful professional.
They have high-quality video
production from state-of-the-art studios and, you know Snubs, these aren’t
those homemade videos that we sometimes see. I’m not knocking those people and I think it’s great that they want to
get their content out. But you can’t
look at the same shot for 30 minutes of listening to bad audio and bad
lighting. You need something that was
done right. And that’s what you get with lynda.com. You also get curated course content. Each lynda.com course is carefully structured
so that users can learn from start to finish, or, like I do, jump to a specific
chapter to answer a specific question. You get easy-to-follow videos that can help you find the answers that
you need. And searchable transcripts even let you search within a video to save
you time and find exactly what you’re looking for.
They have courses for all experience
levels, from beginners to intermediate and advanced, covering a wide range of
technical skills, creative techniques, business strategies and more. You can watch from your computer, tablet or
mobile device and you can even switch and pick up the chapter where you left
off. In other words, learn at your own
pace, on your own schedule, on your own time, when you want to.
So here’s what we’d like you to
do, all you coding 101 sorts – huh- it’s only $25 a month to access Lynda’s
entire course library. Or for $37.50 a month you could subscribe to the premium
plan, which includes exercise files that let you follow along with the
instructor’s projects, using the exact same assets. And you can try lynda.com right now with a
free 7-day trial. Visit lynda.com/c101
to access the entire library. That’s
over 2000 courses FREE for 7 days. That’s lynda.com/c101. And we
thank Lynda for their support of Coding 101.
Shannon: Yay!
Fr.
Robert: Now Snubs, we’re talking about sanitization of input.
Shannon: Yeah,
sounds like something I learned in restaurant management courses in
college.
Fr.
Robert: Right, it’s so weird. Sanitize, make sure to clean before
you come back to your coder. Now, Lou, what does it mean to sanitize
your inputs?
Lou: It means to basically do, so like for
instance, you’re asking them for a number, you have to make sure that they are
going to give you a number. If they’re
asking you for a specific piece of text, that you’re not going to put some
crazy funky command in there that potentially can execute on your
application. For instance, if I ask for
a Y or N, I’m going to make sure that there’s Y or N. In this case, I’m doing a comparison saying
“If they entered Y, then set it to true. Otherwise, if they didn’t, then I’m just
going to consider it false. And that’s
all I consider. I don’t consider any
other input here. So if they enter xyz, it’s just going to say “Uh, it’s probably
false,” and move on. And so this way
guarantees that what the user’s entering pertains to what they were asked.
Fr.
Robert: Now we talked about this briefly when we did the section on
variables, right? We talked about what could possibly happen if you didn’t
sanitize. But I know there’s a lot of programmers out there who are like “this is just a yes or no, how
could this possibly affect anything?” Lou, do you want to explain why a buffer overflow works?
Lou: Sure. So
each variable has its type and that type has a certain amount of space that it
can handle in memory. And so when you
program, when your compile your app, it only checks to say “okay, the user’s
going to enter in a 32-bit value here”, so whatever that number is, right? So if they start entering things in that
potentially are larger than that value, then the memory space is not enough,
and then the operating system, or the process say, “Aw, I can’t handle this”
and it will crash. And so these are the
types of things that the developer has to make sure they say, “if the user asks a question, they need to type in or enter
within the constraints that I specify.” So that’s why you see a lot of , um,
what they call checking, variable checking, input checking, when you’re typing
in to like a web page that says, you know, you can’t enter text in the phone
number field, that kind of thing, it’s because they want to make sure they
don’t get into that type of trouble.
Fr.
Robert: Right, right. Now, if we’re lucky the program crash. Snubs,
if we’re not lucky and we don’t sanitize the inputs, what could happen?
Shannon: Buffer
overflow!!
Fr.
Robert: Buffer overflow,
right. Which means that, believe it or not, if you have a
really poorly constructed program, someone could actually respond to an input
with code.
Shannon: Hacks!
Fr.
Robert: Hacks! It actually gets written in the memory and gets
executed and suddenly you’re toast.
And that’s the story of how we get . Now Snubs, how about this? Follow me on this. What do you think about
making for a homework assignment? Just ask our code monkeys to go out there,
take any of the code that we’ve given them so far, do a program, then input
something and sanitize it.
Shannon: Yeah, I
think that’s great idea.
Fr.
Robert: Create something that asks for a number and then check that
they’re not giving us letters, that they’re not giving us characters, that
they’re not giving us a number that’s too big, right?
Shannon: Yeah,
absolutely
Fr.
Robert: And you could use any of the tools we’ve already given to
you. You could go really advance and use Lou’s super cheating way of parsing your string,
or you could use a series of IfElse statements to
check the logic.
Shannon: I’m
going to try to do some If Elses.
Fr.
Robert: That’s a great exercise, because it gets you in the mindset
of – okay – this is step 1, step 2, step3, step 4.
Shannon: Yes,
exactly. You always have to have your ending step if you want your beginning
step to work it seems like, if that makes sense.
Fr.
Robert: Yes. Now Lou, we’re going to come back next time – are you
willing to be our code warrior, again?
Lou: Yeah, definitely. I’d love it.
Shannon: Yay!
Fr.
Robert: Now tell the folks at home where they can find you, I mean
I know you’re all over the TWiT TV network, but if
they wanted to find just Lou, where should they go?
Lou: Sure.
Uh, Twitter.com/@loumm or about.me/loumm
Fr.
Robert: Thank you Lou, you are an awesome code warrior.
Now don’t forget to visit our
show page at TWiT.tv/code and subscribe. Also make sure that you download our episode to any format that you want
and that’s where you find our show notes and the link to our GetHelp, which is what you need if you want to do the
homework.
Shannon: Yay, so
much fun. So that’s over a TWiT.tv/code,
is that correct?
Fr.
Robert: That’s correct.
Shannon: And you
can also find us over at itunes, if you want to be
our icoder. I
think we’re still pretty high over there at itunes,
Fr.
Robert: Top ten, which is weird.
Shannon: Which is pretty exciting. Search us on there as Coding 101 we have our video over there an d audio
as well.
Fr.
Robert: As long as you’re going to follow us on itunes,
why not jump over to our YouTube page at youtube.com/twitcoding101. You’ll be able to find all of our videos, and
if you join that community you’ll find out when we’re going to do our hangouts.
Shannon: Wow,
look at that! We’re almost 2000!
Fr.
Robert: Whoa! And I love our start up screen.
Shannon: So
cool. I’m so proud of us. And we also have a google+ community, if you
want to join that. That’s where I found
the ASCII art from one of our viewers as well. That is over at gplus.to/twitcoding101. If that doesn’t work, I also made a tiny url. That’s tinyurl.com/coding101.
Fr.
Robert: Yeah, we’re having some issues right now with the G+ short
cuts. A lot of them have disappeared, we’re not really sure what’s going on, I think Google hates us.
Shannon: That’s
weird. Or you could just search us on Google plus.
Fr.
Robert: I blame Eric Duckman. Now if you’re not into the G+ crew, you can
also find us on Twitter. That’s actually
a really good way to get a hold of us. I’m @padresj
Shannon: I’m
@Snubs
Fr.
Robert: And finally, you can watch us each week live – we actually
do this Live – on Thursdays at 1:30 p.m.
Shannon: You get
to see us mess up.
Fr.
Robert: You get to see us mess up. If you watch us live, you can
see the pre-show, you can see the post-show, and I can almost guarantee you
that there’s going to be some dancing. And you’re going to see all the bloopers that don’t end up in the
finished product. Yeah.
Shannon: Yeah,
it’s true.
Fr.
Robert: It’s very true
Shannon: So that’s over at live.TWiT.tv and were also on the irc.TWiT.tv
If you want to join in and answer any viewer questions while we are recording
the show.
Fr.
Robert: That’s right. So I think this is the time where I say “I’m
Father Fr. Robert Ballecer”
Shannon: “I’m
Shannon Morse”
Fr Robert and Shannon: End of line