One liner code is better or format code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 24

One liner code is better or format code

One liner code is better or format code coz it's fight between speed and understandable

18th Sep 2019, 6:28 PM
Huzaifa Khilawala
Huzaifa Khilawala - avatar
42 Answers
+ 46
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.
18th Sep 2019, 7:35 PM
Tibor Santa
Tibor Santa - avatar
+ 10
I prefer readability. Although oneline codes bring more of a challenge and mental satisfaction, Python's beauty is in it's indentations ;)
19th Sep 2019, 5:38 PM
Humayra🇧🇩
Humayra🇧🇩 - avatar
+ 9
Clarity over cleverness.
19th Sep 2019, 6:33 AM
Sonic
Sonic - avatar
+ 8
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
18th Sep 2019, 8:24 PM
Javier Felipe Toribio
Javier Felipe Toribio - avatar
+ 7
I agree with 「HAPPY TO HELP」 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
18th Sep 2019, 9:42 PM
Javier Felipe Toribio
Javier Felipe Toribio - avatar
+ 6
For a formal usage, I only write one-liners for functions when it is short and clear enough.
19th Sep 2019, 12:55 AM
Flandre Scarlet
Flandre Scarlet - avatar
+ 6
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.
19th Sep 2019, 5:14 AM
Thoq!
Thoq! - avatar
+ 5
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
19th Sep 2019, 5:41 AM
Javier Felipe Toribio
Javier Felipe Toribio - avatar
+ 5
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
19th Sep 2019, 5:56 AM
Trigger
Trigger - avatar
+ 5
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
19th Sep 2019, 6:03 AM
Flandre Scarlet
Flandre Scarlet - avatar
+ 3
Is good to one line a code but is better to use the normal format of writing codes
18th Sep 2019, 6:38 PM
Isaac Duah [Active!]
Isaac Duah [Active!] - avatar
+ 3
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.)
19th Sep 2019, 5:54 AM
Thoq!
Thoq! - avatar
+ 3
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
19th Sep 2019, 1:36 PM
Arsenic
Arsenic - avatar
+ 3
I would prefer using neat code so that it's look nice and professional
20th Sep 2019, 6:08 AM
Shadil saifi
Shadil saifi - avatar
+ 2
I try to understand one Cbr one line code and it is magic for me...
18th Sep 2019, 8:11 PM
Michal Kříž
+ 2
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
19th Sep 2019, 10:43 AM
Loeschzwerg
+ 2
Peter David Carter I know it's simple. I just appreciate the fact that they are *doing* stuff like that. Just the thought of keeping the community healthy😁
19th Sep 2019, 7:11 PM
Trigger
Trigger - avatar
+ 2
Hi! I'm new to this community, but i was used to code in Atari BASIC as a teenager Well, i used to one-line most of the times, and many of those programs were hard to read (even for me after a couple of months), then i got used to code more "formated" (Atari BASIC uses numbered lines) and comment main sections of the code, like define variables, read data, etc. so my programs became more readable Anyway, now that i`m back in the coding world i`ll one-line every time i feel it does help to save memory, time and resources, as i did with most of my BASIC prograns
19th Sep 2019, 9:21 PM
Rodrigo Perez de Arce
Rodrigo Perez de Arce - avatar
+ 2
Python does not use semicolons BUT needs FORMATTING .... HISS 😢
20th Sep 2019, 4:40 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
No format = No python code (Python does not use semi-colons) 😂
20th Sep 2019, 4:43 AM
Sanjay Kamath
Sanjay Kamath - avatar