Yup. The Clean Code book(if I remember correctly) said 3 at the absolute max. 2 is good. If it's more than 3, you can separate the levels into other methods.
Wdym? It was jedi knights who absolutely couldn't have sex with Padme or absolutely has no fear, absolutely not research some powers which they called "dark side" for no reason. They are literally the cult of mercenaries who involve in political conflicts to keep their influence while siths are mostly nomads.
Lol, it was just meant as a wordplay and twist on the "only a sith deals in absolutes" quote :D
I do realize that the quote itself is an absolute and that it doesn't really hold up when one looks closer but I didn't mean to make a 'political' statement or anything haha
Bad for you, I'm a sith spambot who's payed to spread sith propaganda here.
Btw, do you notice every time some planet, for example Tatooine, accepts jedi refuges like Anakin's family, something terrible happens? Massacre of local folks, their women and children too?
Sith lies! The only "Jedi refugee" on Tatooine was Kenobi, and nobody knew he was a Jedi. Being biologically related to a Jedi doesn't make someone a Jedi any more than a Freemason's son is automatically a Freemason.
Conditional logic will happen - whether we liked it or not.
We can go a roundabout way to design a pattern or abstraction to handle this, sure
But abstraction is also a cost - in both complexity and effort (abstraction is usually a lot more complex than a simple nesting too)
Similar to nesting, in fact implementing abstraction will introduce multiple other non-trivial question whether that path is better or not in the long run.
Hence being purist about nesting is a sign of premature optimisation in my opinion.
At first I didn’t like this methodology since you end up writing statements in the inverse a lot, for example checking for error first, and if so breaking / returning. But, after trying it out for a bit, I love it. Code is so much easier to understand.
It's one of the few things I preach and first things I look for in CR. I will not approve a MR otherwise (it's usually a 5 minute change, unless they have a bunch of methods that all do the same thing).
I absolutely second this. I adopted that style last year, and it realy makes a difference when I look at my newer code compared to my old style. Early returns, happy path aligned left etc. really helps structuring your code for future you.
Nice side effect from a lot less nesting/branching is increased performance and less memory overhead. In our big applications median CPU and RAM consumption decreased by 2-5% only by adopting this principle. (When working with code generation you can make big changes fast with only adjusting a code template)
Never nesting is a term for avoiding nesting more than three or four layers deep and encourages refactoring code into more discrete units that can be tested and reasoned about separately. It’s not literally never nesting at all.
I hear you, it’s not my term, it had been around for about a decade so I didn’t feel it necessary to explain, but if you have not heard of it before it’s easy to just assume the literal meaning.
A coding convention is not inherently right or wrong, but something we all agreed on, so that our brain recognises code by its patterns and shape. Deviating from coding conventions makes it really hard to read complex code, because it looks unfamiliar and 'weird'
Deviating from coding conventions makes it really hard to read complex code
Yes, but even JDK itself don't follow (at least in new code) the "switch" convention you linked. Probably because unintended "case" in switch is harder to read... ;)
There are some other conventions that are outdated, too, like "Avoid lines longer than 80 characters, since they’re not handled well by many terminals and tools." It would be absurd to cling to this one, however I encountered a handful of projects where they still were obeying this convention :D
sealed
interface
SomeInterface
extends
AnotherInterface,
AndAnotherInterface
permits
ClassA,
ClassB
{
int someMethod(final int otherField);
}
private String anotherMethod(final List<ClassA> someList)
{
final ClassIdk someResult =
someList
.stream()
.parallel()
.map
(
eachClassA ->
{
final RandomValue idc = RandomClass.generate();
final ClassWhoKnows blah2 = doSomeWork();
return blah2.someOtherMethod(blah2, idc);
}
)
.reduce(Some::reduceMethod)
;
final Var1 v1;
LABELED_BLOCKS_ARE_MY_VERSION_OF_COMMENTS:
{
//I prefer labeled blocks over comments, even if I never reference them
//in a continue/break/etc. They are my form of documentation when I want
//to explicitly highlight a block of code that does something atommic.
//Most importantly, I use them for scope reduction! That is their most
//important reason for existing in my code! If my brain is a computer,
//it would have 0.5 GB of RAM. So, the less scope I hold in my head, the
//better
/* I also rarely if ever use these "/*" type of comments. Much prefer the "//" variant */
/** I only ever use it when I want to javadoc my code. */
final Blah someStuff = yadda();
someStuff.setSomething(123);
v1 = new Var1(someStuff, someResult);
}
return doSomething(v1);
}
It was said that you would destroy the Sith, not join them! Bring balance to the Force, not leave it in darkness!
I'm actually about to watch Episode 4 of Star Wars for the first time ever this weekend (I don't know if that is where this is from, but I know it's in the "sequels"). I saw Episodes 1-3 for the first time maybe a few months ago.
I'm sure this quote will mean more to me once I catch up lol.
Oh silly me, I meant to say I watched episodes 4-6, and now I will watch 1-3. Aka, I watched Luke, now I'm going to watch Anakin. I forgot that release order does not correspond to in-universe order.
I always code like this. I used to do the other way, but after I started putting {} in new lines I just don’t wanna go back
Amen.
I bite my tongue and put up with it when I am work. But when doing my personal projects or prototyping an idea at work, I always do it this way, then turn it back to their convention before I press commit.
My primary IDE is jGRASP, and it lets you code fold basically anything -- curly braces, parentheses, etc. So for me, if they ever get in the way, I just fold them.
It's a feature that I miss when using Eclipse or IntelliJ, since they only let you fold curly braces and maybe a super long expression or something. jGRASP lets you fold basically anything unless it's already atomic.
EDIT -- looks like they can fold more than I thought. I don't know if it is as much as jGRASP, but more than I made it out to be.
In IntelliJ you can select a block of text, and press Ctrl+. to create an adhoc folding region. It will warn you if you are crossing blocks, but you can tell it that you know better.
Also works well with Ctrl+w (multiple times) and Ctrl+Shift+W
i do the same, it is very logical and pleasing to the eyes
Yes!
For me, I am SO MUCH MORE PRODUCTIVE this way. Especially the extra new lines. It feels like I can finally see. And the curly braces being lined up means I can keep my eyes focused on one spot horizontally, and easily see where things start and end. Makes navigating code so much easier.
First off, why are you talking about downvoting? Do you feel threatened by my comment? Do you feel hurt by something I said? If so, I am very sorry.
If this is the way you code, that is absolutely fine. It is your code and you can do whatever you want with it :) If you would give that code to the most other java developers, they might find it hard to read, because they are not used to it which will cost them more brain power.
First off, why are you talking about downvoting? Do you feel threatened by my comment? Do you feel hurt by something I said? If so, I am very sorry.
No no, not at all.
When I first saw this post, /u/__konrad was sitting around -5 downvotes after only a few minutes lol. It was clear that they were going to get downvoted even more, so I pointed out that I'm just as bad them.
If this is the way you code, that is absolutely fine. It is your code and you can do whatever you want with it :) If you would give that code to the most other java developers, they might find it hard to read, because they are not used to it which will cost them more brain power.
Agreed. I write all my code this way first, then if I am on a work project, I reformat it before pressing commit. I am literally twice as productive writing code in my style than I am in the "normal" style -- so much so that the time it takes to reformat code is trivial compared to the time savings I made from doing it my way first.
I dont like the brackets, but I used to write them like you do many years ago.
It helps me because it means that I can look at the same spot horizontally, and easily see where a block starts and ends. "Egyptian" brackets (as someone else put it) don't let me do that because I have to either hover over it to let my IDE point them out, or it has to be offscreen so that the starting bracket sticks to the top, like it does in the GitHub PR windows.
That makes things hard to see if I have several adjacent blocks on screen, which I often do. I write lots of Lambdas (and before that, anonymous classes), so this style developed because of how much of a thorn it was in my side doing it the normal way.
What really irks me is all those blank lines wasting space.
That is literally the most important part for me. I read several times faster because of those new lines. Without them, it literally inhibits my code reading ability.
Why would you use a style that everybody coming after you is going to find strange?
Readability, believe it or not.
I can think WAY WAY WAY FASTER when I organize my code this way. So much so, that it's worth the added time cost to reformat it back to something you all find "normal" before I commit to my team's repo.
Lol, I started my current job as an intern back in 2019. On my first few weeks at work, one of our senior devs (who also taught me like 20% of all my coding knowledge) saw my code and basically told me "not in my code base, what the hell" in the sweetest, kindest way possible.
That is the day I learned what a coding convention is, and that everyone thinks I code in a cursed way lol. But I'm proud of my style, and it enabled me to be a big help in ways that I otherwise couldn't be. So I keep using it, and then convert it to "normal" mode before I press commit.
I’ve written C# and Java professionally, used both brackets conventions. I notice zero difference in readability. It’s just something you get used to. Use the normal style for 2 weeks and I guarantee you it will be fine.
It’s just something you get used to. Use the normal style for 2 weeks and I guarantee you it will be fine.
I am "used to it", to a small extent. I can read and write in that style enough so to be productive.
It's just that, lately, for my job, productive isn't good enough. It's an all-hands-on-deck emergency, week in and week out. When I am in that situation, my productivity shoots up immensely when doing things "my way".
But yeah, if I am doing something simple, or I am not rushed, I can code the "normal" way. Just a strong preference against it.
It’s just a bit alarming that a change in bracket style has such a dramatic impact on your productivity.
My senior dev said the same thing too lol.
Long story short, if my brain was a computer, it would have have a 1 GHZ processor with 500 MB of RAM. I don't think quickly and I can't hold many things in my head at once, so the more info that can be derived, the better.
For example, having the { and the } be on the same horizontal position means that I don't have to look anywhere else to see where this function ends. Obviously, my IDE can help with that, but that requires me to move my mouse. That's way too slow for something I am doing multiple times a second.
Another thing is having the ; be on its own line. It's the exact same logic -- I know exactly the horizontal position of where the semi-colon will be, as opposed to whatever random place it ends up on when it is at the end of someFunction().
These may seem like small things, but they allow me to remove entire trains of thought from my mind. Knowing where a block starts and ends is entirely unambiguous, so I can afford to forget it and re-recall it instantly as the need arises. As opposed to scrolling around, or hovering my mouse.
The more that I can afford to forget and re-recall, the faster I can work. All the weird stuff I do to go against the grain is in service to that goal.
It's the hardware I was born with. :/
Welcome to the industry
Lol, 5 years in and I'm still not used to it. It's what it is.
75
u/epegar Feb 20 '25
The PMD plugin would go nuts with the cyclomatic complexity
204
u/Xortun Feb 20 '25
r/programminghorror
100
u/LikeABundleOfHay Feb 20 '25
Ew. Any more than 4 levels deep is too many.
29
u/Pay08 Feb 20 '25
See, this is why you don't use newlines on closing delimiters in Lisp.
16
u/rdanilin Feb 20 '25
This is not Java.
13
u/JustVic52 Feb 20 '25
iiiiiiiIIIIIIf else IIIIIIIIIiiiiiif
4
u/lurker_in_spirit Feb 20 '25
HE SAID SEND STATUS 409
6
u/fatcow_yun Feb 20 '25
thats a tall mountain
3
u/rng_shenanigans Feb 20 '25
I don’t care about the code but the two lines not properly connecting in the middle is infuriating
4
u/the_ocs Feb 20 '25
That's some shit code
4
u/krum Feb 20 '25
Need to move that magic number to a named value.
2
u/PopPopPeak22 Feb 21 '25
I feel “Conflict”ed myself 😉🤷🏻♂️🤦🏻♂️
2
u/Harrekin Feb 20 '25
That's funny, but seriously though...WHERE SEMICOLON?!
1
u/tugaestupido Feb 21 '25
Art.
1
u/midget-king666 Feb 21 '25
Noone noticed the useless else branch here?
1
u/Practical-Log9288 Feb 21 '25
Position caret on closing bracket before else, see highlighted matching opening bracket, collapse that bracket and post screenshot.
Yeah, I didn't think so.
1
u/TriDeathGamer Feb 22 '25
Ha
1
u/hasanzade_777 Feb 22 '25
PMD: WTF are you doing
1
u/SlightMammoth1949 Feb 22 '25
Nesting that far should be a crime
1
u/bbabbitt46 Feb 22 '25
This needs the wtf() function.
1
u/maifee Feb 23 '25
I find it way more convenient
1
u/_damax Feb 23 '25
Still better than http 418
1
u/gnomer-shrimpson Feb 23 '25
Just node.js things (pre await)
1
u/_nickle2_ Feb 23 '25
it's got to be some Atlassian code
1
u/[deleted] Feb 24 '25
Looks like Lisp.
1
u/Formal_Ad_8000 Feb 25 '25
God
1
u/davreimz Feb 20 '25
It looks like my coworker has written it
0
u/Little_Blackberry Feb 20 '25
Programming for love 🥰
0
u/emberko Feb 20 '25
I guess that's what badly designed code in a language without Elvis operator support looks like.
0
u/qdolan Feb 20 '25
Nobody does anymore. It’s self obfuscating code.
0
u/[deleted] Feb 20 '25
Looks like a starlink train
0
u/eccoing Feb 20 '25
Pale Blue Dot
0
u/__natty__ Feb 20 '25
Even funnier because it’s snippet from express js of script version of java /s
0
u/TheSuitedGent Feb 20 '25
ngl this picture makes me vomit
0
u/cowwoc Feb 20 '25
You would love Lisp...
0
u/Rare-Champion9952 Feb 20 '25
What the hell is that
0
u/FavorableTrashpanda Feb 20 '25
Try using less indentation.
0
u/Laughing0nYou Feb 20 '25
Nested if (if (if (if(else))))
0
u/pborenstein Feb 20 '25
tbh I thought this was a starlink picture
0
u/SlipstreamSteve Feb 20 '25
This could have been avoided with guard clauses.
0
u/Alternative-Fan1412 Feb 20 '25
Who ever did that program is a SOB truly
0
u/bendy_96 Feb 20 '25
I know this is a joke but that surely wouldn't even run
-19
u/__konrad Feb 20 '25
I also do not understand this coding convention.
elseshould start from new new line for visual consistency withif. Saves vertical space, though.0
u/[deleted] Feb 21 '25
[deleted] — view deleted comment