#CleanCodeDebate: Can we skip the one-liners? 🤔
Hey there techies! 👋 Let’s talk about one-liners in code. Personally, I find them kinda irksome. Have you ever been staring at a screen, trying to decipher a one-liner that looks like a jumble of characters from a secret code? Yeah, me too. 🤯
But here’s the million-dollar question: Are one-liners a must for creating “clean” code? 🧹 Or can we ditch the trend and stick to good ol’ fashioned line breaks and indentation? 💻
I’m curious to hear your thoughts. Do these one-liners really make a difference in readability and maintainability, or are they just a headache waiting to happen? 🤯 Let’s dive in and uncover the truth together!
So, what do you say? #OneLinersOrNah?
📊 Let’s make this fun! Cast your vote below and let’s settle this once and for all! 🗳️
Drop a comment with your thoughts and let’s get this conversation rolling! 💬 #CleanCodeRevolution
No? IMHO, I don’t think clean code advocates for this at all either, so I’m slightly confused about the connection drawn between clean code and overly dense code.
The exact opposite.
Clean code, as a practice, is all about emphasizing readability over other factors like aesthetics or even performance (within reason). That kind of dense hard to read code is considered a smell.
In short “clean” code is a code that easy to read and change. If you want to know more about clean code read R.C. Martin’s book that is called “Clean code” it’s absolute classic and it’s still valid today. But “one liners” are not necessarily can be considered clean code, it depends. For example, ternary operator can simplify the code or it can make code harder to read, depends how you use it. But every time you write a “one liner” or other “clever” solutions just ask your self a question “does this make easier for other person to read my code or am I just stroking my ego here?”
I love a one-liner but the only thing they’re really good for is making your teammates hate you.
What kind of 1 liner?
A triple nested ternary operator instead of some if statements? No
Something like `array.filter().flatMap()` instead of a 20 line nested for loop? Absolutely yes, in my opinion.
from the book “Clean Code”: “Functions should be small. The rule of thumb with functions is that they should be small, then they should be smaller than that.”. i personally really dont like the book, but my interpretation of the book in this case is that it isnt advocating for “physically” small functions, but “conceptually” small functions
“When we write our functions to be long and complex, we make it difficult to isolate the cause of failures and to write effective tests”, therefore, making a complicated, hard to isolate one liner is worse than expanding it because its the same thing, just harder to debug and read / more complex (complexity as in density of code).
in that case hes most certainly talking about functions as a single unit, but im expanding the idea to inside the function as well, if you put variables on different lines, you can debug them easier: looking at them in a debugger, or being able to simply std::cout << variable << std::endl; which to do you would have to take your code OUT of the ternary, turn it into an if statement, then write the print in order to do. by writing the if statement you save code rewriting and increase its debug-ability.
same thing with temp variables, by writing it out you can more easily check everything via debugger or print statement whenever you want to make sure everything is working. for anything involving math, ill OFTEN start with “int result{};” for example, then do “result = num;” and “return result;” which makes debugging a BREEZE.
“It turns out that humans are generally capable of understanding only a few concepts at a time. Our brains are not designed to juggle too many things at once. Thus, the shorter and more focused a function, the easier it is to understand.” one liners force us to have to keep EVERYTHING in our head for the entirety of the line, whereas breaking it into multiple lines you can chunk things by scope, or look at a variable instead of having to remember the value of an expression. you can look at the type instead of having to remember and keep in your head what type it is.
this also aligns with temp variables, you dont need to keep in your brain the state of your program as much because you can just look an SEE the state in the form of the temp variable, you dont have to remember what numbers mean because the variable SAYS what they mean, so you can focus more on the flow of the program than the weeds of it.
“Small functions are easier to read and understand. They are also easier to maintain and modify.” he seems to conflate small functions with being “easier to read and understand” and “maintain and modify” which in the case of oneliners, can be *very wrong to say*. again with ternarys, if you wanna add to them you gotta make them into an if statement which is a pointless rewrite that you couldve just done from the get-go. so basically, writing functions in a way thats hard to read and understand / modify goes against the whole purpose of making them small in the first place, to increase readability.
the reason for the small functions things in the first place imo is to make sure the function does “only one thing”, and small functions tend to be more targeted to do that one thing (in his experience), not JUST to have code be small for the sake of being small.
my entire interpretation, while it isnt really said in the book, is that hes advocating for small “conceptual” size, because of the fact that he cites the size of the function as giving benifits like readability, maintainability, changability etc… which simply is not the case with dense one liners.
that is, use if statements instead of crazy ternarys because theyre conceptually the same “size” (i.e. they do the same thing in the same way, just written differently), and the ternary version is harder to reason about which defeats the entire purpose of writing small functions.
so to conclude, no i dont think that would be bad practice at all.
Depends. Writing cryptic one liners because… reasons? No. Writing the least amount of legible code to get the job done? Less points of failure.
You should only do two or three things per line. If you’re chaining a bunch of functions at least have the decency to put them on separate lines even though it’s actually one statement. It should be plainly obvious what every line does.
Leetcode brain
Exactly opposite. Write verbose code for readability and maintainability. Also copy paste is ok if you are trying to cram too many business logic in one service
Never heard anyone call one-liners clean code, I think we all know they’re a guilty pleasure for some of us though.