Go Wiki: HowToAsk

A quick introduction

Imagine you want to know how to get from “Old street” to “New street”. You call a random person to get help and you ask “What is the fastest way I can get from Old street 19 to New street 3?”. Would the random person be able to help you?

There are a lot of “New” and “Old” streets in the world, so first he would need to know what country and city you are in. If the street layout is complicated then maybe you need to name some buildings that are near you. Of course because speed is an issue then your means of transportation is also important; are you by foot, bike, car or truck? You can go on foot into places where you can’t get by truck.

Those questions around the immediate question “How do I X?” is the context of the problem, it helps other people orient to the problem. Giving advice without context could mean that the other person will describe the wrong city.

In person it’s easy to ask those questions and can be done in rapid succession, although on the forums this will result in a lot of back-and-forth questions that can be avoided. So, how to properly give the context to the problem?

How to ask a good question

People on the forums have limited time available. So, to speed things up, here is a small template for asking a question that will get better answers and answers quicker:

The gist of the problem you are having.


How did you encounter the problem?
What are you trying to accomplish?
What is the context of the problem?
What are the requirements for the solution?
What are the context-specific constraints/properties?
Your compilable and runnable example on play.golang.org

Other notes about the situation (production/school/playing/learning)

Things to keep in mind:

Of course, don’t worry too much… you can’t answer all possible questions. It’s not needed to write a 4 page essay on the whole thing. People can always ask when something needs clarification.

The story of a bad question

Why is that template necessary? Let’s put you in the shoes of the answerer, and let’s say you get this question:

How do I use the reflect Set method?

From the askers perspective this may look like a straightforward problem. Of course, you have no idea what the asker is trying to do. Is he trying to set a value on a value, struct, array or a map? This means you need to ask the first question: Can you give an example?.

Now the asker will provide an example:

I'm basically trying to do this:
    m := make(map[string]int)
v := reflect.ValueOf(m)
key := reflect.ValueOf("hello")
val := reflect.ValueOf(123)
v.MapIndex(k).Set(val)
print(m)
But it just panics.

Now you’ve got some code that even doesn’t compile. This means you need to copy paste it somewhere and fix the mistakes. Putting it on play.golang.org makes it easier to see the problem (e.g. play.golang.org/p/fCxBlL9V4Y).

The fix is simple; just use SetMapIndex instead. Of course that may not be the full story. The asker now comes back with another problem:

Yay, I've got the reflection working, but it's not fast enough.
How can I make it faster?

What does “faster” mean? What is he trying to do? So you need to ask more about the specifics of the problem. So you ask: What are you trying to accomplish?

I'm trying to implement a set package that can store generic types.

Well, ok, but that still didn’t answer why it needs to be faster. Why is he using reflect for that anyway, when you can use map instead. So you answer: Can’t you just use map instead (e.g. map[type]struct{})? What are you writing the set package for?.

I'm writing a program that goes over multiple sequences and that finds the common elements.
The main use for is for using on nucleotides and it should work on large datasets. But it should also work on proteins, which are represented by another type.
Yes, the map[NucleotideIndex]struct{} works a bit better, but it's still not fast enough, and I would now have to write the same code for proteins.

Finally we have all the information that’s needed. The solution is straightforward: biogo https://code.google.com/p/biogo/ has most of the stuff necessary for handling large datasets and parallelism etc. Also what’s the “NucleotideIndex”. The asker may have found the answer and say: Thanks that’s really nice. but there is a possibility of:

Whoa, that looks nice, but I'm in a bioinformatics course and we need to write that code ourselves. The "NucleotideIndex" is a "struct {Nucleotide Nucleotide; Index int}".

That looks weird, why would you do it that way. Nevertheless we’ve all done something stupid, so… now you can start suggesting something better: Probably dealing with map[Type]map[int]struct{} would be much easier. Because you are dealing with sequences and the set elements are always used in an increasing order you can just store the index in an array e.g. map[Type][]int. Also if the memory starts to become the problem you could make it into a run-length-encoded set…

Now we can have a more meaningful discussion about the (dis)advantages of different set types.

Hopefully after this long example, you probably understand why it’s useful to provide more information. The initial back and forth questions could have been avoided. Proper question could have saved time for the question asker and the answerer.

The initial question could have been:

How do I use the reflect Set method with MapIndex?

I'm trying to set a value on a generic map for a set package. But it just gives a panic when I try to do that. play.golang.org/p/fCxBlL9V4Y

I use the set package in a program that goes over multiple sequences and find all the common elements in a sequence. The sequence elements could be nucleotides or proteins. The program needs to be able to deal with data-sizes upto 1GB.

My current code is available at github.com/...

I'm writing this for a bioinformatics course, so I need to implement it myself.

Summary:

More tips

  • Smart questions
  • Short, Self Contained, Correct (Compilable), Example

    This content is part of the Go Wiki.