One liners, quick rant...

Hi fellas and fella-esses...

There are numerous threads on here where people ask for, and often expect, solutions to difficult problems solved using _one_liners_.

Why do they insist as such when it is virtually no different in execution time with well thought out indented code?

Is it me just being picky?

Your thoughts...

1 Like

One liners are "cool". In a perjorative sense. They cause unnecessary maintenance and development problems because of the difficulty in mentally parsing and debugging them. It is code obfuscation. It is the same thing as the code obfuscation contest for C code - except that contest is meant to be fun and ridiculous. 'one-liners' are not.

I've seen similar sentiments expressed here by a lot of folks.

I think many people are just into superficial things; and trying to be "cool" writing one liners is one of those mostly useless superficial things many people are into, I guess.

If my memory serves me right the SAP application offers you a function that lets you execute a single line of code and I guess there are other applications that offer you a similar function. I understand the design decission to keep the code within that application and not to use a script, especially when clusters and multiple applicationservers are involved.

I think one-liners tend to have a bad rep for the wrong reasons.

One-liners are typically used for programming on the command line, where a single line is your real estate. They are very useful as one-off, terse and personal small scripts for an ad-hoc parsing result, for example for general information, problem determination or security forensics. Developing such a small script is usually a lot quicker than editing a file, exiting, running it, re-editing, etc... Typically sysadmins use one-liners a lot for this purpose.

Once a one liner is working and if it proves to be useful for multiple occasions, then it can be turned into a script in a file and then vertical real estate can be used and short names can be replaced by mnemonic names and comments can be added for maintainability and it can be made fool-proof with error conditions. In a script file one-liners are to be a avoided.

These are just two different types of application.

If a one-liner is posted here, it shows the principle or mechanism that can be used to tackle a problem or create an application. The user is free to use it and turn it into a fully maintainable script if he so chooses, or execute it as such on the command line and get his/her result..

1 Like

I've learned the hard way that compressing my code to the extreme just confuses myself when I need to change it. Do I really want 2 hours of extricating myself for a few extra lines of clear code that cost no noticeable time.

There is a case for a one-liner if you can get a tool such as awk to do several things at once rather than calling a loop with multiple greps or cuts being called within it. For me it comes down to using it for processing efficiency. Even if I can make my code fit into fewer blocks on disk, it's just not worth it. If I do, then it gets an explicit comment, often with example code it is logically replacing. If I can adjust it in future in a longer method, then conversion would be simpler if I get my requirements clearer first.

It's the same with debugging logs for batch programs - write lots of info to the log so that when there is a problem, the trace is there. Just make sure you clean up old logs after a short time to avoid running out of space. It's far better than trying to trace or reproduce an error, potentially altering your data for the worse on each attempt or having to put trace information in when an error occurs and you are bleary-eyed.

Robin

Scrutinizer - sysadmins do put two or three commands together on the fly. I do. Pipe this command into this other command and then sort the output. That is essentially what the -exec predicate does for the find command.

Scripts coded as giant one-liners are out of bounds. Period. rbatte1 covers why really well.

I thought we were discussing the massive one-liners we see here a lot. We seem to want to define good and bad one-liners here. So let's say one-liners in scripts meant to be part of production should not be written as "multi-blobs" of piped commands.

Well, like you guys, I write a lot of code, and have a lot of sites, passwords, scripts, etc across many sites.. a lot of IT.

Even the best written code can be hard to understand after we have not touched it for a month or two, let alone a year or more.

So, I learned the hard way many years ago not to write "fancy one liners" and write well documented code and scripts.

Maybe I am being too simple here; but when I was a young lad my mother used to say 'Neo (well, not really Neo... haha), always put things back where you keep them in the same place, that way when you need them again, you don't waste time looking for it"..... and that is what I do not, even thought I never did it when I was young, of course!

The same is true with code... but the twist is different... always make sure your code is easy to read and self documenting as possible (clear variable names, simple clear logic etc, not fancy one-liners) because when we need to go back and work on that code again, we don't want to waste time trying to understand what we did before......

... and my mom would be proud of me, I always put my tools back in the toolbox now, always put my scissors back in the same place, always put my car and motorcycle keys in the same place after I use them.... and so my life is so easy as I never waste time trying to find that stuff anymore. LOL

Just keep it simple and life will be easy and less stress, and we then have more time for other things that are more fun.... that is my view.

When I started out on unix.com, I was awestruck by the ultra-compressed one-liners from the clever guys (Scrutinizer, you were my hero!!!). Over time, I learned from you guys and started doling out some good (I believe ;)) one-liners myself.

I viewed one-liners as a way to test my knowledge and bring in some supposedly esoteric features of the tool into the solution. But, now I realize that the people who will actually use that solution will hardly appreciate (or be able to) the "art". Also, the "compression" will only confuse myself some time in the future! So, it's better to make a trade-off somewhere...

Yeah... my view is that if you want (generally) "one liner" for a more complex task or expression, but it in a file and save it; and execute the script.

Of course, to each his own, and I'm not saying others should not be creative; I'm only saying that my experience is that I have so much code to manage, some from 10 years ago, and "elegant" or "fancy" one liners are hard to understand years after writing them, at least for me.

Anyway, I'm always going back and looking a legacy HTML and PHP and other admin scripts for this site and all the backup sites, etc. I try to keep is very clean, easy to understand, because with a site like this; you could be out have wine with friends and need to come back to the desk to fix a problem!

But I still use the unix pipeline for command line tasks, but nothing fancy or that I would keep for sysadmin.

Anyway, maybe I misunderstood the post or the intention here... which is possible since I have been coding like crazy lately!!

LOL

People figure they should be able to type 19 characters and solve a problem, after all, UNIX has everything right? Of course it's seldom that easy.

Well, Windoze is just "Right-click it" so why not?

Hi Neo...

Yup, I often do a search for something and come across an upload I have written and totally forgotten about. I read it and think did I actaully write that?

Going slightly off topic here with the second quote I was on a Python ML, (they were so obnoxious that I have long since left), and the big guns on there said there was no need to add comments as it was easy to work out what a python script does. I gave them this challenge:-
(Before reading the answer I wonder if the Python experts on here can work out what it does?)

# SomeSound.py

audio=file('/dev/dsp', 'wb')
def main():
	for a in range(0,25,1):
		for b in range(15,112,1):
			for c in range(0,1,1):
				audio.write(chr(127+b)+chr(127+b)+chr(127+b)+chr(127+b)+chr(127-b)+chr(127-b)+chr(127-b)+chr(127-b))
		for b in range(112,15,-1):
			for c in range(0,1,1):
				audio.write(chr(127+b)+chr(127+b)+chr(127+b)+chr(127+b)+chr(127-b)+chr(127-b)+chr(127-b)+chr(127-b))
main()
audio.close()

The solution is here:-
Amplitude Modulation - Tremolo, Was:- An Audio/Sound Snippet For Linux... ;o) << Python recipes << ActiveState Code

1 Like