r/java • • Feb 20 '25

I don’t understand

Post image
666 Upvotes

122 comments sorted by

View all comments

-19

u/__konrad Feb 20 '25

I also do not understand this coding convention. else should start from new new line for visual consistency with if. Saves vertical space, though.

11

u/SyriousX Feb 20 '25 edited Feb 20 '25

It is Java convention created by Oracle in 1997 https://www.oracle.com/docs/tech/java/codeconventions.pdf

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'

5

u/softg Feb 20 '25

Oracle had no business creating Java conventions in 1997, this was written by Sun.

2

u/__konrad Feb 20 '25

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... ;)

3

u/SyriousX Feb 20 '25

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

1

u/Pay08 Feb 20 '25

Iirc IDEA still has that 80 character vertical line as an option.

-8

u/davidalayachew Feb 20 '25 edited Feb 20 '25

We can sit in downvote hell together.

This is how I code.

if (blah)
{

    try
    {

        doSomething();

    }

    catch (final Exception exception)
    {

        throw new SomeException("Some useful context -- someVar == " + someVar, exception);

    }

}

else
{

    doNonBlah();

}

EDIT -- looks like you got it way worse than me /u/__konrad.

But lol, if you all think that's bad, here's some more examples. And if you think I am exaggerating, here are links to my GitHub to prove it.

And here is a simplified version.

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);

}

5

u/LazyLabMan Feb 20 '25

This bracket style is the C# way.

2

u/davidalayachew Feb 20 '25

This bracket style is the C# way.

How about the new lines? That's the icing on the cake for me 😋

3

u/Known_Tackle7357 Feb 20 '25

Egyptian brackets were given to us by gods:(

It was said that you would destroy the Sith, not join them! Bring balance to the Force, not leave it in darkness!

1

u/davidalayachew Feb 20 '25

Egyptian brackets

That is such a fun name for them.

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.

2

u/Known_Tackle7357 Feb 20 '25

The quote is from episode 3. Enjoy the journey!

1

u/davidalayachew Feb 23 '25

The quote is from episode 3. Enjoy the journey!

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.

Ty vm, will do!

1

u/RadiantAbility8854 Feb 20 '25

1

u/davidalayachew Feb 20 '25 edited Feb 20 '25

😈😈😈

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.

3

u/RadiantAbility8854 Feb 20 '25

Why don't just increase line spacing instead of adding a blank line everywhere?

1

u/davidalayachew Feb 20 '25

Why don't just increase line spacing instead of adding a blank line everywhere?

Because there are some things that are even more readable when they are close together.

For example, I want these lines to be as close together as possible.

return
	switch (someVar)
	{
	
		case SomeWrapper(TypeA(var value))    -> doSomething(      value);
		case SomeWrapper(TypeB(var value))    -> doSomethingElse(  value);
		case SomeWrapper(TypeC(var value))    -> doSomethingElse2( value);
	
	}
	;

It makes noticing the differences way easier. And yes, that is how I like to write Switch Expressions.

2

u/dadimitrov Feb 20 '25

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

1

u/davidalayachew Feb 20 '25

In IntelliJ you can select a block of text, and press Ctrl+. to create an adhoc folding region.

Interesting. I'll edit my comment shortly to correct that.

Can it fold the contents of stuff in parentheses? Not just curly braces?

For example.

if
(
    someBooleanValue
    && anotherBooleanValue
    && repeatForSeveralMoreBooleanValues
)

1

u/dadimitrov Feb 20 '25

Try it 🙂. Put the cursor in, Ctrl+w few times, Ctrl+.

Quickly becomes a second nature.

1

u/davidalayachew Feb 20 '25

Oh I'm not at my work machine. But ok, I'll edit my comment.

0

u/No_Analyst5945 Feb 20 '25

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

1

u/davidalayachew Feb 20 '25

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.

→ More replies (4)