Showing posts with label Coding. Show all posts
Showing posts with label Coding. Show all posts

Monday, September 2, 2013

TopCoding

"An expert is a person who has made all the mistakes that can be made in a very narrow field." -- Niels Bohr
An error-prone physicist
I've been thinking about mastery lately, after reading books like Outliers and discussing Talent is Overrated with my brother.  The basic premise of the latter is that mastery is achieved by many hours of mindful practice of things that are within your grasp.  At any point in time, there are
  1. things you can already do,
  2. things you could do with some practice, and
  3. things you might eventually be able to do but are not within your grasp yet.
Since time is a precious resource, you should avoid either striving for things far outside your ken or sticking with what you already know.  You should try to attain what is within your reach and will cause you to improve.

How do you improve as a programmer?  For the last few months, I've been practicing on TopCoder.  TopCoder is a free programming competition site with a number of areas for competition, though it centers around algorithms.  I had tried it when my grad student advisor first told me about it around ten years ago, but my initial and incorrect impression was that it was only for can't-see-the-forest-for-the-trees hackers who loved writing cryptic and nested for loops and who never had any other plans for the evening.  To be sure, doing the competitions and practice problems is a bit like practicing musical scales, but if you're serious about your craft, then you should be serious about practicing.  By and large, the kinds of problems you can find at TopCoder will challenge you in ways that you might not get at work, and they let you do it in a focused manner.

TopCoder's algorithm problems should help you practice the mapping of a real problem to algorithms and data structures, which is really the heart of what it means to be a programmer.  I suggest starting with Division II problems.  I didn't realize there was a difference at first and was highly discouraged by a Division I problem that eluded me for hours.  Each division has easy, medium, and hard problems which you can do at your leisure.  I've been practicing using Java, since I've been spending more time lately with the language and want to learn it well, though it seems like the top ranked programmers use C or C++.  Make sure you look at other people's code after you've tried a problem.  I've been following a guy named DoublePointer, who also programs in Java, and seems to have done every single problem.

I'll admit that some of the worse problems--or perhaps the problems I like the least--are the ones that admit of no answer besides many nested loops where you have to keep track of multiple indexes.  Solving any problem will involve some measure of implementation and some of algorithm design, but these tend too much towards the former.  In general, however, I think the problems on TopCoder are excellent.  Not only do they let you practice at a level within your grasp, but you get to learn about all kinds of subjects, like lotteries, tire rotation, and Incan numbering systems.  I recommend trying a practice set each week.  That should only take an hour or two.

In the end, mastery is all about doing things you can't quite do, which means making a lot of mistakes.  Practice makes perfect because it's in practice where you get to screw up when no one is looking.  I give many thanks to TopCoder for the chance to mess up and learn from what I've done  wrong.  My TopCoder profile consists entirely of the Bohr quote above and is my inspiration to keep at it when I get frustrated by a tough problem.

Sunday, April 7, 2013

Science and the Nature of Code

"I'll believe it when I see it." -- Ernst Mach
Being a programmer is a lot like being a scientist.

In any program of sufficient complexity--which is really any code that is in use--it quickly becomes impossible to understand it all.  Sure, you might grasp the big picture, but you can't predict how it will behave in every situation, or how it will work when refactored.  Code is always a bit of a mystery.

At the same time, code is reality.  It doesn't matter what people think a program does, what it was specified to do, what it was designed to do, or what it was proven to do.  What matters is what it actually does. 

Natural scientists come up with testable hypotheses that explain observable phenomena so that they can make accurate predictions about the future.  These hypotheses will be modified as a result of future experience.  This is the scientific method, and it doesn't apply to natural phenomena alone. 

Programmers typically start thinking scientifically when something is broken.  We observe something we have not observed before, come up with an explanation, test it, make modifications (to the code or environment), and test again.

Unlike the natural world, it may be easier to understand the essential nature of a program since you created it.  In fact, one of the first things we do when debugging is to look at the code.  Looking at code is not enough, however.  You have to actually see what it does.  The problem is that there is not enough time in the universe to exhaustively determine how code will behave.  Furthermore, test cases cover all the situations you can think of.  What about the ones nobody could think of?

For these reasons and more, we use QA environments.  This is our laboratory; it's the place where we test hypotheses.  Unfortunately, QA environments are never perfect.  Even if they are copies of production, they suffer from one fatal flaw: they aren't production.

Scientists face a very similar problem.  Since they can't see the source code, they can only rely on experimentation.  The problem is that their QA environment isn't the same as the real world either.  A large hadron collider, for example, creates highly contrived situations.

Drawing the analogy between natural scientists and programmers is useful for a few reasons.  First, taking the attitude of a scientist helps to distance ourselves from 'our' code.  It can be easy to get defensive about code we've written.  If you treat code as something to be understood, however, it suddenly has a life of its own.  It's not anyone's baby.  It just is.

Second, the analogy to science helps to highlight some of the ways we can respond to problems.  Some programmers start to theorize all kinds of possible explanations for an outage.  Was it neutrinos or was the firewall rewriting packet headers?  Others come up with more functional explanations.  What if we just restart the server?  These are both valid approaches, but they must both gather evidence in order to show that their hypotheses are valid.

These different approaches also show, third, we need laboratories where we can test hypotheses, no matter how wild.  That's why your QA environment should match your production environment as closely as possible, perhaps even down to the length of the network cables.  It's bad when problems are not caught in QA.  It's worse when you can't even explain why something works in one environment and not another.  Testing isn't just about proving that a solution meets its requirements; it's more generally about evaluating hypotheses.

Finally, I think it's important to think about the scientific method because many programmers are not good at thinking in this way.  It takes experience, and we tend not to get that kind of experience in schools--even science majors don't do much experimentation, unless mocking up lab results to match the Right Answer counts.  A computer science degree is basically a degree in abstract analysis, not too different from a mathematics degree.  In the real world, programmers need to flex their scientific muscles too.
Related Posts Plugin for WordPress, Blogger...