Just got this web site with the Google Java Standards.
Useful points:
- Files are all in UTF-8
- Braces always used: void method() {}
- One statement per line: how many times have you seen someone a gigantic boolean condition all on one line?
- Enums formatted as if an array (no methods or docs)
- Testing caught exceptions can be ignored if, and named, "expected"
We can find issues with files not formatted correctly, especially when using a tool like Camel and Bean IO to convert files to Java objects. Using the base UTF encoding, we keep characters that can hurt the code from causing issues.
Braces are important for visually defining connected elements of the code and for asserting logic. It may be a couple key strokes less to exclude them, but someone working on that code may incorrectly change the logic and behavior of the code because they did not see the connected operations.
One statement per line helps readability, debugging and can help to deter code smell.
return (x == y && x== z || x== a || x ==b )
Remember here that java works with order of operations as top to bottom. A debugger will just error on the one line if there is an error, and as well, a Boolean condition that is nested in the line may make determining the overall outcome difficult. By breaking the conditions into individual comparisons, the debugger can reveal if the logic is accurate and which statement is causing the logic to be true or false. This might better, even more, refactored into other methods were the Boolean logic is more concise to one comparison rather than as a chain.
(x == y&& x== z|| x== a|| x ==b)
Testing that causes exceptions should name the exception as "expected" helps testers understand what the test is trying to accomplish or asserting. Something is being thrown in order to prove that incorrect parameters or other bad condition is being attempted.
@Test(expected = Exception.class)methodThatCausesException(){ //operation that causes exception}
No comments:
Post a Comment