News for hackers. Stuff that matters.
Re:Response to “Literate programming considered harmful” by quantumhobbit on Wednesday August 31, 2016 @07:41AM (#12397522)
The trend in programming lately seems to be less to no comments and less to no documentation; and it is killing the joy I take in programming. Without comments the only way the next guy has a chance to understand the code is if you stick to lowest common denominator patterns and frameworks. You can't do anything that hasn't been done before.

I think this is related to the proliferation of frameworks. I can't come up with my own pattern that best fits the problem, but I can select from a list of pre-approved patterns in our framework and shoehorn the problem into it. We only hire people familiar with the framework, so no need to document.

It really comes down to laziness. A literate program demands you put forth the effort to understand it and read the docs and code. Whereas a with a framework program you can copy and paste code from blog posts and stack overflow without any clue what is really going on.

Re:Response to “Literate programming considered harmful” by 3pt14159 on Wednesday August 31, 2016 @08:31AM (#12397768)
Completely disagree. I'm not going to summon up simple scarecrow arguments, but for software that makes money, I do not want your clever pattern with comments that saves a microsecond behind a 60 millisecond latency api. I want a simple for each loop. Where I want comments is where code gets hairy for a reason. For example, extra crafty SQL on some summary metrics page, sure, go ahead and comment that. But I don't want to see comments on more than 5% of a code base whose primary purpose is to make money.

And I'm not saying this because I've always had this view, I used to be too clever. I wrote a automatic caching and automatic windowing API client library. The thing was impossible for anyone else to alter because the mental model was too complex. Now I try to keep complexity only where it is needed and I try to keep the complexity quarantined from the interface as much as possible.

Huge projects like Rails can be clever while still making my life easier because they have an army of smart people finding the edge cases, but for most stuff that isn't the case.

Re:Response to “Literate programming considered harmful” by Chris2048 on Wednesday August 31, 2016 @10:15AM (#12398433)
> I'm not going to summon up simple scarecrow arguments, but for software that makes money, I do not want your clever pattern with comments that saves a microsecond behind a 60 millisecond latency api

Is that not a strawman argument?

Re:Response to “Literate programming considered harmful” by 3pt14159 on Wednesday August 31, 2016 @10:23AM (#12398491)
You are right, that isn't worded the way I intended (not the least because I misremembered the name). I meant to say code examples. For example:

    # double the counts.
    counts.map! { |count| count *= 2 }
Is the type of strawman I intended to avoid.
Re:Response to “Literate programming considered harmful” by dllthomas on Thursday September 01, 2016 @05:05PM (#12408874)
I once saw

    using namespace std;  // using namespace standard
Re:Response to “Literate programming considered harmful” by nothrabannosir on Wednesday August 31, 2016 @07:59AM (#12397595)
> I can't come up with my own pattern that best fits the problem, but I can select from a list of pre-approved patterns in our framework and shoehorn the problem into it.

Generally only true for burger flipping software. If your problem is unique, there won't be a framework for it.

Want to be creative? Try seeking out creative problems.

Everything has exceptions, but this rule of thumb seems to apply well, in my experience.

Re:Response to “Literate programming considered harmful” by DougWebb on Wednesday August 31, 2016 @08:30AM (#12397762)
If your problem is unique, there won't be a framework for it.

That doesn't always stop the pressure to force-fit your problem into a "preferred" framework. In a large organization, both the people defining the problem and the people enforcing the constraints on the solution can be different from the people who have to implement the solution, and may not be aware of or able to understand the mis-match.

Re:Response to “Literate programming considered harmful” by bunderbunder on Wednesday August 31, 2016 @08:22AM (#12397719)
If your problem is unique and requires creative solutions, and you work in an anti-documentation team, that's when things get truly nasty.
Re:Response to “Literate programming considered harmful” by qznc on Wednesday August 31, 2016 @08:26AM (#12397740)
Elsewhere known as "Academia" ;)
Re:Response to “Literate programming considered harmful” by Chris2048 on Wednesday August 31, 2016 @10:16AM (#12398450)
Which also can create documentation without code :-)
Re:Response to “Literate programming considered harmful” by ptero on Wednesday August 31, 2016 @08:47AM (#12397873)
I do not personally see this trend. In fact I see the proliferation of automatic-documenters and automatically checked minimum documentation requirements (which, as is often the case with blanket rules, tends to misfire producing volume but not quality; but this is another topic).

It seems literate programming assumes that the person doing development is the same as the one reading documentation. That is not always the case. And when it is not the case, the code embedded in documentation can hurt readability.

For example, I try to get all interface specs distributed, discussed and confirmed ASAP, usually WAY before the code is mature enough to be shown outside. Also, getting others to read, think about and comment on even short ICDs is difficult as is. Getting them to read early ICDs interspersed with early code? Not going to fly. My 2c.

Re:Response to “Literate programming considered harmful” by Jtsummers on Wednesday August 31, 2016 @09:06AM (#12397978)
I haven't figured out literate programming with a large group yet, but I do use it (with org mode) for my own projects (solo or small group). A nice thing, at least with org-mode, is that I can choose whether or not to export a particular bit of content.

  * This headline won't export      :noexport:
    These are some comments for the user and maintainers
    of this document. Describing our formatting and
    practices.
  * This headline will export
    And here's the text describing some message format.
  
    And here's a table showing the layout:
    |  Word  |  12-15  |  8-11  |  4-7  |  0-3  |
    |   0    | WordCnt | TimeTag| ...   |  ...  |
  
    And here's the source for this as a C struct, but
    it's a work in progress. So we won't export it yet.
    #+NAME: MSGXXX_struct
    #+BEGIN_SRC c :exports none
      typedef struct {
        ...
      } MSGXXX_t
    #+END_SRC
No one has to see that stuff until I'm done with it. Or, other tags could be used so I can explicitly select what gets exported depending on the audience. The customer doesn't need the code (at this point). But fellow devs (local, or at partner organizations) may.
Re:Response to “Literate programming considered harmful” by ptero on Wednesday August 31, 2016 @09:53AM (#12398279)
That is reasonable for a solo project, which naturally has a common style readable to the author.

In my (maybe skewed) experience though once 2 or more people start adding comments on the same place of code things can get very confusing quickly -- with mishmash of different styles, commented out comments, multiple copy-paste, etc., so the only way to understand what is going on is to delete the whole Kunstkamera and look only at the code.

Re:Response to “Literate programming considered harmful” by Jtsummers on Wednesday August 31, 2016 @10:15AM (#12398437)
NOTE: The plain text mode here doesn't do the org-file content I've included justice. Copy it into emacs and switch to org-mode to see how this'd actually look and work. And I agree, right now I've only figured out how to do this with myself and one or two other people. I use this for both work and side projects. I also tend to work on smaller code teams (max 3 people) so that helps if I want to use this at work on a specific project. You also have to get buy in on the tool (emacs and org-mode, in this case, which is a hard sell these days). I imagine there are ways to make this scale, but I've not figured it out yet. It'll require more tooling and finer-grained access for editing. Merging with this in git could be a pain in the ass with more than a couple people.

You can get code only views, no deletion necessary. Run tangle, which will spit out all the source files (which is part of the compilation/execution step anyways), or use (org-mode specific): C-c C-v C-v (org-babel-expand-src-block). The latter gives you the expanded (this assumes you're using something like noweb[0]) view of the code in a read-only buffer.

Org uses optional noweb notation to include blocks of code in other blocks:

  #+NAME: print_hello_world
  #+BEGIN_SRC c
    printf("Hello, World!\n");
  #+END_SRC

  #+BEGIN_SRC c :noweb yes :tangle hello.c
    #include<stdio.h>
    int main(int argc, char** argv) {
      <<print_hello_world>>
      return 0;
    }
  #+END_SRC
<<name>> will be substituted with the contents of the other block. The expand-src-block will give you a view with all substitutions made. C-c C-v C-t (org-babel-tangle) will generate hello.c (could be prefixed with a directory, my preferred way, so it'd be "src/hello.c").

A more useful case than the above, I have a switch/case statement. Each case gets broken into its own source block with documentation around it like:

  * Handling different message types
  #+BEGIN_SRC c :noweb yes
    switch(msg.type) {
      <<msg_101>>
      <<msg_102>>
      <<default>>
     }
  #+END_SRC
  ** Message 101
  Documentation
  #+NAME: msg_101
  #+BEGIN_SRC c
    case 101:
      // handle 101
      break;
  #+END_SRC
  ** Message 102
  Documentation
  #+NAME: msg_102
  #+BEGIN_SRC c
    case 102:
      // handle 102
      break;
  #+END_SRC
  ** Default case
  Documentation
  #+NAME: default
  #+BEGIN_SRC c
    default:
      // handle default
  #+END_SRC
Re:Response to “Literate programming considered harmful” by lmm on Wednesday August 31, 2016 @08:43AM (#12397849)
> The trend in programming lately seems to be less to no comments and less to no documentation; and it is killing the joy I take in programming. Without comments the only way the next guy has a chance to understand the code is if you stick to lowest common denominator patterns and frameworks. You can't do anything that hasn't been done before.

Not true. You just have to express it clearly. I view comments as a code smell - they generally indicate a part of the codebase that you know is poorly factored. If your language/framework doesn't allow you to express code clearly, get a better language/framework.

Laziness is one of the cardinal virtues of a programmer.

Re:Response to “Literate programming considered harmful” by bunderbunder on Wednesday August 31, 2016 @03:11PM (#12400687)
It really, really depends on what the comment is saying.

Comments that explain what the code is doing are usually a travesty. With the possible exception of hairy performance optimizations. Also implementations of algorithms that aren't well known -- oftentimes the original paper is dense, academic and behind a paywall, and the Wikipedia article are a muddled mess. You really owe it to your readers to give a sane and comprehensible explanation instead.

Comments that explain why the code is doing what it does, though, are HUGELY important. There's nothing worse than coming into a legacy codebase and seeing it littered with obvious special cases and workarounds that are all undocumented, so that you have no idea what they're for or whether they're obsolete or not. Skipping these kinds of comments leads to inexorable technical debt, because eventually you'll end up with a codebase where everyone is afraid to change the code.

Re:Response to “Literate programming considered harmful” by jcbeard on Wednesday August 31, 2016 @08:35AM (#12397793)
>The trend in programming lately seems to be less to no comments and less to no documentation

Hasn't this always been the case for young programmers? Seems you don't comment until you have to go back to code you wrote yourself a year later and make major changes....you then realize you should have documented it, either inline step-by-step or as a proper doxygen/javadoc style.

I can thank Dr. Leitner (CS 50/51) for enforcing comments in assignments. Took programming courses as an undergrad (I wasn't a CS major, but a Bio/Int'l studies one) in C, no documentation required...code was checked for correctness only. Leitner had his TA's go through and take off points for code that wasn't documented...which needless to say got me in the habit of documenting.

>It really comes down to laziness.

Not so sure. Laziness or lack of good habits? Perhaps a mixture of both.

Re:Response to “Literate programming considered harmful” by levelist_com on Wednesday August 31, 2016 @08:51AM (#12397895)
Funny enough, I had a C & C++ prof deduct points for comments with the argument that they were redundant and that the code should be self documenting. Then again, this is from a guy who never handed back assignments until the end of the semester and by then couldn't remember why he graded you the way he did. Would just respond with "you didn't understand the material".
Re:Response to “Literate programming considered harmful” by jcbeard on Wednesday August 31, 2016 @08:57AM (#12397937)
that's crap. self documenting code sometimes works...but not always. I love people that hand me HDL and say "it's self documenting." Then you open it, and wire names are "x_1245_to_y." Happened all the time when my students would turn in assignments. That stopped after they realized I graded the documentation too :).
Re:Response to “Literate programming considered harmful” by levelist_com on Wednesday August 31, 2016 @02:13PM (#12400277)
I agree and chose to take the deductions rather than avoid writing comments. By that point I had already been in the industry and I knew better; he has never left academia.
Re:Response to “Literate programming considered harmful” by the8472 on Wednesday August 31, 2016 @08:42AM (#12397837)
> Without comments the only way the next guy has a chance to understand the code is if you stick to lowest common denominator patterns and frameworks.

Or

  - read the tests for that method.
  - look at the commit log and associated bugs.
  - use identifiers that are self-explanatory to some extent. auto-complete makes using long names manageable.
  - step through it with a debugger to see what it does.
  - use an editor/IDE that shows additional info about unknown methods, assuming a statically typed language
     where it has total knowledge about available types.

Comments are certainly one way to understand code, but not the only way.
Re:Response to “Literate programming considered harmful” by Tarrosion on Wednesday August 31, 2016 @08:59AM (#12397943)
Many of the techniques you listed are subcategories of reverse engineering. In an ideal world, the code and comments are clear enough that no reverse engineering is necessary.

Given a bizarre method, would you rather see a clear comment or spend half an hour stomping through a commit log?

Re:Response to “Literate programming considered harmful” by falcolas on Wednesday August 31, 2016 @09:06AM (#12397976)
> In an ideal world, the code and comments are clear enough that no reverse engineering is necessary.

In an ideal world, perhaps. But in the real world, the comments contradict the code, the code attempts to use a software pattern which doesn't quite fit (and thus requires a large number of hacks to make it fit), so reverse engineering is your only real choice.

Aiming for an ideal world is never a bad thing, but even if I saw well documented code, I wouldn't trust it. The only difference is that after reverse engineering it anyways, I might feel a bit of surprise when I find that the comments were, indeed, correct.

Re:Response to “Literate programming considered harmful” by rtpg on Wednesday August 31, 2016 @08:18AM (#12397692)
Even in the bog-standard sort of software I write, I rarely find that choosing and applying a pattern is "mechanical".

Even if you tell yourself "oh, I'll use Observer", there are still so many details that can make your solution be boring and alright, or actually clean and magical. Foresight into how your structure will evolve, making things easier to test and read, separating the _right_ concerns.

I have copied stuff from Stack Overflow but always for solutions to a couple lines of code. I don't know what Stack Overflow answer I can copy for the general structure of my code...

Re:Response to “Literate programming considered harmful” by jcadam on Wednesday August 31, 2016 @08:07AM (#12397642)
> The trend in programming lately seems to be less to no comments and less to no documentation; and it is killing the joy I take in programming. Without comments the only way the next guy has a chance to understand the code is if you stick to lowest common denominator patterns and frameworks. You can't do anything that hasn't been done before.

Enter Golang... a language smaller than even ANSI C. It's readable because if you've worked with Golang for more than a week, you basically know every language construct you'll encounter.

As an aside, I've found Clojure to be one of the most "readable" languages I've ever seen, in that I can usually just go to the source code if I'm trying to figure out how to use a library I just pulled in from clojars.

Re:Response to “Literate programming considered harmful” by ctz on Wednesday August 31, 2016 @08:15AM (#12397675)
> Enter Golang... a language smaller than even ANSI C. It's readable because if you've worked with Golang for more than a week, you basically know every language construct you'll encounter.

That's not really what readability is to me. For example, I consider this idiomatic golang code (from the standard library, no less) to be entirely unreadable:

https://golang.org/src/crypto/tls/handshake_messages.go#L297

For me readability is more about suitable levels of abstraction. Languages can affect that by making abstraction longwinded, or expensive at runtime, or hard to compose.

Re:Response to “Literate programming considered harmful” by panic on Wednesday August 31, 2016 @08:28AM (#12397749)
It's ultimately subjective, but I think that code is readable: the variables are well-named and each line says exactly what it does. Abstraction makes it easier for you to think you understand what the code is doing, but if you want to really understand what's going on, you also have to understand how the abstraction is implemented. Straightforward code like this can be understood directly, without having to dig through layers of function calls.
Re:Response to “Literate programming considered harmful” by catnaroek on Wednesday August 31, 2016 @08:42AM (#12397839)
If you think that code is readable, could you correctly write all pre/post-conditions in between statements?
Re:Response to “Literate programming considered harmful” by panic on Wednesday August 31, 2016 @09:20AM (#12398050)
I think that's a little different. Reading code almost never involves analyzing it to that degree. Usually you're just trying to figure out what part is broken or where you need to add some new functionality.
Re:Response to “Literate programming considered harmful” by catnaroek on Wednesday August 31, 2016 @09:24AM (#12398081)
The meaning of an imperative program is given in terms of Hoare triples. If you can't produce such triples on demand, then you don't understand the program in question.
Re:Response to “Literate programming considered harmful” by EdiX on Wednesday August 31, 2016 @08:48AM (#12397882)
> For example, I consider this idiomatic golang code (from the standard library, no less) to be entirely unreadable

Really? It's a binary parser that's parsing data from a byte array and stuffing the results into a struct, it seems pretty straightforward.

Re:Response to “Literate programming considered harmful” by catnaroek on Wednesday August 31, 2016 @09:01AM (#12397952)
> It's readable because if you've worked with Golang for more than a week, you basically know every language construct you'll encounter.

If you've used Brainfuck for more than 5 minutes, you already know the entire language. Is it the most readable language ever designed?

> I can usually just go to the source code if I'm trying to figure out how to use a library I just pulled in from clojars.

Will a cursory read of the source code either tell you how the library deals with corner cases, or convince you that such corner cases don't exist?

Re:Response to “Literate programming considered harmful” by Jtsummers on Wednesday August 31, 2016 @09:39AM (#12398188)
And the source code doesn't tell you why something was done. Why is this message expected to be only 42 bytes long or less? Is there a specification for it? I can see that someone grabs two adjacent bytes, pulls out 12 bits from them and puts them into a variable called "version":

  version = ((data[0] & 0x0F) << 8 | data[1]);
But why? Is this still correct with the current specification of the message format? Who knows! And this is a best case, where "version" is a meaningful variable name.
Re:Response to “Literate programming considered harmful” by catnaroek on Wednesday August 31, 2016 @10:35AM (#12398592)
My thoughts exactly.
Re:Response to “Literate programming considered harmful” by jcadam on Wednesday August 31, 2016 @10:00AM (#12398325)
> If you've used Brainfuck for more than 5 minutes, you already know the entire language. Is it the most readable language ever designed?

Oh, come on... BF was specifically/pathologically designed to be unreadable.

> Will a cursory read of the source code either tell you how the library deals with corner cases, or convince you that such corner cases don't exist?

Um... no, not really (at least not a cursory glance), but readability certainly helps track down issues later when you do run into an unforeseen corner case. Language choice is all about trade-offs -- if things like this are what is most important to you, perhaps you would be happier with something like Haskell or Ada.

Re:Response to “Literate programming considered harmful” by catnaroek on Wednesday August 31, 2016 @10:34AM (#12398588)
> Oh, come on... BF was specifically/pathologically designed to be unreadable.

I was just exposing a flaw in your “readability” criterion. Another example: you can learn the rules that govern a given cellular automaton in 5 minutes, yet be completely unable to predict what said automaton will do when run.

> readability certainly helps track down issues later when you do run into an unforeseen corner case.

I'd rather not have issues, and always be sure that I've handled everything. Only applied formal logic can help.

> Language choice is all about trade-offs

Normally, understandability trumps all else. The only exception is when it's too badly in conflict with performance.

> if things like this are what is most important to you, perhaps you would be happier with something like Haskell or Ada.

Nah. GHC Haskell and Ada are both ginormous languages that I don't believe anyone could fit into their heads in their entirety. In particular, with GHC Haskell, I don't feel confident saying “this code does exactly what I want and nothing else”, because someone might enable an extension I haven't accounted for, and then hilarity ensues.

(And nobody uses the language specified in the Haskell Report.)

Re:Response to “Literate programming considered harmful” by jcadam on Wednesday August 31, 2016 @10:51AM (#12398730)
Truly. I used Ada professionally for several years and wouldn't want to go back. Even though programs that compiled had a much higher probability of just working compared to other languages, programming with Ada was both dull (or maybe that's just an Aerospace industry thing) and frustrating (that type system, ugh).

One thing I found while messing around with Haskell is that while learning and writing vanilla Haskell is one thing, deciphering Haskell written by others is quite another, given the propensity of library authors to use language extensions and stuff like TemplateHaskell.

Re:Response to “Literate programming considered harmful” by tome on Wednesday August 31, 2016 @01:46PM (#12400105)
> I don't feel confident saying “this code does exactly what I want and nothing else”, because someone might enable an extension I haven't accounted for

If you have any examples of code that, when an extension is enabled, neither fails to compile nor fails to behave in the same way it would be really interested to see them!

If I were to try to conjure some up the first place I would start would be interactions between scoped type variables and typeclasses.

Re:Response to “Literate programming considered harmful” by pjmlp on Wednesday August 31, 2016 @08:27AM (#12397746)
I guess that might be a trend in some inhouse development.

In our consulting projects there is always a minimal level of documentation specified by the customer as delivery artifacts.

They want to be sure the next set of guys are able to carry on when there is budget available again.

Re:Response to “Literate programming considered harmful” by nambit on Wednesday August 31, 2016 @08:02AM (#12397610)
I have to disagree with this. I've not found anything of the sort you describe here.

If you have some esoteric pattern in your code, then it's never worth the tradeoff between readability unless it is better performance wise.

Calling them the "lowest" common denominator patterns instead of best practices is being dishonest.

Re:Response to “Literate programming considered harmful” by sitkack on Wednesday August 31, 2016 @08:22AM (#12397713)
> some esoteric pattern

I have been at many sites where "esoteric" means things like first class functions (Python) or recursion. Not some hipster monad, y-combinator or method missing bullshit.

Re:Response to “Literate programming considered harmful” by pdpi on Wednesday August 31, 2016 @08:23AM (#12397722)
There's a middle ground between cookie cutter and esoteric.

While the OP's tone is perhaps a bit harsh, I've been in his shoes before, where a team that doesn't understand why a problem doesn't fit their go-to solution forces you to write a convoluted mess to shoehorn the problem into the solution. performance isn't even the problem, usually. Maintainability is.