How to Learn Programming with Stack Overflow

Learn programming with Stack Overflow

Stack Overflow (https://stackoverflow.com) is the biggest community of programmers on the Internet. There you can find the answer to almost any question related to programming.

So, if you don’t understand something you see in a book, a video tutorial, documentation, or don’t know how to write code you need, Stack Overflow will be among your first choices to go.

If you’re wondering what these weird words mean, “stack overflow” is the name of a situation in programming that occurs when a software developer accidentally allows her or his code to try using more computer memory than can be used, and because of this their program crashes.

This was an especially common and annoying problem in the past, when programming required that developers literally controlled manually each byte of the precious limited memory.

But even today, when computers and mobile devices have enormous amounts of RAM, and modern programming languages and tools help you avoid this problem, you still may occasionally bump into such a situation.

Anyway, the annoyance and the widespreadness of this problem obviously were the reasons why the term “stack overflow” became a name for a website where programmers can find solutions to different problems and difficulties they face in their work.

Search first

Programming questions on Stack Overflow

By its conception, Stack Overflow is the website where people post their questions and get answers from other visitors of this project.

Usually you don’t even have to ask a question by yourself. Just compose thoughtfully a search query in Google, and you’ll likely find the page or even many pages on Stack Overflow, containing the answer you’re looking for, usually at the top of search results.

Actually, you should never start a new topic on Stack Overflow until you’ve tried to find what you needed using a search engine (Google or the one that Stack Overflow has itself).

When you’re just a beginner programmer, it’s unlikely that you’re the first person with your question. Chances are somebody already posted a similar question on Stack Overflow several years ago and got the answers she or he expected to get. So, respect the community and don’t flood the website with non-unique questions.

If your search brought you nothing and you do want to ask a question on Stack Overflow, read the article How do I ask a good question? first.

Don’t copy and paste code from Stack Overflow

Don't copy and paste code from Stack Overflow

Stack Overflow is a great source of programming theory, as you can find lots of pages explaining various difficult programming concepts there.

But the biggest value of this website is the immense collection of code examples, which are often used as ready-made solutions for thousands of typical tasks programmers have to deal with every day.

There is even a joke that many programmers don’t know how to write code by themselves, since the only thing they do when they work on their projects is copying and pasting code snippets from Stack Overflow, which is known as “Stack Overflow driven development”. Sometimes it’s not even a joke.

But you should never do that. I’m not a lawyer, and what I’m saying is just my opinion, not a legal advice, but, as I understand it, it’s forbidden to simply copy and paste code snippets from Stack Overflow.

At the moment, when this text is being written, all code samples posted on Stack Overflow were licensed under the Creative Commons Attribution Share Alike license. This is basically a free license, but it has two serious restrictions: “Attribution” and “ShareAlike”.

“Attribution” means that every piece of code you take from this website must be accompanied by a set of specific attributes: a visual indication of the fact that this code was copied from Stack Overflow, a hyperlink to the page where you found the code, etc.

Indeed, it doesn’t look like a very big problem. But there’s a second term, which makes everything much more difficult, — “ShareAlike”.

“ShareAlike” means that the code from Stack Overflow that you include in your project must be distributed under the same license as the original code that you found on Stack Overflow.

If you work on a commercial project, your obligation to distribute its code under the Creative Commons Attribution Share Alike license might become a huge problem.

So make it a rule not to copy and paste any code from the Internet (not only from Stack Overflow) if you’re not 100% sure that you have the rights to use it for your purposes.

How to use code snippets from Stack Overflow

How to use code samples from Stack Overflow

Even if you’re ready to fully comply with all the license terms discussed above, don’t blindly copy and paste code examples from Stack Overflow. This website already saved you hours or even days of your time, so spend at least several minutes to understand these lines of code.

It’s always a bad idea to include in your code something that you don’t understand, since it may cause unexpected behavior of your application or even worse consequences, like security holes.

Also, such “programming” won’t improve your knowledge and skills. It will be just an illusion that you can solve some problem now, because if there’s no access to Stack Overflow, you won’t be able to complete the identical task on your own.

If you cannot use Stack Overflow as a source of ready-made code samples, use it as a source of knowledge, like a book. Actually, this is the best approach to Stack Overflow anyway, especially if you want to become a good programmer.

When you find a code snippet that does what you need, read it carefully and understand how it works. Then close your web browser and write your version of the code.

Sometimes, when the answer on Stack Overflow basically quotes the official documentation for some programming language or tool, you have no other options than using exactly the same code you see on the website, since an alternative approach is impossible. In this situation everybody writes this code only this way and there’s no license restrictions on it.

For example, if somebody asked “How to show a small window with a message on a web page?” and the answer was “use an “alert” function: alert(‘This is your message’)”, the only way you can do it is by using this “alert” function, which is a standard function in the web browser.

In other cases, when it’s something more than just several lines of standard code, you can usually create an alternative implementation of the idea that you found expressed in code on Stack Overflow.

Your code will probably have similar general idea, but different inner structure and algorithms, different names for functions and variables, different comments, etc.

In programming almost anything can be done in dozens of different ways, so doing this is much easier than it might sound to you right now.

Some parts of the code from Stack Overflow might be irrelevant and redundant for your application, while the other parts that you need might be missing in the code example. So, your final result might have basically nothing to do with the code that served you as an explanation of how to solve the problem you had.

Don’t rush to use the top rated answer

Each question on Stack Overflow can have several answers, which are arranged according to the number of votes they got from members of this community.

Although it might look like an obvious decision, don’t rush to use the most rated answer. It might already be outdated and some better and more fresh solution might be waiting for you below, just scroll the page down.

For example, if at the top of the section with answers you see the answer with 500 upvotes that was posted in 2012, but below there is another one from 2019 with 345 upvotes, you should definitely consider paying more attention to the fresher option.

Sometimes the solution that would be the best choice in your particular situation has just three or five or even zero upvotes. That’s why you should read the entire page, not only the top answer or answers.

Don’t skip even those with negative numbers of votes. When you see such an answer, you know for sure that the solution proposed there is wrong or irrelevant, because visitors of Stack Overflow voted against it. So, of course, you won’t rely on it when writing your code, but you can learn why it’s incorrect, or test your knowledge to find out whether you’re able to see what’s wrong with it.

Also it’s helpful to read discussions below the answers, as sometimes they reveal flaws in code samples that weren’t obvious to their authors and to many of those who gave their votes for those solutions.

Look beyond Stack Overflow

Stack Overflow is a great website, but at times it’s not enough. If your question is too specific, for example, if it’s about a not very popular web framework, use Google to find specialized forums about this particular topic. Sometimes such websites contain much more detailed, deep, and up-to-date information than Stack Overflow.