Who reads code samples?

Who reads code samples? (via Reddit)

Um, me?

Sorry, but I take code samples over mathematical formulas any day. The first example cited in the aforementioned post pretty much confirms this.

The Python example is something I can actually read and understand. I can follow it step by step, understanding what it does at each point. (And if I don’t, I can copy it to a file, stick print statements in it, and examine what it does exactly.)

The formula, on the other hand, is gobbledygook to me. I am a programmer, not a mathematician. To me, the formula is just a confusing mix of (Greek) letters and other symbols. It is essentially unreadable without knowing the context, i.e. the meaning of all those symbols and how they fit together.

(You could argue that the same is true for the Python code — yes, there is context there too, of course, but the difference is that I am expected to know it, as I am a programmer reading a book meant for programmers. It’s not a math book meant for mathematicians or students of math.)

The second example is even worse; the code is longer, but so is the formula, to the point where it’s not only completely unreadable to me, but also sufficiently overwhelming that I won’t even try to figure it out.

It’s cool if you can easily read the formulas, of course. More power to ya. But limiting the examples to formulas makes the text inaccessible to those who can’t read them, or have trouble doing so. Also, you’d still have to translate them to working code (because, again, this is a book for programmers :-).

(I have this book too, by the way… I kinda like it so far.)

Update: Come to think of it, I would much rather have learned math (what little I know of it, that is :-) illustrated with programming examples, rather than using formulas that are so dense that my eyes gloss over the moment I see them. Math would probably have been a lot more understandable and clear back then.

Which makes me wonder, *are* there books that teach math concepts using programming examples? I’m not talking about books like SICP that assume that you already have a firm grasp of mathematical knowledge.

33 Comments

  1. Louis-Philippe Huberdeau said,

    June 2, 2008 @ 10:03 am

    I guess you are right in the sense that the formula has to be translated. But to write the code in anything else than python, I have to translate twice. Translating the code directly from python to anything else is likely to waste important optimizations due to not using some libraries available, vector operations for example.

    The code samples contain way too many implementation details, like how the data is stored or how the calculation is performed to avoid extra loops. They are important when you want to write code, but the focus in this book, to me, should have been to understand the logic behind the algorithms.

    I am a trained programmer. I had to do math as part of engineering school. I truly believe in code, but I think most programmers should also learn to read maths. It allows to understand the logic behind the numbers in a much better way and improve it from there. With the raw code, I find it hard to read the high level logic. The code would likely remain a code snippet you simply copy and paste without understanding what it does.

  2. Hans Nowak said,

    June 2, 2008 @ 10:25 am

    “”"The code samples contain way too many implementation details, like how the data is stored or how the calculation is performed to avoid extra loops.”"”

    Hm, OK, that should (and could) have been abstracted away… although the ability to do so probably differs per example.

    OTOH, maybe being able to copy, paste and use a snippet, without the need to fully understand what it does, might actually be one of the goals of the book, even if not explicitly stated. I can imagine some people would just want to pick it up and use the code they need without much thought.

  3. josh said,

    June 2, 2008 @ 11:56 am

    The formulas given as examples are pretty simple one you get past the scary variable names… The only “interesting” notation is sigma for summation and vector magnitude. (I’m not a mathematician, btw.) Once you understand that, the formula expresses the same thing in a way that can be read more quickly, because it’s at a higher level.

  4. Hans Nowak said,

    June 2, 2008 @ 12:04 pm

    @Josh: I am aware these are not especially difficult formulas, as far as math goes… and in a long forgotten past I probably learned how to read them. Still, they’re too obfuscated for my tastes.

    (I agree that providing the formula provides many people with valuable information, but it shouldn’t be the *only* way to provide this information, at least not in programming books. :-)

  5. Mike Seth said,

    June 2, 2008 @ 1:04 pm

    And the problem with example code is that it is often written by people who aren’t aware of example code’s power over newbies. Worse yet, people seldom update it as the underlying project/API evolves…

  6. peter said,

    June 2, 2008 @ 1:08 pm

    you’re not a real programmer then. real programmers know math. computer science is an extension and application of mathematics. you’re gonna have to learn intensive higher-level math if you major in computer science at the university level.

    sorry, you lose.

  7. JB said,

    June 2, 2008 @ 1:08 pm

    I suppose nothing beats having both the code sample, and the formula. Structure and Interpretation of Classical Mechanics is a perfect example because they expose a history of mathematics books where the authors purposefully kept the formulas concise yet obscure just to keep with tradition.

  8. Silas Snider said,

    June 2, 2008 @ 1:09 pm

    Ever heard of “The Structure and Interpretation of Classical Mechanics” (full text: http://mitpress.mit.edu/SICM/)? The book presents classical mechanics both through equations, but also through programming assignments and simulations written in scheme.

  9. Jeffrey Dodge said,

    June 2, 2008 @ 1:10 pm

    Why do you make such a differentiation between mathematician and programmers? They’re both basically design tasks.

    In any case, being able to read a mathematical formula is probably the most useful skill a programmer can have. You also don’t see any chemists, physicists, or engineers complaining about mathematical formulas, and they’re not mathematicians either. Truth is, to be truly competent in a technical field, you need to know math.

    I’m a programmer, but I prefer the formula to the code. The code is overly verbose while the mathematical formula is not only easy to read, but concise. It’s not hard, you just have to know greek letters. Go read wikipedia.

  10. Robert Kraig said,

    June 2, 2008 @ 1:38 pm

    I’ve taken Calc 1 – Differential Equations. This math is not especially difficult to actually do. Rather the math notation looks especially difficult in that unless you’ve covered these concepts it looks like gobbledygook. Working with vector magnitude just takes however many dimensions you have sqrt(i^2 + j^2 + k^2) or just two dimensions sqrt(i^2+j^2) i = x, j = y, k = z or what ever you determine their axis to be, and it gives you a positive sum without direction.

    Also their equations are not listed in standard mathematical notation.
    function(var1,var2,var3) = some expression,
    not func(var1,var2,var3): some epxression

    Math is not programing.

    Also, anytime you see notation | something | it explicitly implies magnitude when you are dealing with math, as it’s implications require positive values. Magnitude is something generally required when you work with physics and try to determine forces of any type. They help you avoid components of axis. However, finding where this guy got that equation is a bit of a mystery to me. I looked up on wikipedia and they have many different types of guassian expressions/equations. Also in vector form i’m not sure on the notation of this function if it is in rectangular spherical cylindrical

  11. Caleb said,

    June 2, 2008 @ 1:41 pm

    I would really expect any competent programmer to have taken enough math that the formulas referenced here would be more or less obvious in meaning. Also, it is important for a programmer to understand the underlying math behind what they’re implementing (including the applicable symbolic notation). By only providing code samples, you really aren’t conveying the whole abstract concept — to echo Louis-Philippe, the samples give too many implementation-specific details. What it really comes down to is whether you want to understand the concepts, or just get working code.

    The deeper problem, I feel, is that there is a strong anti-math sentiment that is becoming more common among programmers and computer scientists. As a CS and math student, I am often surprised at the way my fellow CS students have an aversion to math; from what I’ve seen, better understanding of math (and the logic used to describe mathematical concepts) is key to getting a real grasp on computer science. You describe this exact reluctance toward math when you talk of “formulas that are so dense that my eyes gloss over the moment I see them”.

    Now, I would agree that programming examples can be useful in a programming book, but certainly not as a replacement for the actual formulas. Especially where the math gets hard, it is useful to see real code someone has written, but it is dangerous to rely on this for the reason stated above. As for supplementing math books with programming examples, I think this is useful in an applied math context, where we are actually concerned with implementation, efficiency, etc. Code, I believe, really doesn’t have much of a place in pure math, because we are often concerned with structures that are too abstract to talk about this way. In fact, this is why mathemeticians developed all of this “gobbledygook” symbolic notation: we need a concise, yet precise way to talk about these subjects at a much higher level than the logic understood by compilers. In fact, I have the opposite problem from yours; my eyes tend to gloss over tedious programming examples to find a formula which expresses all that verbiage in a compact way.

    I didn’t mean to come off as hostile or elitist here; I know a host of people who would agree with you whole-heartedly :)

  12. Guy Russell said,

    June 2, 2008 @ 1:47 pm

    The thing with Math formulas is that they are a way of citing a problem as opposed to executing the problem.

    For example, Epsilon is just a way of saying “the sum of elements of this particular shape and form”. As subtle as the difference might be, it is *not* saying “add the elements”.

    While some mathematical formulae are indeed very hairy (like the vertical bars in the exponent in the cited examples), they are in general much more simple than the associated programs.

    I’m not saying this in an attempt to make you learn math or shunning your ways. Just indicating a fundamental difference in what a program does and what a mathematical formula is.

    Solving a mathematical formula is a completely different thing to do btw.

  13. Michael said,

    June 2, 2008 @ 1:50 pm

    I get where you’re coming from, but I have to say, as someone who frequently deals with computational physics (constrained dynamics, thermodynamic simulations, etc.), I appreciate a set of formulas.

    Granted, algorithms tend to be a little more obtuse in formula form, since they’re not pure mathematics or physics. However, they do have a tendency to nicely abstract away the implementation, which can be very helpful, esp. if you’re target languages use vastly different idoms (logic, functional, oo, concurrent message passing, procedural, etc.).

    I think it’s just a matter of familiarity. Formulas are a fantastic way to express concepts, but one must be a native speaker. You are obviously a native speaker of your chosen programming languages, so they are a very comfortable medium for you to relate ideas. But could you comfortably consume ideas related in Prolog? Haskell? Smalltalk? J? Io?

    Mathematics tends to be thought of as a sort of lingua franca that can be consumed by anyone, regardless of background… except that it’s not entirely. It’s still just another language that the reader must have internalized in order to understand the idea being related, so you run in to the same problem. You either know it or you don’t, and if you don’t, then it’s just as frustrating as a Python programmer trying to understand Haskell for the first time.

    However, Mathematics, more than any other medium, has such a long history and is so universally adopted within acedemia that it tends to be a very general medium to distribute ideas to a broad, diverse audience.

    Take a look at the language of type theory some day. Talk about obtuse! But once you internalize it, it relates some fantasically difficult concepts in beautifully suscinct form, and with no dependence on understanding a particular concrete application of the theory.

    I guess I’m arguing for the gray area… It’s a nice place to be.

  14. Guy Russell said,

    June 2, 2008 @ 1:53 pm

    Must add: the subtlety I mentioned is important for things like simplifications. A very simplistic example is to define some value as “the sum of terms x” times “the sum of terms 1/x”. These things obviously create invariants, which when simplified in programs result in magic numbers or otherwise “weird” things. Things which don’t make sense conceptually, but just seem to work.

    A very good example of this is the infamous exp() magic number in the early quake source code, or more importantly encryption.

  15. gene t said,

    June 2, 2008 @ 1:53 pm

    “math concepts using programming examples? ”

    Yes, the Haskell books are quite nice, especially Doets, van Eijck “The Haskell Road to Logic and Math”

  16. 300baud said,

    June 2, 2008 @ 2:48 pm

    Math is “too obfuscated” for your “tastes”. Reddit, you failed me.

  17. jon said,

    June 2, 2008 @ 2:54 pm

    You don’t need to know math to program; you just need it if you want to program well.

  18. Bob said,

    June 2, 2008 @ 3:09 pm

    I left this link on the original, but it’s even more appropriate here:

    http://video.google.com/videoplay?docid=-2726904509434151616

  19. Cristian said,

    June 2, 2008 @ 3:35 pm

    Any programmer worth his salt should know math (not advanced stuff, but at least the basics). End of story.
    But, I do agree with the fact that sample codes were needed, since it was a book bout programming.

  20. dut said,

    June 2, 2008 @ 4:29 pm

    ::wading through the trolls and formula fanbois::

    i agree with hans. unless you’re doing formulaic coding like physics simulators for a living, there’s really no way a formula will be available that will help with the problem you’re likely trying to solve. i’ve done some statistical forecasting code where it helped to have formulas to show the basic mathematical relationships between the parts. that’s because i had a good understanding of the parts and no idea how to mathematically put them together in a meaningful way. the other 99% of coding i do is data storage and interface, as I’d guess it probably is for most coders out there.

    looking at formulas in general, they all use “single letter variables” for a reason — they’re meant to be surrounded by reams of text explaining what the variables are. with decent code samples, i can read and understand not only the math relationship being expressed, but often what the variables mean in relation to the problem and what some of the mathy bits do without any outside help. whether you know the symbolic representation of “vector distance” or not, “vect_dist(v1, v2)” is pretty unmistakable…

    hell, most code books i read are used as reference — i ONLY look at the code samples and can ignore the texty bits between. when someone comes up with a summation formula that can help me validate user input with a dozen cascading business rules on each elelment, hook a brotha up!!

  21. Jack said,

    June 2, 2008 @ 5:03 pm

    The problem with the math-taught-by-code idea is that not everything dealt with by mathematics is computable.

  22. Andrew Richards said,

    June 2, 2008 @ 5:10 pm

    Learning math through programming: http://www.cs.trinity.edu/About/The_Courses/cs301/math-for-the-layman/

  23. topi said,

    June 2, 2008 @ 9:45 pm

    who reads code samples? Well I do if I can pull myself away from my spore and mold collecting…

  24. przemek klosowski said,

    June 2, 2008 @ 9:46 pm

    It’s a false dichotomy—rather than having to choose between equations or code, how about having both? Check out Literate Programming: Donald Knuth came up with, what, 25 years ago? Equations help understand the algorithm on the conceptual level, but they often gloss over important details; code obviously determines the actual result. Literate Programming includes both forms.

    Mathematical notation is supposed to be precise, but since the symbols are defined to mean what the author wants them to mean, it can be tricky to parse.

    By the way, in a high-level language the code becomes quite close to the equations, e.g. in Octave/Matlab, you’d write exp( i * abs(v1-v2)) even if v1,v2
    were vectors or matrices—and it would even run quite fast because they use
    optimized linear algebra libraries, which typically outperform regular compiled loops because of cache and similar effects.

  25. Timmy Jose said,

    June 2, 2008 @ 10:39 pm

    I am sorry if I presume too much but those formulas weren’t exactly equations from Quantum Electrodynamics! I would even say that anyone with a college degree in Science and Math would be able to understand the notation, if not what it implies (or how to solve it). And what manner of programmer would say he’s not a mathematician in disguise? Seriously!

    I had read the blog in question and I found the first example in favor of the code but the second one totally biased me toward the equation.
    I would have rather preferred if the scheme was to have the equation and the code together. After all, it is a book meant for mathematical programming, so that makes sense.

  26. Nes said,

    June 3, 2008 @ 11:27 am

    I completely agree with you Hans. I’ll take code over math notation any day. Math may be shorter, but unless you know the assumptions, math notation is not detailed and specific enough. This exact problem is what lead Sussman to write the book “Structure and Interpretation of Classical Mechanics”. He thinks it is easier to explain scheme once and then physics using its notation than trying to explain all the oddball math formulas with inconsistent syntax and semantics.

  27. Nes said,

    June 3, 2008 @ 11:50 am

    Just to bring up an example: Consider a math formula of “(a*b)”. I have no clue what that is supposed to mean; a boolean expression, a matrix multiplication, an arithmetic operation, functional composition? If I see a Python expression “(quantity*price)” I have little doubt what that means. To begin variables have descriptive names instead of random Greek letters and second the operator is not overloaded to death.

  28. Phil said,

    June 3, 2008 @ 4:06 pm

    What about pseudo code? Comming from a Finite Element Context where ||v_1 – v_2|| might have possible meanings, depending an the space you are in. I think that giving the reader an example in some programming language has not only the advantage of clarifying the statement, but also makes it easier to estimate the runtime of an algorithm (count the necesarry operations).
    In most math books algorithms are also illustrated by pseudo code which is even standarized. From my point of view providing both the mathematical representation of the algorithm and an implementation is good style and makes a book much more readable.

  29. Michael Terry said,

    June 3, 2008 @ 6:26 pm

    Someone above said you’re not a real programmer if you don’t know math. I’m not sure what the cut off for “knowing math” is, but let’s say it’s linear algebra, since that’s the highest level you need at universities near me. My guess is that, if you did a study of programmers (real and fake), and controlled for IQ and experience, there’d be zero difference in productivity between those who knew math and those who didn’t. Discuss.

  30. Brooks Moses said,

    June 5, 2008 @ 1:04 am

    Michael: Obviously, you’re getting at the (probable) result that there is zero difference in productivity between “real programmers” and “fake programmers”.

    This is perfectly reasonable, and what one would expect from the term. Much as a “real man” or “real woman” is generally merely a man or woman who happens to fit the speaker’s patronizing ideas of what an ideal person is, and have little-to-nothing to do with actual abilities at anything important.

    I find Hans’s point interesting, as the math examples are entirely transparent to me, but the Python takes work and all runs together when I look at it at first. It’s worthwhile — because, as a _real_ programmer, I write documentation too! — to be reminded that not everyone sees the world the way I do.

  31. jon said,

    June 5, 2008 @ 3:18 pm

    Its the problem solving tools learned when doing math that come in handy during programming. “knowing math” is a bit of stretch; anyone claiming an absolute knowledge of maths is fooling themselves. However, productivity for said programmer would increase if they had a good understanding of the practical methods of mathematical problem solving.
    By taking math, they are forced to understand proper problem solving methods and the steps therein because of the complexity of the problems that need to be solved. For example, if a programming problem involved sets of data that need to be analyzed (many do!); the programmer would have an easier time if they used induction. Its just a tool that works really well for that problem; and someone would have a hard time learning it outside of a class dealing with set theory and logic.
    A great read is G Polyas “how to solve it”. Even a cursory glance at the introduction will help a programmer become more efficient. It’s a tool for math, usefully applied to programming.

  32. Daerin the Cluer said,

    June 9, 2008 @ 1:15 am

    Mathematics is readable to a broad, diverse audience WITHIN ACADEMIA. Chemists come from academia. Physicists come from academia. So do engineers. The majority of programmers, OTOH, are self-taught. Hence, the argument that mathematics is a “lingua franca” doesn’t apply to programmers.

    Programming languages are written in plain ASCII. This has a very specific advantage when it comes to (for example) a Python programmer seeing Haskell for the first time. If you don’t already know the programming language being used, you can look it up with a search engine, and learn the whole syntax in a matter of hours. You can even get a compiler or interpreter, which will allow you to verify for yourself that you really understand. Those operations that aren’t built into the syntax have names that can be entered into search engines. Programming languages support incremental, self-directed learning. You can learn enough of the language to solve the problem at hand, and learn the rest when the need arises.

    Math, using the traditional notation, doesn’t work that way. You have to study it for years to even be able to begin to read it. Every single new concept that gets introduced has its own unique syntax– and it’s not just a matter of a certain type of brackets: In math notation, typesetting attributes such as subscript, superscript, bold, italics, etc all have crucial meanings. These things are proof against being looked up in a search engine. You can’t look up what an upside-down “A” means (not that it always means the same thing). You can’t look up what it means when a “B” has two small letters in subscript next to it. Even if you could look up these meanings, the majority of the meaning of a math formula is implied– the formula itself is just a hint for those who are already in the know.

    Because math is a natural language in which most of the real knowledge is conveyed by context alone, there are no compilers in which you can automatically verify that you really understand it. This verification comes in only one form: The greedy, tenured college professor, and his greedy, money-guzzling institution.

  33. Lee Phillips said,

    June 10, 2008 @ 4:01 pm

    Your arguments have a mainly vocational thrust to them. Comfort with
    mathematics might not make you better at your job, but it will make you a more
    complete person, because mathematics is part of our cultural heritage. Mathematical notation
    is a way to express ideas; code examples are not a substitute, as a program is a
    set of instructions for a machine. (Although I am aware that ideas can be expressed
    within programs.) English is another notation for expressing ideas; one idea can be
    expressed with the word “car”; pasting in a photograph of a Buick does not
    accomplish the same thing. It is more concrete, but, unfortunately, also more concrete.
    A mathematical statement can be manipulated to produce other statements; if the first
    is true then the statements derived from it are true, too. The compactness of
    mathematical notation makes this manipulation powerful; we are showing how ideas
    follow from other ideas. Although programs can be transformed into equivalent
    programs, their very concreteness makes this more cumbersome and hardly practical
    except for small examples: one is just, then, doing mathematics with a bad notation.

RSS feed for comments on this post