question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve formatting of method will lot of parameters and with exception

See original GitHub issue

Source:

    public LoggingConfiguration(@Value("${spring.application.name}") String appName,
        @Value("${server.port}") String serverPort,
        JHipsterProperties jHipsterProperties,
        ObjectMapper mapper)
        throws JsonProcessingException {
        // ...
    }

Formatted with prettier 0.4.0:

    public LoggingConfiguration(
        @Value("${spring.application.name}") String appName,
        @Value("${server.port}") String serverPort,
        JHipsterProperties jHipsterProperties,
        ObjectMapper mapper
    )
        throws JsonProcessingException {
        // ...
    }

I’m not really sure what the result should be, but the position of ) after the last parameter is a bit strange in regards to the exception. Maybe:

    public LoggingConfiguration(
        @Value("${spring.application.name}") String appName,
        @Value("${server.port}") String serverPort,
        JHipsterProperties jHipsterProperties,
        ObjectMapper mapper
    ) throws JsonProcessingException {
        // ...
    }

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jhabercommented, Nov 13, 2020

Prettier is an opinionated formatter, and it seems like one of those opinions is that either all elements in a group should line break, or none should (and prettier-java inherits this behavior from prettier since it’s a prettier plugin). The example above might look tidy because all of the arguments are the same type and happen to align nicely, but it quickly becomes text soup when that’s not the case, for example:

public void doRemoteFetch(RemoteDataFetcher remoteDataFetcher,
    JsonDeserializer jsonDeserializer, CustomHeadersProvider customHeadersProvider,
    ResponseSignatureVerifier responseSignatureVerifier, ErrorHandlingStrategy errorHandlingStrategy
)

vs.

public void doRemoteFetch(
    RemoteDataFetcher remoteDataFetcher,
    JsonDeserializer jsonDeserializer, 
    CustomHeadersProvider customHeadersProvider,
    ResponseSignatureVerifier responseSignatureVerifier, 
    ErrorHandlingStrategy errorHandlingStrategy
)

But like I said before, this style is just an opinion so there’s not much use in debating which way is objectively “right”. However, this opinion is pretty much at the core of prettier and unlikely to change. If you don’t like prettier’s opinions, google-java-format might be of interest, it’s another formatter that’s a bit less opinionated and also has IDE plugins available.

1reaction
sanjayrawat1commented, Nov 13, 2020

@MichielBugherJelli suggestion looks pretty good to me. Why keep every element to next line once reached print width. A smart wrap respecting print width value would be perfect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - How to format methods with large parameter lists
Personally, I prefer to break after each parameter if I have to break at all, i.e. public static Foo makeFoo( Foo foo, Bar...
Read more >
CA2241: Provide correct arguments to formatting methods
Learn about code analysis rule CA2241: Provide correct arguments to formatting methods.
Read more >
Are there guidelines on how many parameters a function ...
I've never seen a guideline, but in my experience a function that takes more than three or four parameters indicates one of two...
Read more >
9 Best Practices to Handle Java Exceptions - Stackify
Handling Java exceptions isn't easy, especially for beginners. Read this post to understand exceptions and best practices for using them.
Read more >
How To Use String Formatters in Python 3 - DigitalOcean
This tutorial will guide you through some of the common uses of string formatters in Python, which can help make your code and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found