Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
Join us in building a kind, collaborative learning community via our updated Code of Conduct.

How do I ask a good question?

Asking a question that is well-received by the community will get you more upvotes and quicker answers. Here are some tips for asking a successful question.

Be on-topic

Do not ask questions about broken code, hypothetical code, or non-existent code, as such questions will be closed as off-topic.

If you have a programming-related question that does not meet those requirements, then it is not suitable for Code Review. You might be able to ask it elsewhere.

Titling your question

State what your code does in your title, not your main concerns about it. Be descriptive and interesting, and you'll attract more views to your question.

Here are some examples of titles that are unacceptable because they are too generic:

  • How can I make this faster? (Everyone prefers faster code. The (performance) tag can convey this desire.)
  • Simplify this chain of if statements (Applies to too many questions.)
  • Ruby function needs refactoring (Reviewers will decide if it needs refactoring. You don't have to ask for it in the title.)
  • Is this good OOP design? (Title is not distinctive.)
  • Online judge says Time Limit Exceeded (Title tells us nothing beyond what would be conveyed by (programming-challenge) and (time-limit-exceeded) tags.)

Furthermore, you should omit these kinds of superfluous phrases from titles, because these requests are implicit in every Code Review question:

  • Can you critique my …
  • Please help me…
  • … needs improvement
  • Reduce the complexity of…

The norm is to summarize the goal of the code in the title. Some typical titles are:

  • Project Euler 9: finding a Pythagorean triplet
  • Game of Life with a Swing UI
  • View controller for smoking cessation iOS app
  • Validating inputs for a vacation request form

If your code does not have a goal, then it is likely that your question contains hypothetical code, or that you are asking about best practices in general rather than for a review of your code. Such questions are off-topic for Code Review.

Asking your question

You will get more insightful reviews if you not only provide your code, but also give an explanation of what it does. The more detail, the better.

Also, tell us some background information. Why did you write the code? Is it an (interview-question)? Is it (homework), for which you would prefer not to have someone provide a completely rewritten solution? An online (programming-challenge) (in which case, please link to the challenge and include a summary of the challenge)? Are you a (beginner) picking up a new programming language? If you are deliberately reimplementing a common library function as an exercise, you should tag it as (reinventing-the-wheel) so that you can get reviews that are more insightful than "Hey, just use this standard function instead!"

Be sure to embed the code you want reviewed in the question itself; you can leave supporting, but non-essential, code in links to other sites. Strip out sensitive information such as passwords, but do not alter the code so much that it no longer resembles your original work.

Example of a poorly asked question

Can this SQL join be improved?

I have a query like this that seems complicated and is taking a long time to run. We're running this query in a loop, so speed is important. I'd like to see if any of you SQL experts can help me simplify it.

SELECT *
    FROM table_a a
        JOIN table_b b
            ON a.col1 = b.col1
            AND a.col2 = b.col2
            AND a.col3 = b.col3
    WHERE a.col1 = ... AND a.col2 = ... AND a.col3 = ...
        AND b.name = '...';

(sql)

Problems with this question include:

  • Title: It states a concern about the code rather than the purpose of the code. It is also too generic to be distinct.
  • Fuzzy language: What does "a query like this" mean? We can't tell which parts of your code are to be taken literally, and which parts are idealized.
  • Hypothetical identifiers: What do table_a and table_b really contain? How about col1, col2, and col3?
  • Lack of detail: Is this running on MySQL? Microsoft SQL Server? How many rows are involved, and how long is it taking? How are the relevant tables defined, and have you put indexes on the columns?

Here's an improved version of the question:

Finding incidents involving a particular server

This query is part of a PHP web application that our company uses internally for logging incidents on our machines. We have about 20000 rows in ServerEventLogs and 3000 rows in Incidents. The following query takes about 800 ms, which is too slow for a responsive UI, since we would usually have about 5 hosts per cluster.

$log_incidents = array();
for ($cluster_hosts as $hostname) {
    $res = mysql_query("SELECT *
        FROM ServerEventLogs log
            JOIN Incidents inc
                ON log.year = inc.year
                AND log.month = inc.month
                AND log.day = inc.day
        WHERE log.year = $year AND log.month = $month AND log.day = $day
            AND inc.hostname = $hostname;");
    $log_incidents[] = logIncidentToJson($res);
}

(php) (mysql) (sql) (join) (datetime)

The added detail makes a big difference in the quality of the advice that reviewers can provide. For example, the fact that col1, col2, and col3 are actually fields in a date can mean that we can take advantage of MySQL's datetime support.