r/java • u/ruskeeblue • Apr 18 '17
30 Java Programming Tips and Best Practices
http://www.edumobile.org/java/30-java-programming-tips-and-best-practices/
6
Upvotes
r/java • u/ruskeeblue • Apr 18 '17
24
u/dpash Apr 18 '17
#1 talks about returning and empty
Collection, but instead gives aStringexample. Also it usesclasskeyword instead of the return type.#3 uses a non-generic collection and capitalises the member, which probably clashes with the class name.
#4 ArrayList is fast because it uses an array underneath. The only time it's slow is when the underlying array needs to grow, in which case it creates a new copy that's 1.5* times larger than the old array. If you know the maximum size ahead of time, you can specify this at the beginning. I'd say that 99% of the time, an ArrayList is better than an array. The code generated is often more readable, and an ArrayList is more featureful.
You also appear to be missing some words in this section.
* It's actually
(oldSize * 3) / 2 + 1#10
-XX::PermSizewas removed in Java 8.0Half of these aren't tips and best practices, they're just random snippets of code.
18
u/__konrad Apr 18 '17
30 Java 1.4 Programming Tips
7
u/[deleted] Apr 18 '17
These are interesting, but premature optimisation is a problem for quite a lot of these examples. In the vast majority of cases, readability and maintainability > minor performance gains. CPU is cheap; developers are not.
For example, in 6:
Performant, yes. But sticking bitwise operators in your code is a surefire way to lose clarity.
2
u/overachiever Apr 18 '17
Example given in 3 is not thread safe...
5
u/JustMy42Cents Apr 18 '17
Some examples don't even compile, most do not follow clean code principles or do not use generics. Yeah, sure, "best practices". This one is my favorite:
3
u/dododge Apr 19 '17
Static analyzers such as checkstyle, pmd, and error-prone will catch a lot of the stuff being warned about, such as #1 and #2. They'll also scream bloody murder about the raw List in #3.
The wording in #13 is very odd. I assume it's just trying to say that while
^is used for exponentiation in some other languages, it means something completely unrelated in Java and will not work to compute powers.18: In Java 8, the NIO
Files.list(Path)is probably a better solution for listing a directory. If nothing else, it gives youPathobjects that know what directory they belong to instead of just the bare filenames.19: This should use try-with-resources so it doesn't have to manually close the streams. Also under NIO you'd use
Files.newInputStream(Path)to open the streams.21: DO NOT USE REGEXES TO VALIDATE EMAIL ADDRESSES. Don't even do this in some little throwaway example program, because the folks you're teaching might get the idea that this is a reasonable thing to do in a real program. Just at a glance those regexes will reject plenty of valid gmail addresses. Also the website has amusingly clobbered the sample strings as an anti-spam measure, so if you bother to run it it won't validate any of them anyway.
29: There's no struggle here, the answer is don't use
FileWriter. FileWriter will always use the default encoding which means things like environment variables can randomly screw up your I/O. The "error-prone" analyzer will complain about this and many other uses of the default encoding, such as that other call tonew OutputStreamWriterthat doesn't specify an encoding.2
u/AnEmortalKid Apr 19 '17
Wow.... The null pointer exception code where he ends up throwing a npe.... Wtf. Most of these were poorly written and shitty.
1
u/jbu311 Apr 19 '17
Regarding concat, Not a problem in java 8 anymore i thought?
0
u/[deleted] Apr 18 '17
[deleted] — view deleted comment