This is a summary of more than 20 years of experience of foreign gods...

"Anything that can go wrong will eventually go wrong."

This is why people like to do "error-protected programming." Paranoid habits are sometimes meaningful, sometimes they are not clear enough and they are not smart enough, and you may feel a bit weird when you think of people who write like this. Here are 10 Java programming tips that I personally feel are most useful and paranoid. Please see:

1. Put the string constants in front

It is never a bad idea to prevent accidental NullPointerExcepTIon by placing the string constants to the left of the comparison function equals(), like this:

Learn Java Programming These 10 tips are not to be missed

There is no doubt that converting an expression to another better expression will not lose anything. As long as our OpTIons are real (OpTional in Java 8 is an encapsulation of objects that can be empty), isn't it? Discuss...

2. Do not trust early JDK APIs

When Java first appeared, programming was a very painful thing. At that time, the API is still not mature enough. You may have encountered such a piece of code:

Learn Java Programming These 10 tips are not to be missed

Looks weird right? Maybe, but take a look at this Javadoc:

"If the abstract pathname does not represent a directory, then this method returns null. Otherwise, it returns an array of strings, where each string represents a file or directory in the current directory."

Yes, it is advisable to add a quiz check to ensure correctness:

Learn Java Programming These 10 tips are not to be missed

bad! The former violates rules #5 and #6 of 10 subtle best practices in Java coding. So be sure to remember to null check!

Do not believe "-1"

I know this is paranoid. The early description of String.indexOf() in Javadoc was like this:

"The position where the character first appeared in the character sequence will be returned as a result, or -1 if the character does not exist."

So, -1 can be taken for granted, right? I am not right. Look at this:

Learn Java Programming These 10 tips are not to be missed

Who knows? Maybe on a particular occasion they will need another encoding value. If it is case-insensitive, otherString will be included. At this point, maybe return -2? Who knows?

After all, we have a lot of discussion about NULL - the value of hundreds of millions of dollars. Why not start discussing -1, in a sense -1 is null in another form of the int type.

4. Avoid accidental assignment

Yes. Even the best programmers can make this mistake (of course, not including me. See #7).

(Assuming that this is JavaScript, we are temporarily paranoid to think of this language)

Learn Java Programming These 10 tips are not to be missed

Say it again. If you have a constant in your expression, place it on the left side of the equation. This way, when you intend to add another =, it is not easy to make mistakes.

5. Check null and length

Whenever you have a collection, array, or other, make sure it exists and is not empty.

Learn Java Programming These 10 tips are not to be missed

You don't know where these arrays come from. Maybe the early JDK APIs?

6. All methods use final declaration

You can tell me what you want to open and close, but that's all nonsense. I do not believe you (can inherit my class correctly), nor do I believe in myself (will not accidentally inherit my class). So except for the interface (specially used for inheritance) should be a strict final.

Learn Java Programming These 10 tips are not to be missed

Yes, it is written as final. If this does not make sense to you, you can also change the class and method, or send a feature request, by modifying or rewriting the bytecode. As a public editor of the Java technology stack, I'm pretty sure that telling you to rewrite classes/methods is not a good idea.

7. All variables and parameters are declared with final

Like I said. I do not believe that I will not inadvertently overwrite a value. That being said, I do not believe in myself at all. because:

Learn Java Programming These 10 tips are not to be missed

This is why all variables and parameters are declared with final.

Learn Java Programming These 10 tips are not to be missed

Well, I admit that this one is not used by myself, although I should use it. I hope that Java can be used like the Scala language. People use val directly to represent variables everywhere. They don't even consider mutability. They only use var to declare variables when they are explicitly needed, but such opportunities are rare.

Do not trust generics when overloaded

Yes, this will happen. You think you've written a superb API, it's really cool and intuitive; then there's a group of users who just put all types in the Object until the damn compiler stopped working, and then they suddenly linked To the wrong way, think that this is all your fault (things are always the case).

Think about this:

Learn Java Programming These 10 tips are not to be missed

Because, you know... your users, they are like this

Learn Java Programming These 10 tips are not to be missed

Believe me, I have seen more, and there is such

Learn Java Programming These 10 tips are not to be missed

So paranoidism is good.

9. Always add default in the switch statement

Switch... As one of the funniest expressions, I don’t know if it’s awesome or weeping silently. Anyway, since we can't get rid of the switch, we'd better use it properly when necessary, such as:

Learn Java Programming These 10 tips are not to be missed

Because when the value=3 is introduced into the software, default will work and make it work! Don't mention the enum type with me, because this is also true for enums.

10. Use braces to separate each case block of the switch

In fact, the switch is the most pithy statement, anyone who is drunk or loses can use it in a language. Take a look at this example:

Learn Java Programming These 10 tips are not to be missed

In the switch statement, only one scope is defined for all cases. In fact, these cases are not really statements, they are more like labels, and switch is the goto statement that points to these labels. In fact, you can even compare the case statement to the astonishing FORTRAN 77 statement. For FORTRAN, its mystery has gone beyond its function.

This means that the variable final int j can be accessed by any case, whether or not we have a break. It doesn't look very intuitive. We can create a new nested scope for each case by adding simple curly braces. Don't forget to add a break at the end of each case block.

in conclusion

Obsessive-compulsive disorder at the time of programming can sometimes seem strange, making code often longer than necessary. You may think, "Oh, this situation will never happen!" But as I said, after going through a programming career of 20 years or so, you wouldn't want to correct those just because of programming languages. The stupid and unnecessary bugs caused by ancient and inherent defects. Because you know... .

Male Pin Headers

1.ANTENK MALE PIN HEADER Series in Headers are a full range headers in a variety of configurations including Single, Dual and Three rows, Straight or Right Angle in Thru-Hole or SMT mounting. Their close tolerance .025" sq. posts are smoothly finished and taper tipped to eliminate insertion damage to the PCB or mating connector. ANTENK Pin Headers can be easily cut into exact sizes as required. Options include stacked insulator versions and choice of tin, gold or selective gold plating.


Antenk Male Pin Headers Types By Pitch

0.8mm Male Header
1.0mm Male Header
1.27mm Male Header
2.0mm Male Header
2.54mm Male Header

Antenk Male Pin Headers Application

Antenk's Male Pin Header are widely used in electronic equipments,such as monitors ,electronic instruments,computer motherboards,program-controlled switchboards,LED,digital cameras,MP4 players,a variety of removable storage disks,cordless telephones,walkie-talkies,mobile phones,digital home appliances and electronic toys,high-speed train,aviation,communication station,Military and so on.


2.54mm male pin headers 90°2.54mm male pin headers 180°2.54mm male pin headers SMT

Male Pin Headers:

SPECIFICATIONS:
Current Rating: 0.75Amp
Insulation Resistance: 1000M ohms min
Contact Resistance: 20M ohms max
Dielectric Withstanding: AC500V
Operating Temperature: -65°C to +125°C
Contact Material: Brass
Insulator Material: PA6T,UL94V-0,Color:Black



Male Pin Headers,Pin Male Straight Box Header,Smd Pin Headers,Long Pin Male Header,Pin Header,Male PCB Connectors

ShenZhen Antenk Electronics Co,Ltd , https://www.antenk.com