Wednesday, September 26, 2012

Showing More Solution Code

Show Solution Code

This is something I've been meaning to do forever.

As a start, I've changed the String-1 and Logic-1 sections so that for about 20% of the problems, it will put up a "Show Solution" button once someone has solved the problem. (To see solutions before solving the problem, see the Warmup sections.)

Inside the solution code I've also added "Solution notes" comments -- a couple sentences pointing out what's useful or interesting about those lines of code.

Very often I see submitted solution code that works, but which could be better, sometimes quite a lot better. Hopefully seeing clean solution code will help people see how to write better code. It does seem like a great moment to learn from the better code, since the person working on the problem has all the right context at that moment, thinking about that problem and its code. Sebastian Thrun of Udacity makes a good argument that putting good solutions in front of people at the right moment can be a big win. Heh, of course having some "perfect" solution put in one's face could be a little annoying! Here they've just made this little victory, and now they have to look at the better solution. Well the benefits seem well worth a little annoyance and humiliation!

The Show Solution button will appear for problems with hints, as they always have. I've added hints for several problems, so those will now show solutions too. I've also added the Show Solution button to a bunch of random problems, doing it for roughly every other problem running down the middle of the three columns in the String-1 and Logic-1 sections. The phrase "Post-solution available" down at the bottom of a problem page means the bit is set so it will show the solution once it's solved. All told, about 20% of the problems in those sections have solutions available, so I figure most people will run in to them here and there.

Custom problem authors: you can do this yourself by adding the showpost:true tag to your problem, or by adding a hint.

Clear Code vs. Short Code

Another slight issue is that not everyone agrees what code is best. For the record, I'm aim for code which is the clearest, which is the IMHO the best sort of code for almost all projects. Sometimes there is a solution which is very short but kind of ugly, and I tend to avoid code like that. However some people argue for the most dense code, even if it's hard to read. Anyway, the balance between short code and clear code is a whole separate article I need to write one of these days. Maybe the best approach is to show the clearest solution, and then mention other approaches in the comments. There's probably not a single "best" anyway, just a few good styles.



7 comments:

  1. I think it would be great NOT to have a "Show Solution" button on any problems. I may have to stop using the site with my classes. It is too much of a temptation to click the button, instead of engaging with the problem. I know solutions are available on the web, but this takes more of an effort. Seeing different solutions that their classmates come up with causes great discussions about such things as clear vs short code. I believe the "show Solution" button will take much of this away.

    ReplyDelete
  2. nice useful video/hands on tutorials for beginners in python

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thank you for a GREAT ;) site Nick! I'm just starting with Java programming, and this is the best training i have found.

    ReplyDelete