News for hackers. Stuff that matters.
Re:80 chars per line is great by rdtsc on Monday March 25, 2013 @11:36AM (#5437549)
Not being able to fit the code in 80 columns easily (for me at least) is a smell test for potential problems code.

Now it depends on languages, I am thinking of Python, C and Erlang here.

If you use too many:

  full_project_name.useful_package_name.handy_function(...) 
is there a way to alias it to a function local name such as my_handy_function = <the above>. Maybe there isn't (again it depends on language here as well).

> I think this is partly because our text-based editors typically don't separate the meaning of our code from its layout.

You need a better editor. That is a solved problem. I like emacs the best. I imagine vi is just as good. It knows how to indent code based on the semantics. It knows you are in the middle of the list or just started a code block and does the right thing. (And I don't mess much with the default, an editor that does most thing reasonably form then on I assume whatever else it does to my code is now the new "right thing").

> A few functional languages promote ideas like “let x in y”

It kind of proves my point -- if you have functions that are going past the end of the screen, you need to think a bit about the code (also of course depending on language here, but most languages allow this). Break it up into functions. For functional languages like Erlang, they really encourage short functions that are 5-10 lines long. Use function clauses and pattern matching (with guards) to lay out the logical structure and so on.

Re:80 chars per line is great by Chris_Newton on Monday March 25, 2013 @11:54AM (#5437673)
is there a way to alias it to a function local name such as my_handy_function = <the above>

I agree, though of course a single function call is a trivial example. It would be nice if we made more use of intermediate variables for exposition to simplify complicated expressions more generally, too. But somehow this sort of idea rarely seems to get promoted whenever long lines come up in discussion. :-(

You need a better editor. That is a solved problem.

I respectfully disagree. Current editors, even the most flexible, rarely do more than automatically sorting out line breaks and indentation, and rarely provide more viewing options than collapsing blocks and jumping around to one cross reference or another. I am challenging the assumption that even the same symbols in the same order are necessarily appropriate in different contexts.

I think there is a parallel here with the idea that a variable or function name can be relatively short if it’s only used locally with the full context available, but it might be better to use a longer, more detailed name or to add full qualifiers like which package/module/scope the entity lives in if it’s going to be used more widely. This is a simple case of adaptation that many programmers would apply instinctively, but it illustrates the basic principle that using the same name/representation for an idea regardless of context isn’t always helpful.

Break it up into functions.

But at the moment, breaking something into functions for ease of presentation typically equates to breaking something into functions to establish an independent scope, to allow wider reuse, or for other non-presentational reasons.

For example, most languages offer no way to break down a moderately long pipeline algorithm into stages without separating each stage into its own function, complete with manually passing all of the context in and passing all of the results back. If many stages depend on much the same general context for that algorithm and the stages don’t make much sense outside of the algorithm as a whole, might we not consider some sort of lightweight, presentational way to split things up for better readability, without implying all the side effects and maintenance overheads of full functional decomposition?