Wikipedia:Reference desk/How to ask a software question

From Wikipedia, the free encyclopedia
Jump to: navigation, search

The question[edit]

When asking for help with software, new programmers often ask questions which nobody could possibly answer. Here is an example:

I am writing a Perl program, but it doesn't work. I am reading in the names but they come out wrong. Is there a solution?

The question is unanswerable for three reasons:

  1. readers don't know what the goal is
  2. readers don't know what the problem is
  3. readers don't have example source code in which to look for the problem

Goal[edit]

For readers to know what's going wrong, they must first know what right looks like. Tell readers what you're trying to do.

Let's fix the request so that the goal is stated:

I am writing a Perl program, but it doesn't work. The program should read in a list of names, and then print them in alphabetical order. I am reading in the names but they come out wrong. Is there a solution?

Problem[edit]

That's better, but there's still no way for readers to answer the question -- they still don't know what the problem is! You must describe what's going wrong.

Let's fix the request so that the problem is stated:

I am writing a Perl program, but it doesn't work. The program should read in a list of names, and then print them in alphabetical order. I am reading in the names but they come out in the wrong order. In fact, they come out in same order that they started in! Is there a solution?

Example[edit]

That's better, but there's still no way for readers to answer the question, since the names could be in the wrong order for many reasons. You must post an example of source code that exhibits the problem behavior.

Let's fix the request to include an example:

I am writing a Perl program, but it doesn't work. The program should read in a list of names, and then print them in alphabetical order. I am reading in the names but they come out in the wrong order. In fact, they come out in same order that they started in! I have posted my code below. Is there a solution?

 1 #!/usr/bin/perl
 2 
 3 # Read the names into the @people array
 4 while (<>)
 5 {
 6     push @people, $_;
 7 }
 8 
 9 # Now sort the names
10 sort @people;
11 
12 # Now print them out
13 foreach $person (@people)
14 {
15     print $person;
16 }

Perfect! Now the question can be answered by anyone who knows Perl. In addition, now that readers can actually see the program, they are able to provide tips to help the questioner solve the problem on their own next time.

Finally, an answer![edit]

Here's a good answer to the question, which would not have been possible when we started:

The problem is on line 10. Unlike the sort() function in some languages, Perl's sort() does not re-order the list "in-place". It actually makes a copy of the list and then sorts and returns that. Changing line 10 to:

   @people = sort @people;

will fix the problem. This is actually a pretty common mistake, and perl will catch it for you if you turn "warnings" on. To do that, change the first line to:

   #!/usr/bin/perl -w

and perl will say:

Useless use of sort in void context at line 10.

which is a little obscure, but at least tells you to look for trouble on that line. Using -Mdiagnostics instead will give you a very long and helpful explanation. -- PerlyGates 22:39, 18 August 2009 (UTC)

External links[edit]