Refactoring to clean code.
amaxus.com
amaxus.com
It's strange, however, that the author brings up the absolute necessity of unit tests when refactoring like this, yet says he omitted them for brevity. I've found, however that one of the most difficult and important parts of refactoring is making sure code has proper test coverage, especially when starting with crufty code. It's unlikely that the (not very clean) code he presents came with a thorough set of unit tests.
The first thing I try to do when working with insufficiently tested code is to begin refactoring it into a more testable form. This involves many small steps, and in some cases manual testing to make sure that the automated tests I'm writing are testing the correct thing. I think this article would be better if he moved the parts conducive to that (#6 and #9) to the top and included examples of writing tests (with appropriate mocks).
Further, canvas is a rich and complicated API that is relatively difficult to test against; for this article something simpler might have been more appropriate.
The hacker mentality, furthered perhaps by the prevalence of scripting languages, allows people to think sometimes that "if it passes the test, it's good for production!"
This is how amateur software projects die. I've seen it dozens of times: a young, ambitious programmer starts promising project, creates a sort of mock-up that runs (badly), and then tries to continue adding features and improving upon the already-decaying codebase.
Refactor your code. Make things clean. Even if you never plan on passing your passion project on to another developer, it's important to understand that without clean code even you will be unable to understand and efficiently work with your codebase.
This is one of the better introductory tutorials I've ever read, and the hands-on example is excellent. Great read, and I've bookmarked it to refer people to later.
I often find the "many little functions" working against me. An abstraction is beneficial if your task doesn't require seeing through that abstraction - but otherwise it can be harmful. If I need to change something core to movement, flap() and drop() aren't doing me any favors.
Unless theres other glaring issues, if a function is already less than 30 lines of code, I usually find a comment nicer than refactoring to yet another method.
As a rule of thumb, I comment only when there is a surprising externality relevant to the code e.g. performance, obscure workaround.
No, you aren't the only one. I made a similar case in a related discussion a few months ago:
http://news.ycombinator.com/item?id=1224210 http://news.ycombinator.com/item?id=1224206
This particular tutorial seems to have some merit, but it's a bit too clever for its own good at times. For example, the very first change introduces a named constant called SIGHT_SHOT_RADIUS_PX, which is not in fact used as a radius at all. Indeed, if it were, the later refactoring to split out a testForHits operation that in turn uses testForXAxisHit and testForYAxisHit would break down, because while testing for hits is a useful atomic operation, the tests on the two axes would not be independent and the logic in those functions is meaningless out of context.
No unit test is going to warn you about traps like these, and breaking things down according to dogmatic rules rather than to separate meaningful, self-contained concepts does tend to create such problems IME.
Now, you may disagree with people on what constitutes "better code", but that's not a knock against the practice of refactoring.
As for "many little functions", although this style is favored by the OO elite, the research literature is actually against it. Moderately sized functions (IIRC, up to 100 lines - but the studies were in old languages) have been found to be less error-prone. Steve McConnell and Robert Glass have both written about this. I've posted citations here before but don't want to right now.
And writing good tests is HARD. If you think it is easy then you are testing the wrong thing. A lot of people say refactoring without tests is pure danger. I disagree. Sometimes you don't know enough about the problem space to write good tests. However, refactoring forces you to understand the "micro" world of the code. And this gives you a leg up in order to understand what tests are actually needed.
(Edit: quicktest -> QuickCheck. Thanks!)
I'll say the one thing that would really help would be unit tests on the old code. It's rough transitioning some dirty logic that seems to work from one end to the other and not having a simple test to know I didn't break everything upon refactoring.
[0]: http://www.amazon.com/Working-Effectively-Legacy-Michael-Fea...