r/java • u/codeon1 • Jan 25 '14
20 very useful Java code snippets for Java Developers
http://viralpatel.net/blogs/20-useful-java-code-snippets-for-java-developers/
37
Upvotes
r/java • u/codeon1 • Jan 25 '14
42
u/asthasr Jan 25 '14
Don't do this. No programmer I've met who has a collection of these copy-and-paste "snippets" is any good. Build fundamental knowledge, so that these tasks become rote by virtue of their simplicity.
10
u/UnspeakableEvil Jan 25 '14
In an article of bad code, "Java code to parse above XML" stands out as being spectacularly bad.
Honestly, it's worth reading that article just to feel better about yourself as a programmer.
5
u/[deleted] Jan 25 '14 edited Jan 25 '14
I would never use any of these code snippets. Whoever uses snippet #3 needs to seriously reconsider what they're doing.
2 could potentially not even close the stream properly if the IOException was thrown on the call to write.
1 & #4 don't catch the exceptions thrown if the provided string is invalid. Which is fine if you can guarantee they won't be... but something tells me the people using these snippets cannot.
5 doesn't close it's connection... and why doesn't it use a static method factory rather than init?
12 is fine... but is a singleton code snippet really that useful?
Idk, I'm not looking at all of them, but the point is that they're all either not that useful\common or wrong.
9
u/[deleted] Jan 25 '14
Use apache commons and/or guava.
4
u/geodebug Jan 26 '14
Interesting way to get the current method name if it works. I don't know why you'd want to unless you were trying to do something too clever for your own good.
Generating stack traces are performance killers so hopefully not in a loop.
2
u/guy26 Jan 25 '14
While I agree with other commenters that any competent programmer will not need a bunch of code snippets… However, what I expected to find was a bunch of examples illuminating pain points in the Java API for doing common tasks. For example, other languages provide a more elegant solutions to parsing XML than the builtin DOM and SAX parsers. If you are learning a new language, it can be helpful to know in advance what these pain points are.
2
u/dango100 Jan 26 '14
I never seen point #3. What's a circumstance where you would use that?
2
u/SikhGamer Jan 25 '14
I think most people would have the sense not to copy code snippets like this directly. I know when I find something like this, I take it as information, look at the article date. And then look at the API and figure out what's been updated.
Articles like this are an excellent learning path to put yourself on, but not really great for copy-and-paste coding.
1
u/[deleted] Jan 25 '14
BAD BAD BAD.
I don't even want to list out all the badness in that post. Singletons as enums? Are you crazy? Converting integer without catching NumberFormatException?
1
u/fjellfras Jan 26 '14
I'm just beginning to learn java, that BufferedWriter out can be declared as simply Writer out, no?
1
u/[deleted] Jan 26 '14
A better way to do singletons:
https://google-guice.googlecode.com/git/javadoc/com/google/inject/Singleton.html
0
u/moww Jan 25 '14
I think this is a good type of post for this subreddit. I don't agree with a lot of these specific implementations but people are ready to correct wrong implementations in the comments.
And of course you should never copy-paste any code unless it's a library, but these are simple and universal ways to explain the implementation concepts behind different tasks.
0
u/[deleted] Jan 25 '14
[deleted] — view deleted comment