Why and how to construct a minimal example? – A case study
Let us look at a recently posted question as an example. The question, as originally posted, did not have a complete example.
The definitions of functions like
beta were not shown.
Thus people could not run this code, and could not see the error on their own computers.
Always make your example code complete. Make sure that when you copy the code to a new notebook, and run it, it illustrates the problem you are asking about.
When the asker of the question was told to make it complete, he posted an extremely long notebook. This is just part of it:
Would you read through so much code to find someone's mistake? Most people wouldn't. This is why it is important to make the example code minimal.
Always make your example code as short as possible. Localize the problem yourself, do not expect others to do it for you!
Creating a minimal example, or localizing the problem, is usually very straightforward. However, it may be very time-consuming. Please do it on your own before asking a question on StackExchange.
How to localize the problem?
Simply keep removing parts of the code that seem irrelevant. Remove some, then check if the problem disappeared. Keep repeating until you find the bit of code that caused the problem.
Let us use the notebook from the linked post as an example. This is the part that causes the error:
Animate necessary to trigger the error? Are you not certain? Remove it to test! In this case the error was still there. In fact it was quite clear from the error message that the error was in
Show came from the
FG[s] function above. The function definition can be dropped, and we can look at
Then we end up with a long
Show expression of the form
a, b, c, ...,
PlotRange -> ..., ImageSize -> ...
c, etc. one by one and see which one makes the error go away. Each of
c, ... depends on several previous definitions in the notebook. Thus removing them can hugely reduce the size of a complete example.
In this case, it turned out to be the
PlotRange option, which did not even depend on the several pages of code that was also included in the notebook. The error was caused by a single line only, which was quite straightforward to find, and could have illustrated the problem alone.
Furthermore, it turned out that this line was not written in the same way in the original post as in the notebook.
The example code in the original question was useless because:
- it was not complete
- it was not identical to the notebook
- it was unnecessarily long
Most importantly, people could not simply copy it into their own notebook and see the error for themselves.
Please do not commit these mistakes and maximize your chances of getting an answer. Always post example code that is complete, minimal (as short as possible), and illustrates the problem clearly.