Dark theme

How to ask a good coding question

First: how to ask a bad coding question

One of the problems with writing code is that you can get very close to it - so close that no one understands you when you ask about it. For example, people who teach coding are used to getting emails like this:


From:37352@college.org
To:missJones@college.org
Date:24/3/2020

It doesn't work - how do I fix it?


or...


From:37352@college.org
To:missJones@college.org
Date:24/3/2020

I'm getting an "ArrayIndexOutOfBoundsException" on line 10. Help!


I know, seems crazy right? But people in a panic do it all the time. These aren't great questions because the person getting the mail immediately has to reply and ask:


From:missJones@college.org
To:37352@college.org
Date:24/3/2020


a) Who are you (including surname!)?
b) Which class/year are you (other people may have the same name!)?
c) What are you working on?
d) What language are you using and how are you running your code?
d) What specifically doesn't work?
e) Can you send me your code?


To which the paniced reply is, more often than not, a screenshot of half the code, which can't be run or experimented on because it's a screenshot and you can't copy and paste code from it.

How to ask a good coding question

So, first - and this seems obvious, but very few people do it - say who you are, which class you're in, and what you are working on (as specifically as you can manage - don't just say "that tutorial" or "what you set").

What you say next depends on the problem.

If you have a problem with how to do some coding (not debugging), then send a short example showing what you're trying to do. Making this will help you think through the problem as well.

For example, if I was trying to write some maths software that included a counting loop that skipped odd numbers, I wouldn't send the whole program, because I don't need the person I'm asking to understand the whole program. I can just say:


I'm trying to make a Python loop that skips odd numbers. Is this the right way to do it? It doesn't seem to work.

for i in range(0,10):
   print(i)
   i = i + 2


To which the person can reply:


Good thinking, but, no, unfortunately this won't work; you can't change the loop variable like this in Python (you can in other languages), it just uses the next number supplied by "range" when it loops again; instead tell it to jump up by 2 each time:

for i in range(0,10,2):
   print(i)


They haven't had to wade through 100 lines of code to understand what you're asking. This is sometimes called a Reproducible Example (reprex). It is Minimal (as small as possible without being unreadably weird); Complete (it completely describes the issue); and Reproducible (people can run it to see the issue).

On the other hand, if you have a debugging issue, and you really can't spot the issue, you'll need to send the person the problem code so they can run it. This means sending them enough of the code that they can start it running and see the whole region of code where there is an issue (i.e. the line of code the system is complaining about and any blocks it is in).

In practice, this quite often means sending all the code, along with any info they need to replicate the issue. This may include:

  • any input files it needs to run;
  • any instructions on how to run it;
  • supplying or (better) giving a download location for code libraries you've used;
  • the URL of any website where you've got ideas from or that you're trying to replicate.

Definitely don't send screenshots: no one wants to re-type all the code from a screenshot so they can experiment on it.

Don't send random bits of software where the person can't see the code before running it: people would have to be crazy to run strange and potentially damaging code on their machine.

Of course, once you've got the reply, also write and thank them: if for no other reason that they'll then know you've got and read their email.

You can find out more about asking good questions on StackOverflow, which also has a page on making reprex (this page is significantly influenced by these writeups, so is also cc-by-sa licenced).