Minor cleanup
This commit is contained in:
parent
e2126624f4
commit
73eb6333d2
12 changed files with 338 additions and 61 deletions
|
@ -23,15 +23,15 @@ package java;
|
|||
* @since 1.0
|
||||
*/
|
||||
public class BestPractices {
|
||||
private final String ip = "127.0.0.1"; // not recommended
|
||||
private StringBuffer buffer; // potential memory leak as an instance variable;
|
||||
private final String ip = "127.0.0.1"; // not recommended
|
||||
private StringBuffer buffer; // potential memory leak as an instance variable;
|
||||
private String[] x;
|
||||
|
||||
void bar(int a) {
|
||||
switch (a) {
|
||||
case 1: // do something
|
||||
case 1: // do something
|
||||
break;
|
||||
default: // the default case should be last, by convention
|
||||
default: // the default case should be last, by convention
|
||||
break;
|
||||
case 2:
|
||||
break;
|
||||
|
@ -47,6 +47,4 @@ public class BestPractices {
|
|||
name = name.trim();
|
||||
System.out.println("Hello " + name);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ package java;
|
|||
public final class CodeStyle {
|
||||
final int FinalField = 1;
|
||||
private int x;
|
||||
private int y; // class cannot be subclassed, so is y really private or package visible?
|
||||
private int y; // class cannot be subclassed, so is y really private or package visible?
|
||||
|
||||
// missing constructor
|
||||
|
||||
|
|
|
@ -26,10 +26,6 @@ public class Design {
|
|||
String field;
|
||||
int otherField;
|
||||
|
||||
void foo() {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
|
||||
public void bar(int x, int y, int z) {
|
||||
if (x > y) {
|
||||
if (y > z) {
|
||||
|
@ -39,4 +35,8 @@ public class Design {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
void foo() {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,18 +37,18 @@ public class ErrorProne {
|
|||
}
|
||||
}
|
||||
|
||||
void foo() {
|
||||
try {
|
||||
// do something
|
||||
} catch (Throwable th) { // should not catch Throwable
|
||||
th.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
void bar() {
|
||||
try {
|
||||
// do something
|
||||
} catch (NullPointerException npe) {
|
||||
}
|
||||
}
|
||||
|
||||
void foo() {
|
||||
try {
|
||||
// do something
|
||||
} catch (Throwable th) { // should not catch Throwable
|
||||
th.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,12 +33,6 @@ public class MultiThreading {
|
|||
sdf.format("bar"); // poor, no thread-safety
|
||||
}
|
||||
|
||||
void foo() {
|
||||
synchronized (sdf) { // preferred
|
||||
sdf.format("foo");
|
||||
}
|
||||
}
|
||||
|
||||
Object obj() {
|
||||
if (baz == null) { // baz may be non-null yet not fully created
|
||||
synchronized (this) {
|
||||
|
|
|
@ -29,7 +29,6 @@ public class Performance {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private boolean checkTrimEmpty(String str) {
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
if (!Character.isWhitespace(str.charAt(i))) {
|
||||
|
@ -41,7 +40,7 @@ public class Performance {
|
|||
|
||||
void foo() {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("a"); // avoid this
|
||||
sb.append("a"); // avoid this
|
||||
|
||||
String foo = " ";
|
||||
StringBuffer buf = new StringBuffer();
|
||||
|
@ -51,7 +50,6 @@ public class Performance {
|
|||
|
||||
buf.append("Hello").append(" ").append("World");
|
||||
|
||||
StringBuffer sbuf = new StringBuffer("tmp = " + System.getProperty("java.io.tmpdir"));
|
||||
// poor
|
||||
StringBuffer sbuf = new StringBuffer("tmp = " + System.getProperty("java.io.tmpdir")); // poor
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue