Transcripts

Coding 101 5 (Transcript)

Shannon Morse: Today on Coding 101 we’re talking if, elses, functions and some really cool examples from you.

Netcasts you love, from people you trust. This is TWIT! Bandwidth for Coding 101 is provided by CacheFly at cachefly.com. Coding 101 is brought to you by lynda.com; learn what you want, when you want with access to over 2000 high quality online courses and training videos. All for 1 low monthly price. To try it free for 7 days visit lynda.com/c101. And by JIRA, an Atlassian product, JIRA is a project management solution for teams planning, building and launching great products. To learn more about JIRA and to try it free for 30 days visit atlassian.com/c101

Somewhat Function(al)

Robert Ballecer: Welcome to Coding 101, it’s the TWIT show that gives you the knowledge to live in the wonderful world of the programmer. I’m Father Robert Ballecer.

Shannon: And I’m Shannon Morse and today we are going to get you learned up on everything so that you can be a Code Monkey.

Fr. Robert: That’s right, we love Code Monkeys but of course we know that the key to being a Code Monkey Shannon, is repetition right?

Shannon: That’s right it is.

Fr. Robert: And what did we talk about last week?

Shannon: I don’t remember… What did we talk about? I’m just kidding.

Fr. Robert: It’s all a blur.

Shannon: Last week we talked a little bit about “If else” statements or functions and then we also talked about relational operators I believe it was called.

Fr. Robert: Yes that’s right because we needed to figure out a way to have certain code execute and other code not.

Shannon: Yes exactly. The first thing I wanted to show you guys was Lou’s example and then I wanted to show you my example for “if else”. YAY fun times! Let’s take a look at my computer and I’m going to open up visual studio, so I’ll go to my start menu and click on visual studio. I found out something interesting. If you just try to open a file, no project is going to be tied to that file; you have to open up a project.

Fr. Robert: One of the things we haven’t talk about but it’s actually kind of important is that any time you’re doing one of these projects or one of these files there are actually a bunch of files that are attached to it, you just don’t see them.

Shannon: Yes exactly. Basically what the project is going to do is it’s going to look at all those different files and it’s going to try and combine them and build them all at the same time and then run them all. So if you have 2 files in the same one and you’re trying to compile both of them separately it’s going to work out so well, so I build 2 different projects for mine. I’m going to go to open project and I’m going to open up Lou’s example over here Episode 5 Lou’s example. What we’re going to see is exactly what he showed us last week. So I didn’t change anything and I clicked start and it’s going to ask me; “what would you like to write?” So I want to write Howdy. How many times? I’m going to say 7. Do you want to print it on different lines? Yes I do. Ops it didn’t put it on separate lines.

Fr. Robert: That’s ok. We’ll troubleshoot that but essentially what the code did was it used a series of “if else” statements and some loops in order to take in input from the user and then decided what the user actually wanted to do.

Shannon: Yes exactly. So then I did my own. I go to open project, this is Snubs example, click there… Ok this is my example. I basically took the same thing that Lou showed us and then I changed just a few things. I played with colors this time because I hadn’t played with that yet. I put in some different colors for ground color, console color, dot red…

Fr. Robert: Snubs is getting crazy!

Shannon: I was just messing around with a couple of different things. I was trying to break them and then I actually did break it a few times. It was a little frustrating but I figured it out. Then I changed this to say “what is your favorite color”. Choose red or blue. You’re going to get this in a second. The next line is in magenta and then it says “what is the air speed velocity of an unladen swallow? It might be 3. Then after that we go into “if” so that changes the text up here that I typed in number 3. It changes that text to be an integer. So if that integer is not shown as text or if it’s not shown as a number then it tells you “your text whatever it says is not a number”. So if I type in unladen swallow up here and put in Birds then it would tell me that “your text “birds” is not a number”.

Fr. Robert: So this is what we were talking about last week where you need to sanitize your inputs because there is always going to be someone who tries to put in something that they shouldn’t put in and if it asks you a number they’re going to put “blue” and that would actually say no you’ve got to give me a number.

Shannon: Blue is not actually a number. So if it does actually work it’ll go ahead and step down to the next part which is where is says ok actually was a number, do you want to print this on separate lines? Say yes or no. If: it says yes then it’s supposed to print on separate lines, else: it will print on the same line. Down here we have what is our “if, else”. So this one says “if” on separate lines, console writes line text. Basically what that means is that it’s going to enter every single time. Else: it will just write the same text on the same line. That is what this bit means right here.

Fr. Robert: Right and again that is the key concept that we had from last time. If an “If, Else” allows you to sort of either do this or that but not both, which is important in programming because otherwise programming becomes useless…you’d just write this huge bit of instruction.

Shannon: They’d just break.

Fr. Robert: Right. You want a way to choose which pieces of code are going to execute and an “if, else” statement is a way to do that.

Shannon: And also I should mention up here…see this bit? That’s my operator. So operators can be anything from less than, greater than, they can be equals, they can be…

Fr. Robert: Greater than or equal, less than or equal to, not equal to. It’s basically what we learned in high school math right?

Shannon: It’s comparing one kind of counter to another – what’s it called?

Fr. Robert: You’re comparing operands to the other.

Shannon: It compares 1 operand to another one and then it tries to tell you whether it’s true or false. So that’s what I did right there. If we step down after this “if, else” then we get the bottom part which is “the count was” and then it counts up how many times it actually printed out on your screen. After that I just left this the same. Console Color is red and then it said type enter to exit. After that it resets the color and read key. We’re done.

Fr. Robert: Something else that I really love that you did in this program is that we never talked about some of the other things you can do with console. We’ve only talked about console read and console write. As you played around with this you found that in the IDE (integrator, developer environment) if you type console it will actually give you a drop down menu of all the other things you can choose and you found…

Shannon: And you can just click on one and that way you don’t type it differently and you don’t type it wrong.

Fr. Robert: You found the one for color.

Shannon: Yes, Lou has showed us color at the end of his but I started playing around with different colors. For example it does not recognize orange but it does recognize red. It doesn’t recognize purple but it does recognize magenta. So there are a couple of strange things going on in there but as I say just play around with it and then break it.

Fr. Robert: Yes break it and then fix it.

Shannon: So I clicked on start and it’s going to show you mine is very colorful. Choose red or blue, blue. Then on here what is the air speed velocity of an unladen swallow. I’m going to say its 7 because I’m into color. Print on separate lines and I’m going to say yes and it prints on separate lines. Blue, blue, the count was 8. Type enter to exit so I hit enter and it exits.

Fr. Robert: Now I know that for some of the folks at home this is going to look like one of those early web sites that put every…. But then again you’re doing exactly what you should be doing; which is as you’re learning a language mess with it.

Shannon: Yes, I’m starting to mess with that, I’m starting to mess with the IDE, Visual studio for windows desktop and that’s pretty interesting. It definitely helps to go into all the different tools and all the different menus and figure out what all of them do. Because if you don’t want something to be set the way that you broke it then just don’t hit save. Just play around with the tool and you’ll be able to figure it out.

Fr. Robert: Again folks, this is one of the things that we’ve been trying to stress since episode 1 and that is that we’re not trying to give you a High school or College class program. We can’t do that in 30 minutes a week. What we do want to do is to show some of the more interesting concepts and then have you go out and as Shannon does so expertly each week, break it. Mess around with the code, jump into the IDE and figure out what else it’ll let you do. Add some color, play with wild loops, try make it crash. That’s really what we do in the world of the code monkey.

Shannon: Speaking of interesting things that our friends have done out there… we got some really cool examples this week. Would you like to see them?

Fr. Robert: Yes I would. I’m all about the homework and I’ve got to tell you that our Coding 101 code monkeys have really stepped up.

Shannon: Yes they have. So I’m going to read the email from this one first off. This one came from Matt and Joshua. They said that their 8 year old son Joshua is really enjoying Coding 101, he wanted to try a tic tac toe game. So he and his father Matt spent the last couple of days coding and debugging a simple console based tic tac toe game in C sharp. He also wanted to send you the code to look at and try so we are going to include that in all of our notes and it’s a very simple 2 player tic tac toe game. It’s just strictly in a console so let’s go ahead and check it out.

Fr. Robert: Hey Shannon, shall we play a game?

Shannon: Let’s play a game.

Fr. Robert: Let’s do it.

Shannon: I go into my console applications and I named this one tic tac toe so that it was easy to find, click on that and then I choose tic tac toe .cs, that’s the file inside my program.

Fr. Robert: By the way if you’re going to do homework for us send us your code so we can show it off!

Shannon: Yes definitely. So I definitely did check this too because it was in an email and I was like “I don’ know what I’m running on my computer”. So I went ahead and looked through the code and it’s totally legit. If you go up to start and then it says please enter player 1’s name. I will enter Snubs. Player 2: Padre. Rows are to the side and columns are up and down. For the row I’m going to choose 1 and for the column I’m also going to choose 1 so it should appear in the middle.

Fr. Robert: 2, 2.

Shannon: Ok so you have a 0 down here. I’m going to choose…oh man you’re making this hard on me. Let’s do 1, 2. Your turn.

Fr. Robert: 0, 2.

Shannon: Alright, and I’m going to choose 1, 0. I win.

Fr. Robert: It looks really simple but the coding that went into this game is actually really good, it’s long and it requires checking of states because you have to know if that person won. That’s actually difficult at the end. This is the sort of stuff that will eventually lead into an app. This is a really good understanding of combining a bunch of different elements into a working piece of code. I love it. Very well done.

Shannon: We also got a bunch of pretty interesting coding examples over at our G+. That’s gplus.to/twitcoding101.

Fr. Robert: I really, really like this one.

Shannon: Both of these are from Square Iguana. The first one is an episode 1 example. This one basically types in his name and then it validates credentials for the user. It looks like a video game. Isn’t that interesting?

Fr. Robert: Good job Square Iguana!

Shannon: Then it gives you some text and then it’ll refresh itself. He also gave us the code for this example and he uses a whole bunch of console colors on here. He used dark cyan on here. He also used a whole bunch of writes on here.

Fr. Robert: See here’s the thing; there is nothing here that we haven’t touched on at least a little bit. This is using console command and loops. So if you know those consult commands and loops and if you’re actually start to break down the real world into discernable pieces as we’ve been asking people to do, you could turn this program into something wonderful.

Shannon: Yes he did turn it into something wonderful.

Fr. Robert: Thank you very much Iguana.

Shannon: Here is the 2nd example. This is for episode 2 with while loops. He said the first thing when taking about while loops is animations and games that he thinks of and for games we need at least some kind of gifs so animation is a cool demonstration for this example. Isn’t that cute? It’s awesome. I love that and it’s a regular operator just like we’ve seen.

Fr. Robert: Again Square Iguana you hit it out of the park, this is fantastic. I throw down the gauntlet I think to everyone else. I think you’ve got to show Square Iguana….

Shannon: I don’t know if I can beat that. I’m still pretty beginner.

Fr. Robert: Well I’m not allowed to, but I will say I was impressed. Given that we haven’t gone into some of the more advanced concepts they did a really good job with what they had.

Shannon: Absolutely and speaking of which we have a sponsor that can help them get learned up on even more C sharp and C plus, plus.

Fr. Robert: We’re all about giving people the knowledge but again we only have 30 minutes a week. So we thought we should have a sponsor on the show, we should have someone who could give our code monkeys everything else that they need and fill in those blanks. Give us the real classroom style education so that they could learn to code.

Shannon: Who is it?

Fr. Robert: 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 creating and software skills to business negotiation and programming. At lynda.com you’ll learn how to code, create and build applications from the foundations of object oriented programming in C and C++ the desk top and mobile apps for today’s most popular operating systems. You’ll explore the fundamentals of programming, building web applications with .net and my sequel, managing data with sequel data bases, connecting to cloud services and much, much, more. Now I’ve been for a long time but what I love about lynda.com especially when I’m brushing up on my c and my c++ skills is that they let me fill in all those little gaps. You know this Shannon. You learn something awhile back but then you kind of need a refresher course and Lynda’s all about that. Lynda helps you review. Now you can improve your skills, learn new software and keep up with technology with lynda.com. They have over 2000 courses with more added daily. Popular courses include: foundations of programming, html, php with my sequel, objective c, java, java script and word press. Instructors are working professors at the top of their fields and expert teachers. Now Snubs let me ask you this. How many times have you gone in to a Youtube video and it’s horrible. The lighting is bad, the audio is horrible and it’s like someone talking to their cell phone and it’s all jittery.

Shannon: Yes that happens on occasion.

Fr. Robert: Do you want to watch it all the way through?

Shannon: No I get bored about a minute in.

Fr. Robert: Exactly, which is sad because there are a lot of really smart people out there but if they’re not doing all the extra… all the lighting and all the camera work that they need to…

Shannon: The production.

Fr. Robert: The production values. Well you’re just not going to watch it all the way through. You don’t have to worry about that with Lynda.com because they do all their work in a studio. A state of the art studio with professionals who are at the top of their fields, expert teachers who’ve actually worked in the field, so they’re not just giving you the theoretical – they know how it works in real life. They have curated course content, each Lynda.com course is carefully structured so that users can learn from start to finish, or as I do, just to a specific chapter to answer a specific question. They have easy to follow videos that can help you find the answers you need and searchable transcripts that let you search with in a video to save you time and find exactly what you’re looking for. No more scrubbing through a video, you just jump to the part that is going to answer your question. Lynda has courses for all levels, beginners, and intermediate and advanced, covering a wide range of technical skills, creative techniques and business strategies and so much more. You can watch from your computer, tablet or mobile device, you can switch and pick up on a chapter where you left off. In other words learn at your own pace, on your own schedule, on your own device at your own time. Now here’s what we want you to do. If you’re serious about becoming a code monkey then try Lynda.com. It’s only 25 dollars a month for access to Lynda’s entire course library or for 37.50 a month you can subscribe to the premium plan which includes the exercise files that you follow along with the instructors projects using the exact same assets. You can try Lynda.com right now with a free 7 day trial. Visit Lynda.com/c101 to access the entire library with over 2000 courses free for 7 days. That’s Lynda.com/c101 and we thank Lynda for their support of coding 101.

Shannon: Functions!! YAY.

Fr. Robert: We’ve been holding back on this for a while because we didn’t want to freak people out. We wanted to give them some of the basics but functions are a big part of programming. Now imagine that you’re writing a program that’s not 1 page, not 100 lines, not 200 lines, not 1000 lines but 1 million lines long.

Shannon: Oh that’s scary!

Fr. Robert: The way that we’ve been working so far is that we’ve been reading left to right, and top to bottom.

Shannon: Just like reading.

Fr. Robert: Now we got loops that lets us do a piece of code over and over again but imagine if you had a piece of code, a program that was so complicated that it had those million lines and you didn’t want to have to map out each and every single possibility, every single time it might run, correct?

Shannon: Right now as far as I know once I get to the end of the code I could just copy and paste the same thing over again but it would be a pain. I don’t want to do that.

Fr. Robert: Let me give you an example, let’s say that there’s a function that turns off the lights in your room. And remember we always want to break down everything in the real world into usable pieces. If I could use this function at any time in my program I would have to every once in a while put in an “if then” statement saying “if they want to turn off the lights now do this” and it’ll just continue. Well I couldn’t do that enough times because it means that them my user would have to get to one of those break points before he could turn the lights off. Why would you do that if you could just write one function, one little piece of code that turns off the lights and then just call it.

Shannon. Oh I see.

Fr. Robert: Write it once, call it many times.

Shannon: So is this kind of like whenever Lou sanitizes his information it always goes down and it tries that little function?

Fr. Robert: Right, so Lou has a function that sanitizes and the reason why he does that is because he’s going to sanitize multiple inputs. He wouldn’t want to have to re-write the entire set of codes that sanitizes the inputs. Instead he writes a generic sanitizing function that says take in this input, make sure it is what I’m expecting and then give it back to me.

Shannon: So it kind of sits outside and waits for you to call it. That’s awesome!

Fr. Robert: The other cool thing about a function is you can write a bunch of modules that are self-contained. You can then copy and paste in to another project. If you’ve just got code in a really complicated project you would never just copy and paste it over because you don’t know what dependencies there are there right? Maybe it’s calling on a variable that was initialized on line 7 and maybe it needs a feature, a piece of code that’s down on line 4000. So you would never just copy it over but if it’s a function it’s already by definition self-contained.

Shannon: Oh I think I get it! I want to see an example though.

Fr. Robert: I think that’s a good idea. What we’ve got here in this really messy… I pre-wrote this because people were complaining hey Padre your handwriting is horrible on the chalkboard… so ignore this for a while, it doesn’t exist. We’re just giving you a basic structure of a function. Whenever I’m using a function in C sharp I need to use this format: visibility, return type, name and perimeter.

Shannon: Say what?

Fr. Robert: Exactly, what are you taking about?

Shannon: What are you talking about Padre?

Fr. Robert: Ok so when we talk about visibility there are different types of functions. I can have a public function, I can have a private function, there are a couple of other weird types that I could use but that basically says which pieces of code can see this and what kind of data is going to be seen from it. Most of the time when I’m writing I’m writing private functions because I don’t want other parts, I only want to call it specifically what I want to call it but you can flip back and forth. For our demonstration, for our code war I believe we’re using all private functions. The return type: If I’m writing a function that just does something. So let’s say I write a function that writes a bunch of text and that’s all it does then I don’t have a return type, it’s not returning any data to me, it’s just doing something on the screen and then ending. So if I’m not doing anything I put void. In other words there’s nothing that is coming back.

Shannon: That makes sense.

Fr. Robert: I’m going to call the function and nothing comes back. Name is just the name of the function. You need something to call the function.

Shannon: And you can name it whatever you want?

Fr. Robert: Yes it’s just like naming a variable, so it’s the same limitations on what you can call it. But you need a name for the specific function and it has to be different that all other functions because anytime you type in that name you’re telling the compiler go back to that function I wrote.  Then finally in parenthesis perimeter, these are the perimeters the data gets passed into the function. For example let’s say I want to write a function that’s just going to loop over and over and write stuff on the screen. This is where I would declare that there’s going to be a string coming into the function and use that string to execute the code.

Shannon: Oh okay.

Fr. Robert: That’s the theoretical model. Here's what it actually looks like. Here's an example of a function that I just drew up into the 3 min.

Shannon: Public is your visibility.

Fr. Robert: Correct.

Shannon: So it's public, and you can see it anywhere basically.

Fr. Robert: Correct.

Shannon: Integer is the return type, integer answer.

Fr. Robert: Right. Actually, I messed up. So it's going to return an integer. So it's going to give you a whole number.

Shannon: Answer is the name and then int a, int b is the perimeter.

Fr. Robert: Right. So it's telling me that I've got a public function that is going to return an integer that is going to be called answer that is going to be using two parameters, integer, a and integer b.

Shannon: Oh, so you're doing a math equation there.

Fr. Robert: I am basically doing a math equation. Inside here, inside the brackets, this is a code of the function, integer c. So it's initializing a new function called c that is equal to A+ B. So whatever integers I put as my function it's got adding them together and returning it as c. Which is an integer right?

Shannon: Right.

Fr. Robert: That’s how I declare a function, that's how I define it. When I'm calling it all I have to do is write this: again, because I'm lame on the chalk board… Okay so I know its answer so I put answer and I'm going to give it, x, and y. So now what I've done is said , please run the function called answer and give it the parameters that I have previously from some other function or some other piece of code called X and Y. That will jump into the function X becomes a Y becomes b and it's combined and returned and answer becomes the answer of these combined.

Shannon: Oh, interesting.

Fr. Robert: Does that make sense?

Shannon: Kind of.

Fr. Robert: We’re going to go more in depth when we get to code warrior, but this is fundamental, you need to know functions if you're going to go into programming, because again, we want the ability to mimic the real world and in the real world, as we've been doing with if then statements you want to break it down to the teeny tiny pieces. What better way than to make some functions.

Shannon: Yay functions! I can't wait to see what Lou says about this.

Fr. Robert: We're going to have to bring the code warrior in here, because he's got the real stuff right?

Shannon: Yes of course, and it's always nice to see what he's writing as well, because it gives you a really generic idea of how to make your own functions.

Fr. Robert: Right. But before we do that Snubs you want to take some time to pay some bills?

Shannon: I hate paying bills. Can we make this easy?

Fr. Robert: How about this, you want to take some time to share the good folks at home how they should be storing their code, how they should be managing their projects?

Shannon: That sounds good.

Fr. Robert: I'm glad to welcome into the show. Another one of these sponsors who is kind of a natural for coding 101 and that is Atlassian. You see, Jira, which is an Atlassian product is one of the world’s most powerful and customizable issue and project management systems. If you want to know what that means it just means that you have a single pane of glass that runs your projects and your enterprise, runs the inter-workings between your colleagues and even controls your code. It easily captures and organizes your workflow so that you can prioritize and take action well staying up-to-date with activity going on around you. Jira lets you capture and organize and prioritize your teams’ issues, tasks, features and bugs. It gives your team a simple, intuitive interface for collaborating with each other in real time. And seamlessly integrate your planning, documents, your backlog, your issues and your code repository, all on one platform. You can set up any business process you can imagine, so that your team works the way that they want. This is not one of those tools that is going to demand that you change your workflow to fit the suite. They want the suite to fit the way that you work. You can define your own issue types to track the information that matters most to your team and stay in the loop with notifications via e-mail, chat at mentions or RSS and monitor streams of activity or self-updating reports and dashboards so that you're always in the know about the data that actually matters to you. Jira which you expand infinitely into any direction with thousands of Jira add-ons including test management, time tracking management and hundreds of other uses. Now is your issues involve code (and if you're a code monkey of course it does) Jira ties everything together from an initial planning dock. The files and change sets in your code repository all the way through to development deployment. Jira’s integration allows teams to follow their code from that development all the way through till a very in one system, and you can even build Jira yourself 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 100,000 person enterprise. They are used by 125,000 companies and 70% of the Fortune 100 and NASA. Jira sits at the heart of Atlassian’s offerings for managing your entire application development process from concept to launch, and they'll set by you, stand with you as your project progresses. So here's what we want you to do we want you to go to Atlassian.com/c101 for more information on Jira and to see if it's right for you. Monthly plans start at $10 an month for up to 10 users and you can try it free for 30 days. Remember Atlassian.com/c101 and select try it for free. We thank Atlassian for their support of Coding 101.

Shannon: Thank you.

Fr. Robert: Hey Stubs, isn't this the time where we add someone to the show?

Shannon: Did you hear Lou?

Fr. Robert: Is there a Lou out there? Is that a code warrior? That is our code warrior!

Shannon: It’s Lou!

Lou Maresca: Hey guys how’s it going?

Shannon: Welcome to the show.

Fr. Robert: In case you are just joining us, Lou is a wonderful employee at Microsoft who has been on many a Twit show, he's been on enterprise Tech, he's been on TN2 he's been on coding 101 and he is a serious code monkey. Lou how are you doing?

Lou: I'm doing fantastic. How are you guys doing?

Shannon: Doing great and today you are code warrior. I'm so excited.

Fr. Robert: Now Lou, we just did a quick thing on functions. Of course we didn't go into everything, we just gave them enough to get running. But you did something wonderful, you took some of the code that we’d already been running in our previous episodes and you put them in the functions.

Lou: Right. First of all I want to say that the examples that were given to the show – those were awesome. They make mine look like junk.  What I wanted to show today is the ability to break up what we’ve already done and – I’ve gotten a question from multiple people actually but 1 specifically, Joshua Walker. He asked if I could on Twitter build some kind of menu system to allow a user to select some functions and the program would go and do that. So what I did was integrate the previous examples that we had; where we had like repeat a word and convert from a number to a binary and I wanted it made so a user could select the different capabilities of a program and then they would go and run them using our function examples. So that’s what I wanted to do today, to show you how to do that and it’s using all the different things that we’ve used so far – console, while loops and if statements and stuff like that.

Fr. Robert: Now Lou before you get into the crazy stuff let me ask you really quick because there’s going to be people who say; well I don’t really need to use a function right? All those functions seem so simple, I just put them in the code. What would be something you would give to the code monkey who is just starting off who says functions are too hard, I don’t really need them, I’m just going to code it as I go.

Lou: You could pretty much just do some simple tasks in a bunch of lines and not have to break it up into functions. It’s completely understandable but just as you said before as programs get larger and bigger and they do lots and lots of things it actually takes a toll on getting confusing on the page. It doesn’t really make sense to keep everything in one spot and so breaking them up in little small what they call functions or functional pieces that allow you to make it more… you give them names and tell what we pass them and what they return and it makes more sense and kind of organizes things a little bit. It’s not only necessarily you looking at that code but it makes more sense to the person who might take it on later. Or even you later, sometimes I go back to my code and it has hundreds of thousands of lines and I say I wonder what this one thing does and luckily the function has a good name and it maybe has some comments and its broken out and it’s functionality allows me to actually determine what it’s doing.

Fr. Robert: My old college professor used to tell me; functions especially when you’re starting off are an unnecessary pain until you come back to a project after a month away or 2-3 away and you realize, I have no idea what my code does. Whereas if you’ve commented it properly and if you’ve properly named functions then you pretty much know exactly what every section of code does.

Lou: That’s right, exactly.

Fr. Robert: Show us what you’ve got.

Lou: Sure. So again back to just the standard console, what I wanted to do first was there is a way to actually grab, to make it more personal, there is a way to grab the user name of the current user in the system. So this is environment variable called user name and I’m just going to store it under user name variable and this allows me to just pass kind of pass that around and have welcome messages and stuff like that. So the first thing I’m going to do is create a function that basically says Hello and prints out a menu. So if I go down here I have a quick little function that says print menu and just like you said it is a void and that void means it doesn’t actually return anything. I’m not a big fan of the void key word but it’s from the old C++ and C days and so it still exists in some of the newer languages but void means it doesn’t really return anything. We have a print menu that is basically telling everybody that hey this is just going to print the menu. Then I’m going to take a name so this way I can actually print the users name in the menu somewhere. In this case I’m saying I’m saying I’m setting the Title, the console window to Coding 101 – this is just the title of the window and then I’m saying Hey welcome to Coding 101. Here I’m actually putting the user name or the name that got entered in there. So I’ve got a function and I’m taking the parameter and using it in my little function again just to get console write line like we’ve used before. Then the rest of it is just a console write line to basically say hey selection A was repeat a word, selection B will convert numbers to binary and selection C will just say hello.

Shannon: So right now we're not actually getting any kind of string user input or just getting a whole bunch of stuff written…

Lou: That’s it. So if I wanted to actually call that and just do that in my program, I would just to like what Padre said. I'm going to go and call the print menu function and it gives me a little -even though I've typed a function it's still going to give me this drop down menu of what I typed and then I'm going to pass in the username that I grabbed from the system. So I run this and I'll get welcome Imaresca to Coding 101 and then I can actually make a menu selection and here in this case, there is nothing there.

Shannon: That’s cool.

Fr. Robert: Now I know there are people in the chat room who are saying, wait a minute, this seems really complicated for something that we did in week one. You're just printing a menu. But again, what you're doing is creating a module so that you never have to write that code again, you can just call it. That one little line that he gave you called print menu you run that line, and that entire section of code will run. It's a really good way to make sure that you are code efficient. Lou will tell you that one of the problems of bloat is that people just keep copying and pasting, rather than making proper functions. Another question that was really good from the chat room was can I save these functions in another file? Yes. And actually, that's something that we purposely didn't talk about, but there are “include statements” in your projects. Those includes refer to files that have functions and you're actually calling those functions, even though they're not on your projects screen.

Shannon: That makes sense. I have a question for you. Lou on the environment that you added in there, can that be anything other than username?

Lou: So there are other things that environment does, environment is kind of generic object that is in C sharp that gives you things like machine name and username, domain names. So a lot of people who do business development or apps that are doing things like specific little tasks on the machine like hands-off type tasks. They use the environment variable to do things; it'll tell you the version of the program. If the product - is the operating is 64-bit, what the version of the OS is. So it gives you some information about the machine and the environment. In this case, I'm just using it to get the username, but I could have put something like this too, and it would work just as well.

Fr. Robert: what I’ve used the environment variable for is you can actually call for the network name of the machine. So that if you are writing say a network aware application you could have the program know, or report back to a central database that; this input comes from this machine, and this input comes from that machine. It’s nice because you don’t have to write that. That would actually be a pain in the butt to write, this is built into the environment.

Lou: I guess I could actually ask the user; hey what’s your name and then I could have also done that too. I just didn’t want to make it too complex in the beginning. But this will at least give us some information to work with. So then once I have my menu now I have to kind of figure out how I’m going to allow the user to basically select one of these items. Now that this is printed out I need to give them the option to select it. So in here I’m going to put a nice “if then” statement basically. The “if then” statement will allow me to basically tell the user to go ahead. I’m going to copy and paste that in there and I’m going to put a variable in here called selection, so whatever they select – right now I’m going to set it to 0. There is a variable called selection and these are all things that are not there yet so I’m going to get rid of those for now.

Fr. Robert: Oh and by the way anyone watching along with the stream, the reason that he’s putting “if” selection equal to 66 – those are actually the values that get returned on a key press.

Lou: Exactly. So I wanted to make this a little simpler than before because we had that fancy function with the Tri-parse and a lot of people said I don’t really understand what that does and so I wanted to make this a little simpler. So as you notice here we’re basically going to the menu, printing the menu and it’ll show A, B and C and then the user has the option to select something and so here I use another function in console called Read key and that means that the next key that you press I’m going to read it and determine what that is.

Fr. Robert: That’s different than what we’ve done before because previously they had to type Y and hit enter. This way it’s just waiting and the very first time you press a key it’s going to give that a value be it 65, 67, 68 etcetera.

Shannon: That’s cool.

Lou: It makes it a little easier because then it’s really easy for me to convert it to a number right away because I can just take that and it’s just a character then rather than a whole string of characters and I convert it right to a number here and I can call it my selection and I store it in my selection variable right here. From then on I add a little space in between. From here on I say ok if it’s A it’s probably going to be character 65 and it’s sc code 65 and then all these characters are defined. If you go look for character codes it’ll tell you what each character is or you can do trial and error. I do trial and error sometimes where I just print out what the code is. This will tell me that this is selection A and that they selected selection A, I’ll do something and then all the way down here I have different selections. A, B and C and then I also have the quick commands, so if they typed Q or if they just hit enter which is 13 or if they hit the escape key. This will allow me to do basically any of those and if you notice it’s actually in a loop. Just a while loop but I put true in here and this is actually considered what we call an infinite loop which means it never ever ends and the way to actually end it is if the user types Q or if they hit the escape key or hit enter. I force a break in the loop. I say break, stop. What it allows me to do then is to stop this infinite loop and it starts over. So the reason I put the loop there is the menu will keep coming up over and over after each function or after each thing that happens.

Fr. Robert: For the more experienced programmers they’re all calling this a main loop. That’s what we normally call it. This is essentially where the program is going execute and then it’s going to call the functions as users hit a key.

Shannon: Ok, got it.

Lou: So if I run this again it’s not really going to do much other than if I just keep hitting things like keys, it’s going to keep repeating the menu over and over again. But if I hit Q it’ll close. It’s as easy as that. So now I have a little function that prints the menu and now let’s add in there for instance the repeat word why where we basically ask the user for a word and they enter it and then we repeat it a certain amount of times. I simplified this a little bit more and what this does again is it’s the exact same function – asks what you want to repeat, they enter what they want to repeat, we ask them how many times and again we have the tri-parse here so I can convert it to a number because they can enter in a whole bunch of long numbers like 1000’s or 100,000’s or that kind of thing. I still want to use tri-parse here but I changed the “if” statement. So only if it is a number – if it’s true, if it actually is a number then I go and actually repeat it. Then I can call this so that I pop back up here and repeat the number I can basically uncomment this and say now if they choose “A” it’s going to repeat it or it’s going to run that function. So I’m going to run this again and I’m going to choose “A” and there you go – it says what word do you want to repeat? Coding, how many times, 10 and there you go, it goes right back to the menu.

Shannon: Oh that’s awesome. So it continually goes back to that while loop.

Fr. Robert: Until you break it. And see he can do that for the rest of those statements; “if” selection “elses” selection “else if” selection. Each time you have one of those he’s got that function right next to it. So if you press a key it will return a value. It will match those up against these values and if it matches that value that particular value of the function will run.

Lou: If they choose something that’s not on this list then it basically ignores it. If you look at it, it says only if it’s that, if it’s this, if it’s that it’s quit. Otherwise just loop again and just keep showing the menu again basically saying we don’t know what you pressed and so we’re going to show you the menu just so you can try again.

Fr. Robert: And you can do that because it’s in a loop. You just leave out that last else. There is no else, it’s only this, this or this. If none of those just keep going back into the loop until you do get something that matches up.

Shannon: Ok.

Lou: There are some things in here that I want to make sure and show. This is an operator and it’s called the “or” operator. It’s basically saying this or this or this. It’s just allowing me to have multiple different selections in here and to put them into one “if else” statement. You can kind of play with that, you can get rid of these and I could put a whole bunch of other “else if’s” in here if I really wanted to and just put breaks in all of them but I didn’t want to repeat myself over and over again so I just put them all together using that operator.

Fr. Robert: Lou, M5 has a good question in the chat room. He said; could you overflow that?

Lou: You could. Over time – well I guess you can’t because basically what happens is that every time you call a function it put another function on the stack. But since it’s returning it’s actually taking a step back and so the stack is very, very, small so it shouldn’t ever overflow. The only time you could probably overflow is if you enter a number that is over an “int”, that’s really over the 32 bit size of an int. In this case it’s in this fancy little tri-parse method that will tell me that it’s too large and then I’ll get a false.

Fr. Robert: You need 4 billion something, something million blah, blah, blah…

Lou: Right, it wouldn’t exactly… But in this case there isn’t really any overflow capability. Even if it is an infinite loop I’m just kind of returning over and over again to this same spot.

Shannon: That’s cool.

Fr. Robert: So do you want to run through the rest of your program really quick?

Lou: Yes sure. So again we have convert to binary and again we’re just going to copy it from our previous example. In fact I didn’t even change the code in this case. I just wrapped it with a function and so if I go down here really quick I’ll just paste it in here so you guys can see. Again it’s the convert to binary function; it’s the exact same code that we had in the example. What I’m going to do now is go up here and uncomment it. Now if they choose option “B” it’ll call that, so I’ll run it again there you go. So if I hit B it says enter a whole number and there is the binary version of it right there. So the last thing is the say Hello. This one I wanted to actually do it but we probably don’t have enough time but what I wanted to do is actually have this create a loop and call the function a whole bunch of times so you can see in the example why you don’t want to have to repeat your code over and over again. But in this case it’s just going to say hello to the user name and you can make it do whatever you want obviously and like Snubs did, she changed the color and stuff so that was kind of cool. But in this case it’ll say hello and if I just hit go it says hello lmaresca in cyan. So you can do whatever you want and then again convert number. So it basically it can do whatever the application allows me to do in these selections.

Fr. Robert: Now Lou this is a lot and I know people are going to freak out when they get to your code because it’s already up on get hub, but we want people to break down each of these pieces into something that they’ve already covered right? You took the programs that we talked about in the first 4 episodes and you put them into functions, so there are actually not a whole lot of super new concepts in this program right?

Lou: If you show my screen one more time, and the nice thing about Visual Studio is that you collapse things and I collapsed everything. If you think about it, there really has a collapsed way of looking at it, and there's really not much here. It just says hello, convert, repeat and print and that's it.

Fr. Robert: You’ve got your main loop in you've got a couple of supporting functions and that's it.

Lou: Right. So it might be a little daunting at first, because there is a lot of stuff because as a program gets larger, more and more lines are going to get in there and it's going to get hard to follow and that's why you need to name things correctly, and sometimes you move things to other files in a gets a little less daunting.

Shannon: I'm starting to feel like I'm playing legends of the red Dragon again on PBS, because you have to all these options like do you want to fight the Dragon to you want to go sleep at the hotel. Do you want to run?

Lou: There’s a whole movement now for those games again actually.

Shannon: I did kill the red Dragon by the way.

Fr. Robert: I did not. Lou you are truly code warrior. Now before you send our code monkeys off into the void, you've got a little bit of homework right, you got a challenge and reply to see them do something with this code.

Lou: What I want to do is actually obviously add some more functions in here. Maybe make the menu a little bit more personal and may be make a little game out of it like some of the examples that came in that allows the user to choose to go and do things and make decisions. Decision-making is kind of a cool thing using if statements and loops. So try that out and see what you can do. And again, maybe use that loop to see how a function can be called over and over again and repeat itself, so you can kind of not have to repeat your code over and over again.

Fr. Robert: Lou Maresca from Microsoft. Thank you so very, very much. We're going to have you on next week, and you want to give this a little taste… Of course, we've got to flush out some of the material we had today, but where do you think we should take our code monkeys next?

Lou: We talked about functions and how to break it out. Maybe we could talk about properties and fields, and other things that will allow you to kind of breakout things that describe things. Obviously I have property as a human. So we could basically described pieces of functions using property and stuff like that.

Fr. Robert: If you want to tell the folks at home where they can find you. I know you have a twitter address and I know that you're here on the twit TV network. So if they watch long enough, they'll see you, but let's say they want to know what goes on in the day of a code warrior. How do they find you?

Lou: You can actually e-mail me LouMM@outlook.com. I'm throwing this out there, and I'm so sure that if the current filters that I have work really well then I'm going to get your e-mail. I’m LouMM at twitter as well.

Fr. Robert: Lou, thank you, thank you so very much. Now don’t forget that if this kind of blew you away that your best bet is to drop by our show page because you’ll find explanations from Stubs complied, you’re going to see the notes from the Ivory Tower and you’re going to get the link to the get hub. Now folks remember the get hub is the code that you should be executing. That’s where you’re going to find the actual code that we used on the show each and every single week. Also if you’re there subscribe. Get it automatically on your iPhone, iPad or Android device, your laptop, desktop, whatever. That’s where you’re going to find some of the best show notes in the business.

Shannon: And hey did you know that we are on iTunes? We’re one of the top listed ones in educational.

Fr. Robert: But we’re not going to stay there long if people don’t tell their friends about Coding 101.

Shannon: So if you want to and you use iTunes definitely go and subscribe there and maybe write us a review too and let us know what you think.

Fr. Robert: Yes and also did you know that we’re not just at our RSS feed and iTunes but you can also find us on Youtube. Go to youtube.com/twitcoding101, you can watch our episodes and you can catch updates about our upcoming Google hangouts. We are approaching the end of our first module which means that we’re going to have to have a big hangout as we approach our wild card episode.

Shannon: Oh I’m so excited! Can I have a beer while we do that?

Fr. Robert: Yes, absolutely.

Shannon: Ok cool! And we’re also on Google Plus again that’s over at gplus.to/twitcoding101 and we have plenty of awesome feedback going on there and I love the code that you guys are sharing so thank you so much.

Fr. Robert: Make sure to put your homework on there because then you get the accolades of your fellow code monkeys. Of course if you’re not into the G+ grove you can also find us on Twitter. That’s probably actually the best way to find me. You can reach me - I’m @padresj so twitter @padresj.

Shannon: And I’m @Snubs. It’s super easy.

Fr. Robert: Did you know Snubs that we’re actually live right now?

Shannon: Oh we are?

Fr. Robert: Yes. Every Thursday we go into some code monkey stuff here @live.twit.tv.

Shannon: Hi everybody. We also have a chat room. It’s over at irc.twit.tv; you can also access it through live.twit.tv also.

Fr. Robert: Yes and you’ve heard us, we pull questions from the chat room as the show goes on. It’s our way of making sure that we connect with the legion of the code monkeys. Well I think that’s about it. Get us next time. I’m Father Robert Ballecer.

Shannon: I’m Shannon Morse.

Fr. Robert: End of Line!

All Transcripts posts