News for hackers. Stuff that matters.
Re:The Clean Coder: The Craftsman 62, The Dark Path. by ekidd on Sunday October 03, 2010 @11:37PM (#1754525)
I'm a raving TDD/BDD fanatic, and this kata still makes me cringe a little. At least they finally converge on a plausible-looking solution. Sadly, some TDD katas on the web turn into train wrecks. See Ron Jeffries' Sudoku solver, for example:

http://ravimohan.blogspot.com/2007/04/learning-from-sudoku-s...

If I were implementing this algorithm using TDD, I'd first write down a bunch of test cases, and then try the obvious algorithm. If that worked, I'd test a few more corner cases. There's really no reason why you have to blindly stagger through the design space when the algorithm is obvious.

Re:The Clean Coder: The Craftsman 62, The Dark Path. by nostrademons on Sunday October 03, 2010 @11:47PM (#1754536)
I think this is a problem with a lot of methodologies, in that they often obscure that the point is to write good software. If you can write good software without following the methodology, do that. All these techniques - agile, scrum, stand-up meetings, CRC cards, TDD, pair programming, code review, unit tests - are just tools at your disposal to make writing the program go smoother.

Google's methodology for writing quality software is basically:

1. Hire good people.

2. Let them do what they need to to get the product out the door.

This seems to have been adopted by FaceBook as well. I wish more companies would consider it instead of adopting the snake oil that a bunch of consultancies peddle.

Re:The Clean Coder: The Craftsman 62, The Dark Path. by hello_moto on Monday October 04, 2010 @10:49AM (#1755785)
Google does a lot of test automation. I don't know if they do test-first or not but I heard that new-hire is given the "Working Effectively with Legacy Code", written by someone who works with Uncle Bob. At Google, there is a special role called Software Engineer in Test: they write tools to help developers debug performance issue, they also sit down with developers to help them to make their code more testable (this happened when you don't do test-first, sometime/once-in-a-while, developers will write code that's not easily testable via automation tests). Google is huge fans of code-review (probably that's why Andy Hertzfeld dislike Google).

In contrast, Facebook probably doesn't do these practices (or did them in minimum). Facebook subscribes themselves to cowboy culture. Few days ago there were 2 posts in HN complaining about the Facebook API quality and documentation. I've used Facebook since 2006 and I do notice that their software is buggy whenever they push a newer build (you can tell: as soon as things degenerate, they just pushed the latest build). Aditya, who was the Director of Engineer back in 2008 (don't know if he's still), gave a talk recorded by InfoQ. He was asked about the state of unit-tests (or automation tests) and he said that the number of tests are minimum and not as many as he would've liked it to be.

I know that some companies enforce Code-Review, TDD and CI: kaChing, IMVU. There are others (I think Disqus and Quora do that too, but I'm not sure to what extend) as well but they might not say it out-loud around here.

The point is this: It doesn't matter what it calls (unit-tests, integration-tests, acceptance-tests), as long as you have extensive automated-tests, you can iterate faster and safer in the long-run. You can also deploy faster because you're quite sure nothing breaks.

The old QA process no longer applies when people would write code and throw it over the wall to the QA and hope everything goes well (check Uncle Bob article about how we do QA is wrong).

Re:The Clean Coder: The Craftsman 62, The Dark Path. by grok2 on Monday October 04, 2010 @01:56PM (#1756571)
Actually this post from that same blog (to me) correctly summarizes the Kata exercise: http://pindancing.blogspot.com/2009/09/sudoku-in-coders-at-w...

Specifically Norvigs quote in the blog: "I see tests more as a way of correcting errors rather than as a way of design. This extreme approach of saying, Well, the first thing you do is write a test that says I get the right answer at the end, and then you run it and see that it fails, and then you say, What do I need next? that doesn’t seem like the right way to design something to me."

Re:The Clean Coder: The Craftsman 62, The Dark Path. by Cushman on Monday October 04, 2010 @12:44AM (#1754633)
Yeah, I didn't mean to say that I was questioning TDD in general— rather that it was obvious that this dev's insistence on solving the smallest part of the problem first was blinding him to the fact that the problem as a whole is quite small.

And wow— that Sudoku epic is terrifying.

Re:The Clean Coder: The Craftsman 62, The Dark Path. by xyzzyz on Monday October 04, 2010 @01:43AM (#1754718)
> (...) and then try the obvious algorithm. If that worked (...)

Personally, I would not bother to implement an algorithm if I was not sure it works correctly -- if it turns out to be wrong, I had wasted my time.

Re:The Clean Coder: The Craftsman 62, The Dark Path. by ekidd on Monday October 04, 2010 @08:55AM (#1755480)
Interesting! How do you know your algorithm handles the tricky corner cases? I'd imagine you need to write down all the corner cases, write a formal proof for each, and hope that (1) your proof is correct, and (2) your code actually matches your proof. Could you elaborate on how you do this?

Here are a few corner cases I can think of for word-wrapping:

1) The line is empty.

2) The line is 1 character shorter/1 character longer/exactly the same length as the wrap limit.

3) The first break point for the line falls before/after/on the wrap limit.

4) The first character(s) of the newly-wrapped line are spaces.

I don't think that I could just eyeball a word-wrap algorithm and know that it handles all these cases correctly. I would, at a minimum, need to check each case. But at that point, I'm just as well off writing test cases, and allowing the computer to verify that my algorithm does the right thing for each. Of course, that's not as good as a proof by induction for each desired property, but it's a _lot_ less work, and it's almost always adequate. And I don't need to worry about mistakes in my proof.

Am I misunderstanding something about your approach? How would you handle this?

Re:The Clean Coder: The Craftsman 62, The Dark Path. by xyzzyz on Monday October 04, 2010 @12:00PM (#1756092)
I do not say I do not test my code. I just see no purpose in implementing code that I hope will work. I need to believe it will work, and if it does not, I am sure that it is an implementation, not an algorithm issue. The first attempt in the article was just silly, and the second was just wrong. I would not waste time writing these, instead I would use this time to figure out the real solution and then I would implement it.
Re:The Clean Coder: The Craftsman 62, The Dark Path. by nostrademons on Monday October 04, 2010 @04:14AM (#1754997)
Hrmm? How else are you going to solve a problem that you've never faced before?

And yes, wasted time is basically a necessary feature of working on problems that don't have a textbook solution.

Re:The Clean Coder: The Craftsman 62, The Dark Path. by xyzzyz on Monday October 04, 2010 @05:34AM (#1755128)
I am going to think about a problem, and if I come up with a solution I am sure is correct, I shall implement it. If I do not, I will search the web for a solution. If I find one, I will make sure it is correct, and then implement it. If I do not, I am going to ask some friends of mine about it. If they do not know the solution, I will temporaily loose the constraints and implement a solution that is "good enough", then I will make the problem my next research problem.

Implementing an algorithm we are not sure that works correctly is not only a waste of time -- it is actually harmful, for it is possible to ship an algorithm that only seem to work correctly. Test cases will not prove that a solution is correct.

Re:The Clean Coder: The Craftsman 62, The Dark Path. by xyzzyz on Tuesday October 05, 2010 @02:00AM (#1758751)
What did I get downvoted for? I am relatively new here and I do not know all the things that are frowned upon?
Re:The Clean Coder: The Craftsman 62, The Dark Path. by nostrademons on Tuesday October 05, 2010 @02:21AM (#1758782)
I suspect that in many people's experience, "a solution that I am sure is correct" is anything but, until you've actually tested it. And testing it often takes far less time than puzzling over it and pondering. And so claiming that you never implement anything until you're absolutely certain it's correct smacks of youthful naivete, of the type common to college students who've never implemented anything.

In many of the more interesting fields, it's often not well-defined what it means for an algorithm to be "correct" anyway. What would a "correct" web search algorithm look like? How about a "correct" recommendation algorithm? A "correct" flight fare prediction algorithm? A "correct" stock trading algorithm? A "correct" Starcraft AI? There're "better" and "worse" algorithms for these, but there's no such thing as a "correct" one.

For many problems that do have an obvious "correct" solution (eg. OCR, face recognition, fare optimization), getting there is a nearly intractable problem, and the interesting part is in figuring out how you can approximate it as well as possible with the resources you have available is the best you can do.

Re:The Clean Coder: The Craftsman 62, The Dark Path. by xyzzyz on Tuesday October 05, 2010 @02:57PM (#1760939)
You are right in what you say, however I think I have been misunderstood. It is well defined what it means for an algorithm to be correct -- it means that it returns expected output for a given input. Ambiguous situations you are talking about are when the problem is not well defined. That is why I used the word "algorithm" in my first comment. Trial and error is perfectly fine when we are not exactly sure what result we expect, but we will "feel" when it's good enough. It is unacceptable if we know exactly what the result should be, though.

When the problem is well-defined, like word-wrapping, or sorting, or whatever, if you blindly stagger and write code hoping it will work, instead of stopping to think why it should work, you are doing it wrong.