Skip to content

Instantly share code, notes, and snippets.

View tkfx's full-sized avatar

Alex Zhitnitsky tkfx

View GitHub Profile
private static void runSomeAlgorithm(Graph graph) {
// do something with graph
}
private static void runSomeAlgorithm(Graph graph) {
if (graph == null) {
return;
}
// do something with graph
}
// Bad.
// - Field names give little insight into what fields are used for.
class User {
private final int a;
private final String m;
...
}
// Good.
private static final Logger logger = LoggerFactory.getLogger(Class.class);
if (x == y && a > 10) // bad
if ((x == y) && (a > 10)) // good
// simple constructor
//
private final int x;
private final int y;
private final String z;
public MyClass(int x, int y, String z) {
this.x = x;
this.y = y;
this.z = z;
// Bad.
// - Line breaks are arbitrary.
// - Scanning the code makes it difficult to piece the message together.
throw new IllegalStateException("Failed to process request" + request.getId()
+ " for user " + user.getId() + " query: '" + query.getText()
+ "'");
// Good.
// - Each component of the message is separate and self-contained.
// - Adding or removing a component of the message requires minimal reformatting.
@Entity
@Table(name = "author", schema = "bookstore")
public class Author implements Serializable {
}
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "authorSequence")
@SequenceGenerator(name = "authorSequence", sequenceName = "author_seq", initialValue = 1000)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "authorSequence")
@Column(name = "id", updatable = false, nullable = false)
private Long id;