You are currently browsing the category archive for the ‘Begginners Guide to Programming’ category.

Many people have the ability to cut and paste code from tutorials and forums, its really not that hard to figure out.   What really separates and a terrible program from an excellent one is what’s done before one hits the computer.

Gathering Requirements

One of the most important aspects of creating a high quality computer program is planning.  You will save time, heart ache, and humiliation if you spend time planning your program. The first portion of planning, assuming you already have an idea, is finding out who your target audience is.  Often  your target audience will be yourself which makes it fairly simple to gather requirements, but it can include everyone on the internet, your dad, people that like music, or any number of people.

Once you have discovered your audience you need to find out what they want out of your program. For simplicity lets awesome the program you are working on is a calculator.  It’s fair to assume that your audience would like all the normal functions of an average calculator numbers, addition, subtraction, etc. If your audience for this calculator is extreme nerds, they may provide a set of requirements that involves deep mathematical functions and the ability to have 20 digits of accuracy.  If your audience is the elderly or children, they may provide requirements like a large UI, easy to find buttons, and user help.  All audiences have things in common and they also specific needs, a programmer really can’t please everyone.   If you can gather specific requirement from a specific group than you are well on your way.  There are many techniques for gathering good requirements which I will not go into here, but the first step is realizing you need to do it.

Features

After you have gathered your requirements you will have to decide which ones you want to implement and which ones you don’t.  In business these decisions are often made by deciding how much time, money, and man power you want to expend on each feature and  which feature is more important.  So the most import features go first, but the tier two or three features will often under go cost analysis the ones with lower cost and higher importance will be implemented, the others will not.  There are many metrics that are used in this decision.  For or purposes you will mainly have to decide what you and your audience want the most.  If you are reading this, I am guessing you don’t have much of a budget and you probably don’t have too many people working for you.

When I work on a program this is how I decide which features to implement.  After I have gathered requirements, I create a list of things the program users cannot survive without, and then I create another list of secondary features.  The secondary features are thing that I will only work when I have completed all of the primary features in my program.  When choosing secondary features I like to give them a time value and an importance value.  So, if a feature will take 1 hour to write and has an importance value of 5 out of 10, and another will take too hours and has an importance value of 5,  I will choose the first task.  Which task you choose depends on how much free time you have, your skill set, how you determine importance, and how much value you attribute to any particular feature.

The primary list of features that users can not live without should have little variation from one person to another, every calculator needs to have certain buttons and features.  The list of secondary features will vary widely, because it requires value judgments.

to be continued …  Next up is Feature Planning, Structure, and  Prototyping

Advertisements

Loops and conditionals are the basic building blocks of most if not all programs. They allow you to perform most types of useful functions one requires from a computer. From counting the number of characters someone has used to making sure the user is entering the correct form of input. Many bugs and performance issues have been tracked back to poor use of both of these, but lets just try to get a handle on using them today.

Loops are really the work horse of computer programming, but figuring out how to cleverly utilize a loop is very important. Loops have major ease of use to performance considerations, meaning that if you use ten million loops where you should have used 2 loops your program is going to run really slow. Also try to avoid infinite loops, were you create a loop never ends. This is usually not a huge deal but people have crashed their computers.

Conditional are statements are traditionally known as ” If then else ” statements, but they commonly come in the form of switch statements as well. Try not to think to narrowly when you think of computer programming, conditionals could come in many different forms. Conditional statements have definite pitfalls of their own. It’s awfully easy to make a minor error and change the flow of your program, thereby wrecking it. When you think of a conditional statement you should be visualizing a fork in the road or turning down a street. The point is, you are really taking the program in a different direction when using a conditional, yeah I know not always but often. If you write a program that asks some one the make of their car to determine what tires are available for that model you will have a program flow ( that’s potentially) like this:

Diagram

Let’s take a quick look at a really simple loop from python and then discuss a couple the things you may want to do with it. Python implements one of the most common types of loops the for loop, but they definitely have a bit of a wrinkle compared to other for loops. The setup of a python for loop is for a in b . Let’s we have the variable b which is to a range value.

b = range( 1,11)

If we want to display every number we could walk through that range of values like so.

b=range(1,11)
for a in b:
print a

You can obviously change the values of the range to count in different ways and exchange the print statement for some other logic. Like if you wanted to perform a calculation ten times or pretty much anything you want to do a certain number of times. Another common form of loop is the while loop. It works exactly as you would imagine. while some condition is true/false do this thing. For instance the following code keeps the program running a user has not entered q. It is also a fine example of what to use if statements for.

while user != “q”:
print “what is your lucky number ? ”
user=raw_input
if user==7:
print “That’s Pretty Lame”
else:
print “thanks”

One more use I would like to tell you about is checking user input. Checking user input is really important, because some one stupid will probably use your program. If you are the only using it, you will do something stupid with input and some point, and without checking inputs your program will crash if you enter a letter where a number should be. I will give you the basic idea and I bet you can figure this one out on your own. Create a program that only allows users to enter numbers one through nine. Start with a while loop like the one above and use if statements to set a value that allows the user to exit the while statement.

I really could go on forever about loops and conditionals, because I feel it’s the true heart of programming, but to really understand them you have got to use them! As always this tutorial is meant to be interactive, if you have questions or comments post them in the comments section.

The next post will get away from code a little bit and dig into designing a program and doing what you really want to do with it.

It has taken some time to make it to this point, but all steps to get here were necessary. We are now going to begin with the basic fundamentals of programming. All examples will be in Python, but I will illustrate how these examples easily move into other programming languages, just change syntax. If you haven’t read Steps 2 and 3 please do so now!

First things first if you haven’t already done so get to your python enabled command line, and then open vim. I would suggest the following steps:

  1. mkdir Practice ( Create a new clean directory , you will thank me later)
  2. cd Practice
  3. vim practice1.py

You now have a fresh and clean directory and have created/opened a new python file to begin programming in.

STD Out and STD In

Every programming language ,that I know of, has some way to communicate with standard ( std) in and standard (std) out. Std in and out is basically the text you see on the command line ,because our programs will be command line programs this will be important. If you use an IDE like eclipse, your standard output will be sent to a “console”, which is a window for viewing a command line. Std in and out are usefull for debugging programs and letting the user know whats happening, even if the user does not need to interact with your program. Let’s create a Hello World Program!

First Time Coding and Compiling

In your vim window or other editor type or copy and paste the following:

Print ” Hello World!”

In vim then hit escape “:wq ” to save and exit vim. You should now be on the command line type ” Python Practice.py” . Hello World! should have been displayed to your command line. A common problem some users have is this error “python’ is not recognized as an internal or external command, operable program or batch file.” If you have this error you need to do the following.

  1. Find install Directory of Python
  2. Type the following into your command line window. For windows “set PATH=<Python install Dir>” for linux/unix export PATH=<Python install dir>
  3. Type python , you should a get weird command line type ctrl z and then enter. You are now good to go.

Next important simple proramming function is stdin, Standard in is how you get input from the user. This is true for any command line program. In python to get input from the user you are going to using the following code.

var = input("Enter Your age: ")
print "you entered ", var

Type this into your text editor, save exit and run. Enter your age, hit enter. Woohoo it works. Note to receive integers from the user you will use input(“…”), if you would like to receive text use raw_input(“…”) . Note that var is a variable and you do not have give it a type. This is convenient, but when you start getting a lot of variables be careful you know what data they are holding. Just to make this clear a variable is something that holds a value for you. All variables must have a different name ( there are exceptions we can talk about later), and in python any variable can contain any type of data. So, var = 2 is just as valid as var = ” Monkeys” or var = i. You can keep giving a variable different data if you want, but I wouldn’t recommend it.

One of the main functions utilized in any programming languages is arithmetic! Let’s take a quick look at arithmetic in general. Most programming languages utillize the well known order of operations. If you don’t know the proper order of operations you need to learn it ! Otherwise, programming anything with a math problem will be a nightmare. Learn the order of operations here “Lesson Order of Operations”

Well hopefully you are ready for some arithmetic don’t worry we will keep it light! Let’s use utilize the old code and add on! In the average langauage you can put arithmetic directly into the std out commands. Try the following. Note: Programming languages have comment, comments are used to make notes in your program and they are ignored when you compile and run your program. To comment in python use # and anything after it will be disregarded.

var = input("Enter Your age: ")
print "you entered ", var
rint 1+3  # A number plus another number
print 3*2 # a number multiplied by another number
print var*2  # our variable var with a number in it multiplied by 2
print var*var # var multipled by var !

If you would like to star a calculation, just set a variable equal to the equation like so.

var = (var * 2)/10
print var

I think you have enough tools to play with for now. Try to get acquainted with getting information in and out to the user, manipulating variables,numbers, print statements, and everything you have learned. You will be surprised what you can do with such simple tools.

As always questions are welcomed and are encouraged, the more you ask the more it helps everyone else. So, Post your Questions, comments, and snide remarks in the comments.

For anyone reading part 3 I am assuming you have already read at least part 2: environments . Which means you know that we will be working from the command line and using vim for this series. I would like to re-iterate that the command line seems “hard”, but that is only because it is unfamiliar and does correlate well to real life examples. The command line is actually quite simple to use and users can get by with a only a couple of commands if they want too. The real reason the command line is difficult is because most people try to teach way too much way too fast. Today, we are going to start with navigation and file creation. Then we will look at getting around in vim. Note: If you want to use a mouse insert gvim everywhere I use vim. Gvim actually contains all the same tools as vim, but also has a GUI interface hence the G in Gvim.

To get started if you are using Ubuntu, Click applications -> accessories -> terminal. If you are windows click start -> run and them type cmd. You now have your command line interface running. The first you want to do is type ls. This will list(ls) all of the directories (directories are folders) and files in your current location. Most of the commands I give you should be written down and attached to your monitor for quick reference. ls is a command you will use often especially at first. Now that you see all the directories choose one to go to. It doesn’t matter which. In my ls I saw the following

Desktop Documents Examples Music Pictures Public Templates Videos

Lets go to Documents this is done with the command “cd Documents” , if you want to speed up the process type “cd Do” then hit the tab key. The tab key will auto fill the rest of the name. “cd” literarly stands for change directory. Now that we are in documents lets see what in there with you guessed an ls. Well there is nothing intersting in Documents folder so lets go back to last folder by typing “cd ..” this command will always take you back by one folder. If you want to go back two folders type “cd ../..” . The final command you must have is “pwd”, this command tells what folder you are currently in. If I type pwd it returns:

/home/jon

You should probably fiddle around with these few navigation tools until you get the hang of making your way around the command line. Remember for your quick refernce guide place the following some where visible:

cd (place directory name)

cd .. or cd ../.. cd ../../.. ( go back one or two or three directories )

ls ( list all files and directories in the current directory )

pwd ( Current directory )

Once you are able to hobble around the command line it’s time to jump into vim , once again if you are too afraid of text only type Gvim when I type vim. Let’s create a new directory the needed command is ” mkdir practice ” and then we will enter it with “cd practice”. Now that you are in a new clean directory type “vim practice.txt” . This will simultaneously open vim and create a new file. There is no requirement to enter the file type at the end of a file, but it is very helpful for organization. I suggest you give all files the proper type all the time.

Now, that you are in vim with open file its important to know that vim will not hurt you. If you ever get really messed up hit “esc” until you hear beeping. Let me explain how vim works. You start in command mode, there are many modes and each are reached by typing a letter in. The mode that is most often used is Insert, you reach insert mode by typing “i”. Once again, if you hit some other key and get into a weird mode just hit escape. Once you are in insert mode you should be able to type like a regular word processor. Use the up and down arrows to get around to different lines. If you chose the Gvim option just click with mouse. You should now just spend some time getting comfortable in vim. Type up a paper in it or something. Your most important commands are save and exit. To save your file you must hit “esc” and then type “:w” and then hit the enter key. You have now saved your file. To make it back to the command line hit “esc” and then type “:q” and hit the enter the key. To exit and save hit “esc” and then type “:wq”.

From now on I will assume you know at least this much about vim and unix. I will be dropping little tid bits of knowledge as I go along with the rest of the series. I hope you practice getting around the command line and vim. The next post we will start with a little programming and theory.

As usual this post is meant to be interactive and I would love to see comments. The more comments we get the more everyone gets out of the series!

One of the biggest frustrations in programming can occur because of ones environment. For the vast majority of programming languages including the one I am going to use for this series , linux is the best platform by far. In fact the more that I work in linux the more that I hate programming in windows, and this is pretty much true for anyone that programs regularly. However, you will run into a few people here and there that swear by some IDE or another.

Speaking of IDEs

An IDE is a horrible idea for anyone trying to learn computer programming, because it makes you think you know what you are doing when you actually have no clue. Converseley the other main functions of the IDE are to simplify processes like stack traces, debugging, setting up ant tasks and far more. The vast majority of noobs will have no idea what 95% of the features are in a IDE and will get immenseley confused by them . The other problem with IDE’s are that they aren’t available universally and they don’t have interchangeable functionality. So what happens if you right a c project in visual studio and need to make a few touch ups from a friends computer. You are SOL, because that is the only thing you have ever learned. However, if you have a solid foundation using a plain text editor you can get by pretty well. IDE’s are just a really big detriment to ones learning. You don’t get experience tracking issues, implenting and installing basic compilers, and also becoming a vim master is super cool!

Hardware

A vast majority of people think that computer programmers need a really sweet beefed up rig, not true at all. Especially for the people following this tutorial the iPhone could easily compile the code you will write, atleast at first. So just grab any old computer and use it. You should make sure the computer is equiped with everything you need first like a video card if you are going to be working on a 3d game etc. I also recommend a sound card because you will most likely want to hear some music at some point. The most helpful peices of hardware you can have is a great keyboard and plenty of screen real estate. When I am programming I really like to have atleast two screen to work with, but I can live with one.

Your Environment

Ideally your environment will be some form of linux that is installed on it’s own computer. Linux is highly beneficial for a lot of reasons, but mostly cause it’s already setup with everything you will need for this tutorial. Well, to be exact most distributions are setup with everything you need. If you don’t have a spare computer to install linux on I would suggest setting up a virtual machine using Virtual Box. Virtual box works for Windows, Linux, and Macs. It is very simple to setup for anyone that has any computer experience at all and for those that don’t. Follow Lifehackers Beginners Guide to Setting Up Virtual Machines with Virtual box. Virtual box is really simple and poses zero risk to your native windows implementation. Infact, virtual box is really nice for beginning programmers, because if you write some poor code that lets say locks your processor to 100% usage. You can just kill that virtual machine and start over. If that was your native machine you would need a complete reboot, it could also save you from worse problems.

If you must continue to work from a Windows OS for whatever reason, I would recommend that you install windows services for unix , vim for windows, and of course Python. Python will be the language I will be using during these tutorials. Windows services for unix provides all the tools linux users get from the command line into the windows command line. These tools will be useful forever, but especially for some of the upcoming tutorials. If you don’t know vim is a plain text editor, and it’s really powerful but a bit complicated. Don’t worry I will hold your hand through out your vim adolescence. Python should be really easy to install, but in some cases it is unreachable from the command line. An easy solution, type the following into your command line window set PATH=PATH:C:\Your Python Location\bin . You should be able to “Python” into the cmd line and have something intelligable pop up.

Environments can be a really complicated thing to to deal with and I don’t want to leave anyone behind. So, I am going to wait for some feed back and see if anyone needs help or if I have negated to mention anything. As usual these tutorials are meant to be interactive and will change based on what the readers need. Please Leave any comments you have in the comments, if for some reason you can’t do that Email me at Jonathan.Barnes11@gmail.com. I will post your question and the answer in the comments for you. I hope to see you soon. If you missed step 1 find it here.

The Beginners Guide to Programming: Step 1 Intro

There are a lot of people that would like to get started in the computer programming world; hobbyists, bored people, and the future developers of the world. Many of you don’t want, need, or have a college education based in computer science, and that’s OK. This series is going to be comprised of all the basic building blocks you will need to get started in the computer programming world and hopefully ease you into the process in a pleasant way. Throughout this series I will be giving helpful hints and tips that I have learned over the last four years of being a computer science student and during my internship.

Anyone that is thinking about following this series through to fruition or that is thinking about starting to write computer programs must first remove any of the misconceptions you may have gathered in your life time about computer programming. One of the most common misconceptions that I have noticed is that “Hacking” is a really cool thing to do, and that hacking can do anything. Hacking in practice turns out to be a terrible idea. You can and probably will incur a very large jail sentence if you do hack anything important. There are white hat hackers who are actually a help to the world, but that is a completely different blog post. Generally hacking is the same amount or less exciting than other computer disciplines, and you definitely cannot hack into helicopters via Live Free or Die Hard.

ballmer_peak This is probably a misconception but a hilarious one. If you want to hear a good drinking + computer story ask me about Rome Total War sometime.

Taken from xkcd

Other misconceptions include computer programming being exciting and possibly simulating some sort of 3d virtual world. For some reason Hollywood continues to perpetuate this myth. For most languages the most efficient way to traverse a code base is with a plain text editor like emacs or vim. Windows worked pretty hard to make people believe that every application needs a GUI. In case you don’t know a GUI is a graphical user interface. For many applications a GUI is overkill and a huge waste of time! The worst misconception which seems to pervade even the educated computer scientists among us, is that computer programming is just sitting there and typing out code. This is WRONG! A good programmer takes time before he/she sits down to code to plan out the features, how they will be implemented, what technology is best to use, and a whole lot of other things. I truly believe that you can reduce your entire project time by up to a 1/3 if you do some preliminary planning.

The biggest complaint I hear from people that want to start programming is that they aren’t smart enough. You will quickly find out that you don’t have to be a genius to program things on the computer. There are a lot of very smart people in computer programming, but there are also a lot of below average IQ people programming computers. I can guarantee that if you can read these blog posts you can program some nice beginner level or intermediate projects. It may take more time for some and less for others, but learning to program can be taught.

If you are excited about programming and want to learn I really hope to see you next blog post. This series may start off slow for the tech initiated so if one section isn’t useful for you be sure to check in soon. I am also happy to take questions and requests for future blog posts.