Hacker News new | ask | show | jobs
by evincarofautumn 4204 days ago
There is a difference between “algebra with numbers” and “algebraic manipulation”. You do the latter all the time with code when you observe and use equivalences like this:

    if a then
      return b
    else
      return c

    ===

    return (if a then b else c)
I.e., “return” is distributive over the branches of the “if” expression, so you can factor it out.

When you see “bad” programmers redundantly specifying Boolean expressions like “x == true” instead of “x”, it’s because they haven’t internalised the fact that “if” takes any algebraic expression denoting a Boolean, not just a special sort of expression with “&&” and “||” and “==” and “<”.

Beginning programmers often struggle with learning the elements available in a language and how they can be composed—and that is exactly what an algebra is. I would be very surprised if an aptitude for algebra did not correlate with an aptitute for programming.

1 comments

This skill can help to write more concise code and refactor better but I wouldn't say it's the core component. Very well-functioning, maintainable code could certainly be written that was full of "== true" lines. That is preferable to indecipherable long-winded one-liners with no comments that are hard to parse.
That’s a false dichotomy. An indecipherable long-winded one-liner with no comments is not well factored code either.

I didn’t mean to imply that syntactic manipulation is the core component of programming, only that it’s a required skill, which beginners commonly struggle to learn. I’ve tutored a good number of beginner CS students, and a big issue is developing a general understanding of a language from the few concrete examples they’ve seen. If you’ve only ever seen “name.name()”, you may be surprised that “(expression).name()” is possible. If you’ve only ever seen “if (name == expression)”, you may be surprised that “if (name)” or “name = (name == expression)” are possible.

This article concerns how beginners learn to program, and I’m only arguing that I would expect an early understanding of the algebra of programs to be an advantage in learning how to productively manipulate programs. When you’re comfortable with the syntax, it is much easier to reason about the more important issue of semantics.

>only that it’s a required skill

What I'm saying is that it might not be required, just pleasant, given my example of someone who codes like that but otherwise writes well-structured good code.