46 AnswersNew Answer
Oneliners are really something between an art form and a mental exercise. They might look cool, but they are quite difficult to write and even more difficult to read. Good code must be simple and easy to comprehend for the casual human reader, so it should be structured, formatted and commented as good as possible. The reader is not supposed to spend hours to untangle unnecessarily compressed mumbo jumbo.
A lot of one-liners I see are not one-liners, they are just spaceless and escapeless codes lol true one-liners are indeed cool looking and impressive, but they do not increase nor decrease performance, space, or time in any way. The compiler ignores escapes and most spaces anyway, so one liners are treated about the same way formatted code is treated. I agree with Javier Felipe Toribio, one-liners should be treated as a fun challenge, not a standard. If your code is unreadable, poorly commented/documented it will be dismissed and criticized in the corporate world.
I prefer readability. Although oneline codes bring more of a challenge and mental satisfaction, Python's beauty is in it's indentations ;)
Oneliner is only for fun, as extra challenge. In real world jobs you must develope readable code in multiple lines with a lot of blank spaces
Clarity over cleverness.
I agree with 「ＨＡＰＰＹ ＴＯ ＨＥＬＰ」 Some "wanna be" oneliners , aren't oneliners. IMHO a good oneliner is made of lambdas, recursive calls, pure mathematical functions and streams concatenating its inputs and outputs with stateless variables in a functional programming way
That's not the standard way to write your code
For a formal usage, I only write one-liners for functions when it is short and clear enough.
There are one-liners and then there are one-liners. Some of them are easy to read and indeed a bit more performant like for example a list comprehension (with not much more than a single ternary expression). This also applies when there is a built-in function. Both times the interpreter does access the underlying C-code more directly. In such cases you definitely should use one-liners. Other one-liners are just for peeps who want to brag about them to newbies. Those very often use combinations of built-in functions which do the trick but can be even less performant than a longer code that is better fitted to the problem at hand.
Cbr✔[ Inactive ] I didn't say that the use of lambdas is mandatory in a oneliner. I said it's an option in a good one. I.e: Including user input. Like this : https://code.sololearn.com/cr32NWw9WY86/?ref=app
In addition to comprehensions and lambdas, f-strings are also a really handy tool for oneliners😁 Though I think they loosely qualify as OneLiners. This is one of my first, not quite the longest anymore https://code.sololearn.com/chu0Xaheiq4W/?ref=app
Above are some good one-liners, or at least not too bad, and let me show a very bad one. https://code.sololearn.com/cZGiT4dCgn54
Python uses semicolon but i don't think that counts as oneliner
Is good to one line a code but is better to use the normal format of writing codes
Javier Felipe Toribio i'm pretty good at oneliner and what do you mean by "a good oneliner is made of lambdas.." Lambdas are user-defined function xd, you don't always need them , for example https://code.sololearn.com/cUINJyXGYG31/?ref=app
Javier Felipe Toribio Cbr✔[ Inactive ] That is actually a very good example of the second kind of one-liner I mentioned... Hard to read and (much) less performant than a better fitting longer solution. (I can still see the appeal of trying to write such a monster.)
Depends on situations If you are in a competition of competitive coding , prefer one line codes as it takes less time is typing and are much more efficient BUT if you are making a project then go for formatted code as it increases readability of code and also helps you to identify bugs in your code easily
I would prefer using neat code so that it's look nice and professional
I try to understand one Cbr one line code and it is magic for me...
The thing with one-liners is, that it usually adds unnecessary complexity. For example the fibonacci code of Cbr✔[ Inactive ] could be solved in way less characters than her attempt. Thanks for proving 😉 https://code.sololearn.com/cn5faURSpwLi/?ref=app