10 practical but paranoid Java programming technologies, java programming technologies
1. Place the String at the beginning
To prevent occasionalNullPointerExceptionException. We usually place the String on the left side of the equals () function to perform String comparison. The following code:
<Pre http: = "" www.ahlinux.com = "" start = "" cmd = "" 9034.html "=" "target =" _ blank "class =" keylink "> String [] files = file. list (); // Watch outif (files! = Null) {for (int I = 0; I <files. length; I ++ ){...}}
Seems paranoid? Maybe, but see Javadoc:
If this virtual path does not represent a folder directory, this method returns null. Otherwise, a string array is returned. Each string represents a file or folder in the directory.
Yes, that's right. We can add some validation:
if (file.isDirectory()) { String[] files = file.list(); // Watch out if (files != null) { for (int i = 0; i < files.length; i++) { ... } }}3. Do not trust "-1"
I know this is paranoid, but in Javadoc for String. the indexOf () method explicitly states that the position index of the specified character appears for the first time in the object. If it is-1, it indicates that the character is not in the character sequence.
So using-1 is taken for granted, right? See the following code:
// Badif (string.indexOf(character) != -1) { ... }// Goodif (string.indexOf(character) >= 0) { ... }
Who knows. Maybe they changed the encoding method and the string is not case sensitive. Maybe the better way is to return-2? Who knows.
4. Avoid accidental assignment
Yes. This kind of thing may happen frequently.
// Ooopsif (variable = 5) { ... }// Better (because causes an error)if (5 = variable) { ... }// Intent (remember. Paranoid JavaScript: ===)if (5 === variable) { ... }
So you can place the Compare constant on the left side, so that no unexpected assignment error will occur.
5. Check Null and Length
In any case, make sure that you have a set, array, and so on, and it is not empty.
// Badif (array.length > 0) { ... }// Goodif (array != null && array.length > 0) { ... }
You don't know where these arrays come from, maybe from JDK APIs of earlier versions. Who knows.
6. All methods are final
You may tell me your open/closed principles, but this is all nonsense. I don't trust you (inheriting all the sub-classes of my parent class correctly), and I don't trust myself (accidentally inheriting all sub-classes of my parent class ). Therefore, for those methods with clear meanings, use final identification strictly.
// Badpublic void boom() { ... }// Good. Don't touch.public final void dontTouch() { ... }7. All variables and parameters are final
As I said. I don't trust myself (don't overwrite my value accidentally ). Even so, I don't trust myself because...
... This is why all variables and parameters are final.
// Badvoid input(String importantMessage) { String answer = "..."; answer = importantMessage = "LOL accident";}// Goodfinal void input(final String importantMessage) { final String answer = "...";}8. Do not trust the generic type during overload.
Yes, it can happen. You believe that the super nice-looking API you write is very intuitive. Then, some users simply convert the original type to the Object type until the damn compiler stops complaining, and suddenly they will link the wrong method and think this is your error.
See the following code:
// Bad<T> void bad(T value) { bad(Collections.singletonList(value));}<T> void bad(List<T> values) { ...}// Goodfinal <T> void good(final T value) { if (value instanceof List) good((List<?>) value); else good(Collections.singletonList(value));}final <T> void good(final List<T> values) { ...}
Because, you know ...... Your users are like
// This library sucks@SuppressWarnings("all")Object t = (Object) (List) Arrays.asList("abc");bad(t);
Believe me. I have seen all this. Including the following
This type of paranoia is good.
9. An exception is always thrown in the Default of the Switch statement.
Switch statement ...... I don't know whether to fear or cry about one of them, but in any case, since we insist on using the switch, we may wish to use it perfectly. Let's look at the following code:
// Badswitch (value) { case 1: foo(); break; case 2: bar(); break;}// Goodswitch (value) { case 1: foo(); break; case 2: bar(); break; default: throw new ThreadDeath("That'll teach them");}
When value = 3, a prompt cannot be found, so it is not unknown.
10. The Switch statement is enclosed in curly brackets.
In fact, switch is the most evil statement, such as some people who are drunk or have a bet are writing code. Let's look at the example below:
// Bad, doesn't compileswitch (value) { case 1: int j = 1; break; case 2: int j = 2; break;}// Goodswitch (value) { case 1: { final int j = 1; break; } case 2: { final int j = 2; break; } // Remember: default: throw new ThreadDeath("That'll teach them");}
In a switch statement, each case statement has only one row of statements. In fact, these case statements are not even true statements, just like the jump mark in a goto statement.