[Tutor] this group and one liners

Alan Gauld learn2program at gmail.com
Wed Jul 6 04:21:44 EDT 2022


On 06/07/2022 03:31, avi.e.gross at gmail.com wrote:
> But for teaching a relatively beginner computer class, ...
> the goal often is to do things in a way that makes an algorithm clear,

Never mind a beginner. The goal should *always* be to make the algorithm
clear!

As someone who spent many years leading a maintenance team we spent many
hours disentangling "clever" one-liners. They almost never provided any
benefit
and just slowed down comprehension and made debugging nearly impossible.

The only thing one-liners do is make the code shorter. But the compiler
doesn't
care and there are no benefits for short code except a tiny bit of saved
storage!

(I accept that some ancient interpreters did work slightly faster when
interpreting a one liner but I doubt that any such beast is still in
production
use today!)

…
> I mean sure, there are lots of extra variables sitting around, maybe some if statement with an else and maybe some visible loops. But they show how the problem is being approached and perhaps allow some strategic debugging or testing with print statements and the like.

They also allow interspersed comments to explain what's being done

And make it easier to insert print statements etc.


> An interesting variation I appreciate in languages with some form of pipeline is to write the algorithm forwards rather than nested. Again, this is easier for some to comprehend. I mean using pseudcode, something like:

Smalltalk programmers do this all the time(in a slightly different way)
and it makes code easy to read and debug. And in the Smalltalk case
easier for the optimiser to speed up.

> The point I am making is whether when some of us do one-liners, are we
really helping or just enjoying solving our puzzles?

In my experience one-liners are nearly always the result of:

a) an ego-trip by the programmer (usually by an "expert")

b) a misguided assumption that short code is somehow better (usually by
a beginner)

Very, very, occasionally they are an engineering choice becase they do
in fact give
a minor speed improvement inside a critical loop. But that's about 0.1%
of the one-liners
I've seen!


-- 

Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list