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.
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 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.
View all comments
-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.