Debug and construct a minimal example
If you have a problem with code that isn't working, you should start by debugging it and breaking it down to the smallest possible program that reproduces the issue.
This may either:
- Just solve your issue, thus you don't ask a question.
- Give you a smaller program to post, which focuses on the specific issue, which is easier to answer and more useful for future visitors.
These are really 2 separate steps, but they can be rather intertwined - sometimes it's just one or / and then the other, but other times you may go back and forth between debugging and breaking it down.
How to create a Minimal, Complete, and Verifiable example.
How to debug small programs.
Grab a pen and paper
This can help you sort out all sorts of problems, from the logic of an algorithm you need to write, to the design of a system, to understanding code you found somewhere, to figuring out why your code isn't working.
Some ideas for what you can write down:
- What will happen at each step in your code
- How each variable changes on each line
- A graph (e.g. UML) containing all the parts you think you'll need, where you can connect different parts, add more details to each part and maybe add or remove parts
- The values of each parameter (and all local variables) for each step of a recursive function call
- In case of needing to write an algorithm, how you'll solve the problem by hand
Some of these can also be done with a debugger or using specialised tools, although, if you're not familiar with such things, a simple pen and paper can be an easier starting point. You can also use a debugger in addition to a pen and paper, to write down what you find with the debugger.
Search (a lot, but more than that)
Type your question into Google.
Then try to think of another way to phrase your question and type that into Google.
Then repeat that a few more times.
Click on every link which even hasn't the smallest chance of answering your question. Spend some time reading every page that seems promising, even if it seems slightly different to your issue or you're having some trouble understanding it.
You should even go to, dare I say, "page 2" of the search results.
Google the final title of your post (and the actual question in the body of your post) - the phrasing you use may change in the process of writing the question, so make sure you actually type that final title and question into Google as a last sanity check before posting your question. It's pretty damning if someone else can easily find an answer to your question by simply throwing your title into Google.
Check the docs and look for code samples
If your program includes a library function or class that might not be working as expected, you should definitely look up the function in the relevant official docs to see whether you're understanding what it's supposed to do correctly, and whether there's maybe an example to help you.
Searching may lead you there, but, if it doesn't, you should make an effort to specifically go there.
Go through a tutorial or read a book
Yes, this might seem like overkill if you just want to solve some small issue, but many times one small issue points to a more fundamental lack of understanding, so we might not be able to explain how to solve the issue sufficiently well for you to understand and/or you may have many more small issues in future which would've all been solved by going through a tutorial or book.
Rule of thumb
You should spend at least an hour on the above, or maybe a few hours (or less if you find the answer that way).
See also:
Stack Overflow question checklist
How much research effort is expected of Stack Overflow users?
site:stackoverflow.com
to your google search.