Category: Programming Language
-
IntelliJ IDEA 14.1: Better equals(), hashCode() and toString()
PROBLEM Let’s assume we want to create the default equals(), hashCode() and toString() with the following bean:- Most IDEs, including older version of IntelliJ, have code generation features that would create something similar to this:- While it works, the generated code is usually crazy horrendous. SOLUTION With IntelliJ 14.x, it allows us to select templates Read More…
-
Underscore.js: Introducing _.chain(…)
PROBLEM Let’s assume we have the following JSON data:- What we want to do is to get all unique employees and ordered them by their names so that we get the following data:- SOLUTION 1: Less Elegant Underscore.js provides various functions that allow us to pull this off. While doable, the code is virtually not Read More…
-
IntelliJ IDEA: Generating Immutable Classes and Fields
PROBLEM By default, IntelliJ IDEA generates mutable classes and fields. One of the steps to achieve immutability is to make all classes and fields to be final. SOLUTION Making Fields Final Go to Preferences… -> Code Style -> Java -> Code Generation tab Under Final Modifier, check both Make generated local variables final and Make Read More…
-
Better Preconditions: v0.1.0
DEPENDENCY Introduction The goal of Better Preconditions is to provide a set of Java APIs that allows developers to create succinct, yet readable and testable preconditions. Why Write Preconditions? Let’s assume we have the following code:- Although this example is simple and trivial, every developer that looks at this code will interpret this API differently. Read More…
-
Guava: Testing equals(..) and hashcode(..)
PROBLEM Let’s assume we want to test the following equals(..):- A correctly implemented equals(..) must be reflexive, symmetric, transitive, consistent and handles null comparison. In another word, you have to write test cases to pass at least these 5 rules. Anything less is pure bullshit. SOLUTION You can write these tests yourself… or you can Read More…
-
Spring Security: Invalid CSRF Token ‘null’ was found on the request parameter ‘_csrf’ or header ‘X-CSRF-TOKEN’
PROBLEM With Spring Security 4.x, the CSRF protection is enabled by default. You may disable it, but to be more aligned with OWASP and the industry security standard, it’s best to leave this setting the way it is. Learn more about CSRF attack… To prevent this attack, Spring Security 4.x requires you to attach a Read More…