How To Learn Programming

  • By Michael Bryant (Shadow53)
  •  • 
  • 28th Jul 2016
  •  • 
  • 14 min read

This post was originally an answer to a question on Quora:

How can I develop programming skills quickly as a rookie.?

I have never actually programmed before. Like I'm a complete beginner. But I'm good at math and also like doing things that make me think

EDIT: I've gotten some great feedback from the Quora community and have even added and changed a couple of things based on that feedback. Thank you all for the support and helping make this answer better.

I'm going to give you some advice based on my own experience of teaching myself to code. This will be long, so here's a quick outline:

  • Drop the expectation of "quick"
  • Learn a statically typed language
  • Begin with structured learning material
  • Don't be an "IDE programmer"
  • Comment your code
  • Learn to find, read, and create documentation
  • Understand how to use Google in a more advanced way
  • Make a project!
  • Learn a version control system

Drop the expectation of "quick"

Learning to program is like learning a new language (spoken by people, I mean). It takes time, especially if you have never programmed before. If you try to learn "quick", you will miss a lot and more than likely end up writing some code that technically works, but comes out something like this:

Code Quality | XKCD Code Quality 2 | XKCD

In other words, not very good code. Take the time to learn it well in the beginning, and you will find that things are easier down the road.

Learn a statically typed language

Most people will recommend starting with Python or JavaScript, since both are easy to program in. You could do that, but I think both end up teaching you some bad programming habits that will lead to trouble in the future (especially JavaScript. Ugh). Rather, I would start with Java or C#. Unlike Python and JS, Java and C# are "type safe", or "statically typed". This means that you write a function (yes, like a math one) that expects an integer as input and then write code that calls that function. If you call the function with an integer as a parameter, it will compile and you will be fine. Mostly. (Integer overflows are fun). Try to call the function with a boolean (true/false), string ("Anything in quotes, aka words"), or anything that is not an integer, the compiler will report an error.

EDIT: Updated with a correction on static/dynamic typing and strong/weak typing. They are not the same. Static/dynamic typing refers to whether a variable can hold one "type" of variable (static) (i.e. only an int or only a string) or multiple "types" (dynamic - a variable holding an int can later hold a string). Strong/weak typing refers to whether types are enforced (only an int can be used where an int is expected) or coerced (whatever type is provided is converted to the expected type, often with weird results). Thanks to Gustavo Bezerra for catching that. The focus here is on strong and weak typing.

With a language like Perl and JS, which are "loosely/weakly typed", you don't have this compile-time checking. Instead, you could write the same function and pass a boolean value to it somewhere else in the code, only to not realize it until the program randomly breaks. This is because (in JS, anyhow) of type coercion, where the runtime tries to convert the value you have into something you can use.

For example: false + 1 = 1 while true + 1 = 2. !"" (the boolean NOT operator) returns true, while !"hello" = false. To take this further, look at the contents of this text file. No really, look at it. Now paste the contents into your browser's JavaScript console (F12 usually opens it). You'll see this:

"Strange thing"

Type coercion at its finest. Starting with Java or C# will teach you much better habits.

Begin with structured learning material

If you're like me, you'll find most structured material (like textbooks) boring, slow, and generally irritating. "Yes, I get how to do this, can we move on?" But you haven't learned about programming yet. This is more than learning a particular programming language. Learning to program is independent of any language - that's just implementation. If you learn how to program well, you can pretty easily pick up another programming language without too much effort.

Think of it like having just finished Algebra 1 and then deciding you want to learn Calculus on your own. You could do that, but you would have to go back and learn a great number of things from Algebra 2 and Pre-Calculus and, well, you wouldn't have as complete of an understanding as you would if you took the classes.

As far as which resources to use, CodeCademy is generally considered the best place to pick up the basics. It has interactive lessons with an in-browser code editor to make things easier. Following my previous advice on which languages to learn, you can find a link to the CodeCademy course on Java below.

As for learning C#... Probably the best place to learn it is from Microsoft themselves. I am not a big fan of Microsoft (I prefer to run open source products like Linux, LibreOffice, Firefox, etc.) so it says something when I link to them.

Don't be an "IDE programmer"

Here's the thing you need to remember when learning to program, though: Don't be an "IDE Programmer". I'm not saying not to use IDEs - oh, IDE stands for "Integrated Development Environment", basically a code editor with lots of helpful features like code completion, code analysis, etc.

Anyways, IDEs are incredibly useful. It quickens your workflow and helps you keep from making silly mistakes and not catching them. When you are learning, however, don't rely on all of the IDE's features. You should be able to write a program in a basic text editor like Notepad almost as easily as you do in an IDE. If you rely on the IDE so much that you can't live without it.... You don't really know the language. You know the IDE.

In my CS1 class in high school, we learned Java using the Eclipse IDE. My teacher would always say "In Eclipse, you do this. In Eclipse...." - Java is not Eclipse. A Java programmer should be able to use any IDE or even a text editor to write their programs, if needed. Learn the language, not the IDE.

Comment your code

Comment your code. This is something even professional programmers forget to do. Whether it is explaining what some weird line does and why it had to be written that way or documentation explaining what a particular function does, comments help you and other people understand your code. It is tedious work sometimes and you just want to keep on coding away, but when you return to that block of code two weeks later you might not even understand what it does. Comments help explain this.

Going off of that, it is more than comments that help make your code understandable. Variable names should explain their purpose. Function names should summarize what they do. Example:

int n;

What does n represent? It's an integer, but what is it? The number of something? The number (n)ine? This is more clear:

int apples;

Okay, so this is something to do with apples. Is it the number of apples we have, or is it the ID number of apples, and int pears the ID number for pears?

int numApples;

Okay, this is the number of apples we have.

In the same way, we have this:

i(int num);

It's a function called i that takes an integer as a parameter. What does it do, though? I dunno.

increment(int num);

I can look at this function wherever it appears in my code and assume that it increments the number that I pass to it. So I pass in a variable that equals 7, it comes out equaling 8.

Smart names help a lot in documenting and understanding your code.

Learn to find, read, and create documentation

I talked a little about documenting your code above. But before I expand on that, I should quickly talk about finding and reading documentation.

If you can find official documentation for the language you are using, that is best. For instance, here are links to the documentation for Java 8 and C#. Other times you can't find official documentation, such as in the case of JavaScript (ugh). Most people will go to W3Schools, but I found that the best documentation is found at the Mozilla Developer Network. It's just a matter of looking for it.

By learning to read documentation, I don't mean learning how to read it. That's pretty self-explanatory. Rather, learn to actually look at the documentation rather than assuming you know how something is used, especially if it's some function you haven't used before that was suggested by your IDE.

Finally, writing your own documentation. This goes along with commenting your code, but goes further. Writing an "official" documentation can make it so that your IDE recognizes your own code and suggests it to you along with information that you provided, such as the summary of what a function does, what parameters it takes and what they represent, and what it returns, if anything. Again following my previous suggestions, here's how to write documentation for Java and C#.

Understand how to use Google in a more advanced way

Added this section on a suggestion from Michał‚ Brix

Sometimes you are going to have an idea of something you want to do but not know how to write it in code. I don't mean "write a game", but something like "adding 1 to every number in an array". At first I left this out because it is my personal opinion that Google can become a crutch if used too much. I'll get to that later, though.

First of all, knowing the specific terms for something is always helpful. "Add 1 to all numbers in an array" will give you better results than "Add 1 to all numbers in a collection of numbers". In the same way, "Add 1 to all integers in an array" is even more specific (though it works the same for an array of doubles, floats, and other number types :p).

Second, knowing how to use the advanced features of your search engine is helpful. You can usually find these features by searching "{My search engine} advanced search". Some search engines also have a little link that says "Advanced" that you can click to view a form that allows you to do an advanced search without knowing all of the keywords and symbols.

As far as personal tips, aside from using specific terms, the best thing I have found is finding those sites that usually have good information and specifying those sites in the search, such as "Add 1 to all integers in an array stackoverflow" - StackOverflow being a question and answer site that many coders use to ask questions and find answers to questions that have already been asked.

An advanced version of that: if I wanted only results from Quora and Stack Overflow, I could search "add 1 to all integers in an array site:quora.com OR site:stackoverflow.com". The "site:" is used to specify which website to use for results (or a domain - "site:.edu" returns all .edu results) and that uppercase "OR" says to use one or the other. It is necessary, otherwise it will return "Your search did not match any documents". These are some of the cool things you can do with advanced search.

Be wary that you do not come to depend on Stack Overflow and other sites for giving you code that works. Understand what the code does in the context of the original question first and then use it for your own project. Copying and pasting code you don't understand is never a good idea.

Make a project!

The last bit of advice you'll need is to give yourself a project to work on to practice what you have learned. A good tutorial/course will give you multiple small projects to build or even a large one, but honestly those aren't always interesting. Do them while you are still picking up the basics. Trust me, it's a smart thing to do - even if they are boring. But when you have completed the course and have a solid understanding of how the basics work, come up with a project to build that encompasses a little of everything you learned (and maybe a little more) and make it.

There's more to coding than just writing code, though. When you're building a project, you need to know what you plan to make, what it should do, how the user interacts with it, basically everything. Coding is just implementing this plan. Then, when you start, start small. One basic feature. Test it. Add something. Test it. And so on. Do not write everything in one go. Especially on something large enough to call it a "project". There's a good chance you will come up against so many errors that you'll just want to be done.

Don't quit, though. It's hard and it sucks. Trust me, I've made a couple projects of my own. I'm currently working on writing a basic RPG engine in C++ and I'm still trying to figure out why things aren't working. Stick to it though and the feeling at the end of finally fixing it is so worth it.

This is why I suggest making your own project, though. By choosing on your own what you will make, you have a greater interest in seeing it through than being told "Go make a Blackjack game". Unless you like Blackjack.

With this project, you'll find that the other things I told you above come into play: static typing will catch a good number of mistakes during compilation. The tutorials you went through should have given you a solid foundation to build your knowledge, which you can work from. Knowing the language and not the IDE will lead to much less confusion about why your program isn't working. Finally, good commenting and documenting practices will lead to code that you can understand when you come back from taking a break on the project. Nothing kills the desire to get things done like having no clue what any of the code does.

Learn a version control system

Bonus advice! Try picking up and using a version control system like Git. GitHub offers free accounts with public repositories (though private means paying a little). It really helps with those big projects.

Hopefully this answer helps you. Given how long it is, if there is anything you are confused on or do not understand (or do not understand why I suggested it), feel free to leave a comment.

Thanks for the A2A!

And to everyone reading this on the blog, feel free to leave a comment on my answer as well, as comments are disabled on this site.