[Tutor] this group and one liners

avi.e.gross at gmail.com avi.e.gross at gmail.com
Wed Jul 6 11:50:26 EDT 2022


Alan,

Well said.

I differentiate between code, often what we call one-liners, that is being done to impress or to solve a puzzle as in how compact you can make your code, as compared to  fairly valid constructs that can allow programming to happen at a higher level of abstraction.

As has often been discussed, quite a bit is done in languages like python using constructs like:

[ x+2 for x in iterator ]

In general, all that is can be written with one or more loops. Someone who started off programming in other languages may hit a wall reading that code embedded in a one-liner. I have written code with multiple nested constructs like that where I had trouble parsing it just a few days later.

Functional programming techniques are another example of replacing a loop construct over multiple lines by an often short construct like do_this(function, iterable), again sometimes nested. You can even find versions that accept a list of functions to apply to a list of arguments either pairwise or more exhaustively.

The same arguments apply to the way some people do object-oriented techniques when not everything needs to be an object. So, yes, you can make a fairly complicated object that encapsulates a gigantic amount of code, then use that in a one-liner where everything is done magically.

I will say this though. Code that is brief and fits say on one screen, is far easier for many people to read and understand. But that can often be arranged by moving parts of the problem into other functions you create that each may also be easy to read and understand. A one-liner that simply calls one or two such functions with decent design and naming, may not qualify as using the smallest amount of code, but can be easy to read IN PARTS and still feel pleasing.

Specifically with some of the code I shared recently, I pointed out that if max() failed for empty lists as an argument, you could simply create a safe_max() function of your own that encapsulates one of several variations and returns a 0 when you pass it something without a maximum. 

BUT, I think it must be carefully pointed out to students that there is a huge difference between using functions guaranteed to exist in any Python program unless shadowed, and those that can be imported from some standard module, and those that a person creates for themselves that may have to be brought in some way each time you use them and that others may have no access to so sharing code using them without including those functions is not a good thing.

- Avi


-----Original Message-----
From: Tutor <tutor-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of Alan Gauld
Sent: Wednesday, July 6, 2022 4:22 AM
To: tutor at python.org
Subject: Re: [Tutor] this group and one liners

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

_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list