Java code formatting - is it allowed to open brace at the next line?
Is is allowed in java to use brace at the next line?
For example instead of:
public class Example1 {
public static void main(String[] args) {
int number = 10;
if (number > 5) {
System.out.println("Hi");
}
}
}
I would like to use code formatting like this:
public class Example2
{
public static void main(String[] args)
{
int number = 10;
if (number > 5)
{
System.out.println("Hi");
}
}
}
Is there a reason why not to use wide code formatting in java?
For example C# uses wide code formatting by default and I think it is more readable then java default code formatting.
Yes, it is allowed.
But is should be discussed with other team members. Entire project should use the same formatter and coding guidelines. It is a good practice to create such guidelines at the beginning of the project to setup ground rules how we format our code :)
Example of large java projects that use wide code formatting.
1. Maven
Maven is quite large java project that uses wide code formatting.
What is maven?
Maven is a build automation tool used primarily for Java projects.
link to src code of below class: here
package org.apache.maven.model.merge;
public class ModelMerger
{
public void merge( Model target, Model source, boolean sourceDominant, Map<?, ?> hints )
{
Objects.requireNonNull( target, "target cannot be null" );
if ( source == null )
{
return;
}
Map<Object, Object> context = new HashMap<>();
if ( hints != null )
{
context.putAll( hints );
}
mergeModel( target, source, sourceDominant, context );
}
// ...
protected void mergeModel_ModelVersion( Model target, Model source, boolean sourceDominant,
Map<Object, Object> context )
{
String src = source.getModelVersion();
if ( src != null )
{
if ( sourceDominant || target.getModelVersion() == null )
{
target.setModelVersion( src );
target.setLocation( "modelVersion", source.getLocation( "modelVersion" ) );
}
}
}
2. HikariCP
Fast, simple, reliable. HikariCP is a "zero-overhead" production ready JDBC connection pool. At roughly 130Kb, the library is very light.
Example of code formatting:
link to src code of below class: here
package com.zaxxer.hikari.metrics.dropwizard;
public final class CodahaleHealthChecker
{
// ...
private CodahaleHealthChecker()
{
// private constructor
}
private static class ConnectivityHealthCheck extends HealthCheck
{
private final HikariPool pool;
private final long checkTimeoutMs;
ConnectivityHealthCheck(final HikariPool pool, final long checkTimeoutMs)
{
this.pool = pool;
this.checkTimeoutMs = (checkTimeoutMs > 0 && checkTimeoutMs != Integer.MAX_VALUE ? checkTimeoutMs : TimeUnit.SECONDS.toMillis(10));
}
@Override
protected Result check() throws Exception
{
try (Connection connection = pool.getConnection(checkTimeoutMs)) {
return Result.healthy();
}
catch (SQLException e) {
return Result.unhealthy(e);
}
}
}
Other
There are many other projects that uses wide code formatting. If somone will find some more projects with wide formatting, we can create new question or wiki with such a list. I really like this topic.
Links
Related links I found during my searching: