This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (x == y && a > 10) // bad | |
| if ((x == y) && (a > 10)) // good |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static final Logger logger = LoggerFactory.getLogger(Class.class); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Bad. | |
| // - Field names give little insight into what fields are used for. | |
| class User { | |
| private final int a; | |
| private final String m; | |
| ... | |
| } | |
| // Good. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static void runSomeAlgorithm(Graph graph) { | |
| if (graph == null) { | |
| return; | |
| } | |
| // do something with graph | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static void runSomeAlgorithm(Graph graph) { | |
| // do something with graph | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static int isOpt(int x, int y) { | |
| int veryHardCalculation = 0; | |
| if (x >= y) { | |
| veryHardCalculation = x * 1000 + y; | |
| } else { | |
| veryHardCalculation = y * 1000 + x; | |
| } | |
| return veryHardCalculation; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static int isOpt(int x, int y) { | |
| int veryHardCalculation = 0; | |
| if (x < y) { | |
| // this would not require a jump | |
| veryHardCalculation = y * 1000 + x; | |
| return veryHardCalculation; | |
| } else { | |
| veryHardCalculation = x * 1000 + y; | |
| return veryHardCalculation; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 0x00007fd715062d0c: cmp %edx,%esi | |
| 0x00007fd715062d0e: jge 0x00007fd715062d24 ;*if_icmplt | |
| ; - Opt::isOpt@4 (line 117) |