You should start with a task that you want to achieve.
From there, you have two choices:
- Ask a question about how to achieve that task in your desired environment. (There probably already is one. How probably will depend on the task. Search first, and if you still go through the Ask form, keep your eye on the dupe-checking lists that appear below the title and along the side.)
- Attempt to figure out the solution yourself, leaning on reference documentation and/or books to guide you.
One way or the other, you will then be implementing a solution.
And then you may get stuck.
You've implemented your solution and can't see anything wrong with it, but it crashes, or it's dog slow, or it produces a different result than what you expected.
Then, you may choose to ask a question about the roadblock you've encountered.
There are several requirements your question must fulfill, including:
- It must be reusable. A question that is useful to you, and you alone, may get closed as “too localized”. Part of Stack Overflow's reason for existence is to be Google food: somebody else who has the same problem after you and Googles it should be able to find your question and use its solution (or, if it has none and they find it themselves, they can provide it).
- It must seek an objective fact. Stack Overflow is not a place for opinion or poll questions. Questions like “What is the best X” or “best way to do X” are not welcome on SO. Remember the green checkmark: it is how you pick the correct answer.
- It must have exactly one correct answer. This rule can sometimes be stretched, particularly over time, but if your question is something like “What are some good resources for learning X?” or “What do you wish you knew about X before you started?”, that's a question more for Quora, not SO.
- It must state the goal, your current method (if you have one), and the obstacle. Explain what you're trying to do, how you've tried to do it, and how you've failed to do it.
- It must end with a question. Not necessarily a question mark, but it should be clear by the end of the question what you're looking for. What is it you need to know? How to make your code not crash? How to make it produce a particular result instead of what it is producing? What the modern replacement for a deprecated API is? If people don't know by the end of the question what knowledge you're seeking, your question may be closed as Not a Real Question.
- It should contain multiple paragraphs. Seriously, nobody wants to read a wall of text.
There is one last step, which is not to post your question.
In writing the question, you may think of other things to investigate. Do them, and add the results to your question—a paragraph (or more) for every line of investigation you pursue. Continue this until you can't think of anything more.
Then, read the question back to yourself, out loud. This may trigger still more realizations, inspirations, or epiphanies, from which you can further expand the question and maybe discover the solution.
Once you have done that, you should have a complete, well-researched, and intriguing question. Post it.
Oh, and if you did find the solution on your own?
Post the question anyway!
Remember, Stack Overflow is meant to be Google food. If you have a question mostly or entirely written, and then you find the solution on your own, you are encouraged to answer your own question! Help others as you seek help for yourself.
Plus, there's always the chance that someone will spot a flaw in your solution and/or have a better one. Everybody wins.