Please create an account to participate in the Slashdot moderation system

 



Forgot your password?
typodupeerror
×
Programming IT Technology

Donald Knuth Rips On Unit Tests and More 567

eldavojohn writes "You may be familiar with Donald Knuth from his famous Art of Computer Programming books but he's also the father of TeX and, arguably, one of the founders of open source. There's an interesting interview where he says a lot of stuff I wouldn't have predicted. One of the first surprises to me was that he didn't seem to be a huge proponent of unit tests. I use JUnit to test parts of my projects maybe 200 times a day but Knuth calls that kind of practice a 'waste of time' and claims 'nothing needs to be "mocked up."' He also states that methods to write software to take advantage of parallel programming hardware (like multi-core systems that we've discussed) are too difficult for him to tackle due to ever-changing hardware. He even goes so far as to vent about his unhappiness toward chipmakers for forcing us into the multicore realm. He pitches his idea of 'literate programming' which I must admit I've never heard of but find it intriguing. At the end, he even remarks on his adage that young people shouldn't do things just because they're trendy. Whether you love him or hate him, he sure has some interesting/flame-bait things to say."
This discussion has been archived. No new comments can be posted.

Donald Knuth Rips On Unit Tests and More

Comments Filter:
  • Shocked (Score:5, Interesting)

    by gowen ( 141411 ) <gwowen@gmail.com> on Saturday April 26, 2008 @12:52PM (#23207690) Homepage Journal

    He pitches his idea of "literate programming" which I must admit I've never heard of
    I'm shocked to discover that Knuth is taking an opportunity to push literate programming, given that he's been pushing literate programming at every opportunity for at least 25 years.

    Now, I've no problem with literate programming, but given that even semi-literate practices like "write good comments" hasn't caught on in many places, I think Don is flogging a dead horse by suggesting that code should be entirely documentation driven.
  • What? (Score:5, Interesting)

    by TheRaven64 ( 641858 ) on Saturday April 26, 2008 @01:00PM (#23207732) Journal
    You've heard of TeX, written in Web, the language designed for Literate Programming, but you've not heard of Literate Programming?

    I have a lot of respect for Knuth as an algorithms guy, but anything he says about programming needs to be taken with a grain of salt. When he created the TeX language, he lost all credibility - designing a language in 1978 which makes structured programming almost impossible is just insane. TeX gets a lot of praise as being 'bug free,' but that's really only half true. The core of TeX is a virtual machine and a set of typesetting algorithms, both of which are very simple pieces of code (they'd have to be to run on a PDP-10). Most of the bits people actually use are then metaprogrammed on top of the virtual machine, and frequently contain bugs which are a colossal pain to track down because of the inherent flaws in the language (no scoping, for example).

    If you want to learn about algorithms, listen to Donald Knuth and you will learn a great deal. If you want to learn about programming, listen to Edsger Dijkstra or Alan Kay.

  • You misunderstand (Score:5, Interesting)

    by Rix ( 54095 ) on Saturday April 26, 2008 @01:01PM (#23207750)
    Using "MULTIPLYBY" instead of "*" is asinine, because both are equally descriptive. Putting a comment above the line telling people why you're doing it isn't.
  • by nuzak ( 959558 ) on Saturday April 26, 2008 @01:04PM (#23207766) Journal
    > But the story during my grad school days was that, Knuth offered 1000$ prize to anyone fining a bug TeX and he doubled it a couple of times.

    The $1000 bounty was from Dan Bernstein with respect to qmail. He's always found a reason to weasel out of ever paying.

    Knuth started the bounty at $2.56 (one "hexidollar") and doubled it every year til it reached $327.68. Several people have claimed it, most people never cashed the checks. One of the first bug finders had his check framed.

  • by TheRaven64 ( 641858 ) on Saturday April 26, 2008 @01:05PM (#23207772) Journal
    The original prize was $2.56 (i.e. 2^8Â), and he doubled it every time someone found a bug until it reached $327.68. Over 400 bugs have been fixed in TeX, and that's just counting the core VM and typesetting algorithms - all of the rest is in metaprogrammed packages, many of which contain numerous bugs. I'm fairly sure that most programmers could write bug-free code if the only place where bugs counted was in a simple VM with a few dozen instructions that interpreted all of the rest of the code...
  • by 1729 ( 581437 ) <slashdot1729@nOsPAM.gmail.com> on Saturday April 26, 2008 @01:09PM (#23207790)

    ... looks like it falls into the same trap as COBOL. The idea that by making programming languages incredibly verbose, they will somehow become easier to use is a fallacy.

    Using "MULTIPLYBY" instead of "*" isn't going to make your code easier to read.
    From what I've seen (particularly of CWEB), literate programming doesn't change the programming language itself, it just adds a TeX style markup to the comments so that detailed (and nicely typeset) documentation can be generated from the source code. Take a look at some of Knuth's CWEB code, such as his implementation of Adventure:

    http://sunburn.stanford.edu/~knuth/programs/advent.w.gz [stanford.edu]

    It appears to be ordinary C once the CWEB documentation is stripped out.
  • by 1729 ( 581437 ) <slashdot1729@nOsPAM.gmail.com> on Saturday April 26, 2008 @01:15PM (#23207822)

    It is probably folklore. But the story during my grad school days was that, Knuth offered 1000$ prize to anyone fining a bug TeX and he doubled it a couple of times. And it was never claimed. If that was true, it is very unlikely he was just flame baiting.
    He offers rewards for reporting errors is his books and for finding bugs in his code:

    http://en.wikipedia.org/wiki/Knuth_reward_check [wikipedia.org]

    Many people save these (usually small) checks as souvenirs. My father -- a frugal mathematician -- received a few $2.56 checks from Knuth, and he promptly cashed each one.
  • by free space ( 13714 ) on Saturday April 26, 2008 @01:20PM (#23207848)

    ...the idea of immediate compilation and "unit tests" appeals to me only rarely, when Iâ(TM)m feeling my way in a totally unknown environment and need feedback about what works and what doesnâ(TM)t. Otherwise, lots of time is wasted on activities that I simply never need to perform or even think about. Nothing needs to be "mocked up."


    I'm not sure, but I think he's talking personally about his own work on his code. Remember that he comes from an era where people had the goal of mathematically proving that the code is indeed correct. He isn't necessarily doing this now but my persaonal guess is that he prefers statically checking the code to checking a running program. In certain kinds of mathematic/scientific applications this could make sense.
  • Re:What? (Score:5, Interesting)

    by cbart387 ( 1192883 ) on Saturday April 26, 2008 @01:40PM (#23207932)

    If you want to learn about algorithms, listen to Donald Knuth and you will learn a great deal. If you want to learn about programming, listen to Edsger Dijkstra or Alan Kay.
    For those that didn't read the article, Knuth expressed criticism on several of the questions asked, but he didn't want to just duck the question. For instance, the 'trendy question' he said this.

    With the caveat that thereâ(TM)s no reason anybody should care about the opinions of a computer scientist/mathematician like me regarding software development
  • by Lengyel ( 1082385 ) on Saturday April 26, 2008 @01:48PM (#23207970)
    I cannot resist including my own literate program [cuny.edu]. The original was written in reply to a programming test, posed by a prospective employer. During the interview I was told that the company contained their costs by keeping salaries low. In keeping with the spirit of literate programming, this story is recounted within the body of the program.

    Literate programming might be more popular if it had support for interactive debugging, with the standard features common in contemporary interactive development environments.

  • by numbsafari ( 139135 ) <swilson@bsd4us. o r g> on Saturday April 26, 2008 @01:51PM (#23207990)
    A better way to handle that is to turn the loop body into a function or group of functions. makes the code easier to read and a good compiler will inline the function so their's no performance loss.
  • Re:What? (Score:2, Interesting)

    by LizardKing ( 5245 ) on Saturday April 26, 2008 @01:53PM (#23207996)

    I think that the grandparent poster was referring to the dissecting and reordering of code that generally occurs with Literate Programming. Take the code from the book "A Retargetable C Compiler: Design and Implementation", which uses Literate Programming. The code is written as snippets and in an order that makes describing the purpose of the compiler easier, but the code itself loses its "integrity" (individual functions are split up and reordered) and is effectively unmaintainable in its source form - it needs to be preprocessed into a more logical order for the programmer to follow, but in doing so you're no longer editing the source in its Literate Programming form!

    I know this will annoy those who believe he's some saint just because they've heard he is from anecdotes, but Knuth is somewhat irrelevant. An academic who has written no significant code (no, TeX doesn't count as it's just a simple interpreter), and who rails against the real world from his ivory tower. Even his books are a waste of time, with code written in an idealised assembly language that glosses over a number of practical considerations, they're dated, unbelievably boring and of no practical use compared to something like the "Numerical Recipes" or "Algorithms in ..." books.

  • computer programming (Score:1, Interesting)

    by nguy ( 1207026 ) on Saturday April 26, 2008 @02:00PM (#23208028)
    Knuth's view of programming seems to be that of clever tricks and fast algorithms. That may have been true when he got into the field, but it isn't anymore. Today, it's about creating big systems that need to be maintained by many programmers, not all of whom are as smart as Knuth. Algorithms come in libraries and are implemented by specialists. So, maybe Knuth doesn't need unit tests for his projects, but real-world projects do.
  • Re:He's right (Score:5, Interesting)

    by clap_hands ( 320732 ) on Saturday April 26, 2008 @02:12PM (#23208094) Homepage
    The thing about unit testing is that it's subject to the law of diminishing returns. A simple test of the basic functionality gets you a lot for minimal effort. Writing dozens of carefully chosen tests to examine boundary conditions etc. gives you a little bit more, but for a great deal more effort. Whether or not it's worth it depends very much on the situation and the nature of the code you're writing.
  • by Animats ( 122034 ) on Saturday April 26, 2008 @02:17PM (#23208126) Homepage

    There's a relevant quirk of Stanford University employee policy. For Stanford academic employees, software usually is considered "work for hire" and an "institutional work", with Stanford holding the copyright. But books and papers are considered to be the property of the author. [stanford.edu] (Policy on this changed in the late 1990s; there's a long history here, primarily involving the founding of Sun and Cisco.) However, Stanford permits the creator to place a work in the public domain, unless external funding prohibits this.

    Knuth's code is open source. But his books are not.

  • by MOBE2001 ( 263700 ) on Saturday April 26, 2008 @02:31PM (#23208202) Homepage Journal
    From the interview I see that Knuth thinks multithreading may turn out to be a flop. I agree and I would go even further. I don't think there is any question that the multithreading strategy used by Intel and AMD for multicore programming is a big flop already. Multithreading is the second worst thing to have happened to computing. The worst is single-threading which is what normal algorithmic programming is based on. Parallelism is the correct approach to computing. Computing should have been parallel from the start even on single-core processors. If parallelism had been emulated in a processor from the beginning, adding more cores would have been a simple evolutionary transition, a mere engineering problem. My point is that there is a better way to do parallel programming that does not involve threads at all. Cellular automata and neural network programmers have been emulating parallism for decades without threads.

    Essentially, you need to have two buffers and a loop. While the first buffer is being processed, the second buffer is filled with objects to be processed in the next cycle. When the first buffer is done, swap the buffers and start over. Two buffers are used in order to prevent signal racing conditions. It's not rocket science. We just need to take it to the instruction level by changing it from a software mechanism into a hardware mechanism. In other words, the mechanism should reside in the processor, whether single or multicore. This is the correct approach to parallelism. Multithreading is going to be a complete disaster, a multi-billion dollar disaster. Google "Nightmare on Core Street" to find out why multithreading is not part of the future of parallel computing.
  • Re:What? (Score:3, Interesting)

    by sc00p18 ( 536811 ) on Saturday April 26, 2008 @02:35PM (#23208222)
    Fortune cookie at the bottom of the page for me:

    They are relatively good but absolutely terrible. -- Alan Kay, commenting on Apollos
  • by zullnero ( 833754 ) on Saturday April 26, 2008 @02:37PM (#23208240) Homepage
    Seriously, if you're "religious" about unit testing and mock objects, then you really need to revise the way you live your life.

    It's just a good habit to get into, if you take it seriously and don't just create tests that test silly little things like "is my text box centered where I slapped it on the form with gui form tool" type of stuff. That's kind of the point he's trying to make, that you program intelligently in the first place to avoid having an insane amount of redundant tests to pass each time you build.

    I've been doing literate programming (well, as close as you can with C and its derivative languages) for a long time now. I've watched XP coders take that literacy and chop it all up because "it didn't look pretty enough". The idea with making something literate is to make it so clear that you can reduce the total numbers of tests needed to make that code pass to only ones that test the actual expected outputs of that function. That's something that intelligent coders who don't just follow the Agile rulebook, but apply it effectively, can do. I don't know how many times I'd see a piece of code that did one simple task, had one test to test the output of that test, then another coder drops 3 more tests because they "didn't feel comfortable with only one" without specifying WHY. That is how you get into having redundant tests that muck up your test infrastructure.
  • Re:He's right (Score:3, Interesting)

    by tknd ( 979052 ) on Saturday April 26, 2008 @02:51PM (#23208296)

    If the coder is already aware of the possible failures, then he should be fixing the code instead of writing a test.

    The purpose of a unit test testing for failure cases is not to detect places where the code has issues, but to ensure that the code performs as expected given the initial requirements/specifications. When people talk about "failure cases" they mean that they expect the code to return some sort of exception, not that they expect the code to fail in how it operates.

    For example consider that a block of code is required to perform a "division" operation. A unit test case to test for a failure case would be one that provides a denominator of zero. In this case we can expect that the code will throw a divide-by-zero exception or return a special error code rather than halt the entire program.

    And after a "unit" of code is written correctly, it shouldn't be changed a lot. Any big changes means a change in semantics, which means the original unit tests are useless anyway.

    Not necessarily. An easy example is a developer writes division function and test cases are written to ensure it operates as expected. A month later we determine that we need to improve performance of the software that utilizes the division function, and the current division function performs poorly. So given the unit test cases, we can re-implement an optimized version of the function, and testing for accuracy of the function is almost free.

    Another example is a developer starts working on a big software project. 6 months later he conveniently leaves the company without notice and his portion of the project is only 50% complete. Would you rather have 50% of code that you are not sure works or 25% of code that you are not sure works, but 100% of the unit test cases required for the existing code to tell you whether or not it works?

    An even more useful example is dealing with bugs. You receive a bug report on some software that performs some function. You eventually find the cause of the bug in a particular function, but realize that it requires a specific state of the system. You fix the bug in the software but it results in "ugly code" that at first glance is unexplainable. How should you ensure that this code is not modified accidentally? A unit test case of course! This is a good example for very special bugs that are the result of hardware issues or other software systems that are out of your control.

    There are valid argument against unit test cases (and automated testing in general). One argument is that in order to do automated testing, you are required to write more code for the test cases. This is a problem because now you have new code that does not have test cases for itself and can have bugs. So if the test case is written incorrectly, then it may point out that the original code is working incorrectly when in fact the test case is the culprit.

    Another argument (which is what I think you were trying to say) is that when the specifications for the code change, so must the test cases. This can potentially double the maintenance work required when updating the software.

    So I don't think that every piece of code needs a unit test case. But for code that will be reused considerably and is closer to the foundation of your project, you probably should make unit test cases.

  • Out of favor (Score:4, Interesting)

    by illegalcortex ( 1007791 ) on Saturday April 26, 2008 @02:54PM (#23208312)
    It's also out of favor because of how much of the real world of programming works. My very small company does a lot of work for a very, very large company. At my small company, we have one layer of management - the owner of the company. Everyone else is in the level of "not an owner of the company."

    At the large company, there are a multitude of layers of management. Any software they build require extensive specifications and documentation far in advance of laying down any code. The decide all the aspects of the software before it's written. At my company, the boss just gives us a general outline of what he's thinking about and ask us to feel out the idea. We use a RAD environment and will often have a first iteration within a week. This version tends to get completely, sometimes multiple times. We do not document any of this in advance because the usable version may differ so much from the original ideas.

    At the large company, their projects tend to take years and years, go far over budget and typically are much less useful than they had originally hoped. As a bonus, they are usually bug-ridden and unstable. Many times they just eventually get canceled by the new layer of management, who then get awards for this "cost saving measure." At my company, our projects are typically finished far in advance for a tiny price. They are typically of very high quality, with very minor bugs which we fix rather quickly.

    This large company frequently hires our company to build software rather than trying to do it internally. They are usually amazed at the things we can do.

    Something like "literate programming" is completely anathema to how our company works. If we started trying to write specifications in advance of figuring out what product our clients actually want (as opposed to what they think they want at the start of the process).

    Now, I will state that our company only works because we don't hire idiots or slackers. Also, I am fully aware that this is not a good way to, for example, design nuclear power plant software or a baggage control system. But for businesses, all that documentation and "thinking" can just cloak that fact that the people building the software don't know what they are doing.
  • by a66at ( 696335 ) on Saturday April 26, 2008 @02:54PM (#23208314)
    No, he prefers debugging (which is dynamic checking) over systematic unit-testing. See:

    That simulator includes debugging features like the ones I found so useful in Ed Satterthwaiteâ(TM)s system for ALGOL W, mentioned earlier. I always feel quite confident after checking a program with those tools.
  • by seaturnip ( 1068078 ) on Saturday April 26, 2008 @02:59PM (#23208340)
    It actually doesn't sound to me like Knuth has heard of the term 'unit test' before this interview at all. It sounds like he thinks it means prototyping a function before writing the real version. Given that he likes to push his model of documentation-driven programming, I think he might be more sympathetic to unit tests if he understood that they can serve as a kind of formalized documentation.
  • Nonsense (Score:3, Interesting)

    by MarkusQ ( 450076 ) on Saturday April 26, 2008 @03:37PM (#23208512) Journal

    Making a program parallel will always be too hard for most programmers.

    Nonsense. The problem isn't with the programmers, it's with the languages. Writing object oriented code in Fortran is too difficuiltr for most programmers, but that doesn't mean that the programmers aren't up to the task, but that the language they are using isn't well suited to the job.

    Learn a little erlang, or Haskel to see how easy writing massively parallel programs can be. p.--MarkusQ

  • Re:He's right (Score:2, Interesting)

    by rawler ( 1005089 ) <ulrik.mikaelsson ... m ['gma' in gap]> on Saturday April 26, 2008 @04:09PM (#23208674)
    Testing is not a waste of time. Writing unit-tests, is. A better use of that time could be learning about http://en.wikipedia.org/wiki/Design_by_contract [wikipedia.org], and possibly a language that implements it. (Dare I suggest http://en.wikipedia.org/wiki/D_(programming_language) [wikipedia.org] ?)

    The problems with unit test as someone already pointed out, is that writing small tests helps you verify the really simple things is relatively quick and easy but, those mistakes are easy to detect and find anyways, even a decent compiler/lint-tool will find many of them for you.

    Most of the really tricky problems comes in the interfacing between units and no unit tests will help you here.

    Design-by-contract, on the other hand, can.

    First of all it requires you to give really clear formal declaration on each component and how it may be used. (A contract) This contract can be used to automate testing of all the exotic, but allowed, corner-cases your implementation may have missed.

    The contract implemented by classes and function will furthermore function as a REALLY clear documentation, spelling out exactly what the allowed input and expected output is.

    Finally, in a design-by-contract-enabled language, you can choose to turn on debugging at run-time, and enable all kinds of tests, including input/output validation, class invariants and other really handy tests that allows you to test AND debug your entire system at once, but still at a highly granular level.

    Design-by-contract will in no way prevent you, or discourage you, from writing test-code (in fact, it encourages it), but it can help you spend your time writing those tests more sensibly, and drive your code in much more life-like scenarios.

    All in much less time spent, and much more return than the traditional ways of unit-testing.
  • Re:Spaghetti-O Code (Score:5, Interesting)

    by Kent Recal ( 714863 ) on Saturday April 26, 2008 @04:14PM (#23208702)
    I know this problem very well from the dark days when I was still writing java.
    There doesn't seem to be a satisfactory solution, it's always a tradeoff.

    While reading this thread I realized a funny thing: This particular annoyance
    totally vanished from my day-to-day headaches when I switched to python about
    a year ago.

    It's a bit wierd because Python doesn't even use braces so one would expect
    it to be even harder to identify where a block begins and where it ends.
    But the opposite has been the case for me: The clean syntax and language
    design has led me to write, on average, shorter blocks with very little
    nesting.

  • by lekikui ( 1000144 ) <xyzzy@b.armory.com> on Saturday April 26, 2008 @04:57PM (#23208916) Homepage Journal

    Worst Summary Ever
    Thanks. I really appreciate the amount of respect and appreciation I get from this site.

    Donald Knuth is not "playing hardball." Nobody needs to call the interview "raw and uncut," or "unplugged."
    Wow, where exactly did I (or CmdrTaco) use any of those phrases? Calling something a "complete waste of time" is, in my book at least, "ripping" on something. I didn't "sully his good name," I posted what I found interesting. You should also point out he has prostate cancer and I left that out. God, what horrible spin I used! You'd think I was talking about someone whose life wasn't at risk, the way I spun that summary!
    Well, speaking for myself, I did find the summary fairly heavily spun. In general, it seemed to take statements out of context somewhat, turning "I generally don't find unit tests useful" into "Knuth rips on unit tests". He mentions that there are definite applications for multicore work, but that he's not in that field. Basically, the summary struck me as being spun to make his statements seem more controversial.

    Knuth is a well-respected figure who makes moderate, thoughtful statements.
    I happen to disagree with his stances on multi-core chips and unit testing. I didn't find anything thoughtful about what he said and really wish he would have elaborated on why unit testing is a complete waste of time.
    Look, shall we quote what he said about unit testing?

    As to your real question, the idea of immediate compilation and "unit tests" appeals to me only rarely, when Iâ(TM)m feeling my way in a totally unknown environment and need feedback about what works and what doesnâ(TM)t. Otherwise, lots of time is wasted on activities that I simply never need to perform or even think about. Nothing needs to be "mocked up."
    That doesn't look like he ripped on it. He said, basically, that he doesn't find it useful, and that it tends to waste time because of the sort of work he's doing.

    In other words, he considered the question, and provided the answer that described what he found for his work. Not what you'll find for your work, or anything like that. Just what he finds works for him.

    From the summary, you'd think he was a trash-talking pro-wrestler.
    Actually, after reading the article, I did find him to be a bit preachy. Apparently you and everyone else find him unquestionably correct in all his statements from that interview.
    I'll admit to having generally found him right. When he's talking about a subject he's not an expert on, he mentions that. He says why his opinion isn't the best on all topics. He seems to be generally careful about what he's saying.

    Where what he says is likely to be personal bias, he mentions that. So weight it as you think is relevant, given who he is.

    Yes, there's stuff in there I reckon is probably wrong. There's also an awful lot that's probably right, and he's generally been careful and thoughtful about what he's saying.

    And also, people are claiming he said these things "in passing." Which I find to be a phrase used when you want to avoid owning up to something you said. If I call you a "whiney bitch in passing" that doesn't lessen it one bit. Knuth claims no one should listen to him. Why is he publishing books if no one should listen to him?
    No, he said, quite regularly, that his opinion on some things wasn't going to be very useful, as he wasn't that good at them. His books are about the topics he is good at. He doesn't write them about software development best practice, or economic, or parallel computation. He deals with the topics he knows, and that's all he expects to be listened to about.

    The guy said some inflammatory comments. If you read the following posts, you'll realize that I wasn't the only one that found them inflammatory or controversial.
    No. Knuth generally was cautious about his statements. Do please find me some actually inflammatory comments from there. Go on, direct quotes, not just rephrasings or spin.
  • by mkcmkc ( 197982 ) on Saturday April 26, 2008 @05:26PM (#23209032)

    Most people using C++/Java/C# end up writing "} //end while" anyway,
    Pray god I never have to work on code written by these fictitious "most people".
    Well, actually, once you've programmed in Python for a while, all of those spurious '}'s to close blocks really start to look as annoying and useless as "} //end while".
  • by jacquesm ( 154384 ) <j@NoSpam.ww.com> on Saturday April 26, 2008 @05:34PM (#23209080) Homepage
    if you feel like experimenting with literate programming try finding the 'leo' editor (written in python)
  • Re:Spaghetti-O Code (Score:3, Interesting)

    by try_anything ( 880404 ) on Saturday April 26, 2008 @08:24PM (#23210180)
    This is why good programmers must be good writers as well. The ability to explain the structure of code, primarily with clear, precise names for classes, functions, and variables and secondarily with comments, is as important as the ability to decompose a program logically. I have seen programs that were twice as complex as they should have been, yet made perfect sense and were easy to hack on, because the code and comments made it easy to understand the original programmer's understanding [zafar.se].

    In the interview, Knuth gave one reason why literate programming isn't popular: few people are good programmers, few people are good writers, and literate programming only works for people who fall into both categories. I think he's right. Encouraging people to write more unstructured prose than they feel a need to results in worthless verbiage, and most programmers naturally limit their comments to sentence fragments and the occasional short paragraph. If you have to force people to write, you need to provide structure and clear expectations. That's why Javadoc and similar schemes are so much more valuable in practice than literate programming.
  • Re:Spaghetti-O Code (Score:3, Interesting)

    by try_anything ( 880404 ) on Saturday April 26, 2008 @09:01PM (#23210444)

    Breaking those pages of code into a bunch of other subroutines solely on some misguided notion that a function shouldn't be longer than N lines
    Usually people can come up with better reasons than that.

    I've worked with code written in the "pages-long function" style. One thing I've found consistently in such code is duplicated functionality. I've even personally seen how this happens: A programmer implements a very large, complex piece of functionality as a function. Later, the need arise to implement a variation on this functionality. It turns out that the variation is quite structurally different, so to implement it by parameterizing the original function would result in spaghetti code. So, a second enormous function is written. The programmer scans the two functions and finds no large sections of code that are duplicated between the two functions, so he calls the job done.

    Naturally, code developed in this manner degenerates into an unmanageable mess as more and more variations are added. Anyone trying to program this way in the business world, for example, will quickly drown. However, people tend to get by with this style in the scientific computing world, where there are relatively few variations that cannot be handled by parameterization.

    The first thing that must be done to get out of this mode of programming is to assume that such variations will arise. Treat the first problem you solve as merely the first of many variations that you will be asked to solve in the future. (Either that, or make the opposite assumption and then completely rewrite your code when the second variation arises.) Then, analyze each variation in terms of the things it must accomplish. You can categorize things using problem domain concepts, solution domain concepts, whatever, as long as you can put names on things.

    (The tendency to think in human terms and give natural language names to things is a hindrance to mathematical insight, but it often generates effective ways to decompose code. Perhaps this is another reason why the "big function" style tends to plague scientific computing.)

    Once you have split the original task into conceptually separate chunks that can be given reasonable natural language names, split the program up into pieces that have those names.

    Voila, now you have a program made of small chunks that can be understood by other programmers. And, since each chunk has a reasonable name, programmers can limit themselves to reading the name and documentation of most pieces of the program, rather than reading every single line of code. This makes the code effectively shorter and easier to maintain, even if you never reuse the chunks to compose a new variation.

FORTRAN is not a flower but a weed -- it is hardy, occasionally blooms, and grows in every computer. -- A.J. Perlis

Working...