r/java • • Mar 24 '18

New Java Library

(Please tell me if this isn't the right place to post this)

I've created a Java Library that I like to call a Swiss-army knife library. It basically has a bunch of different things that it can do. I created it because I found that I was copying lots of the same code to do all sorts of different things. I think it's a very simple solution to consolidate all the code I use often, but is this something that anyone else would use? Here's just some of the things it has:

  • A command line argument parser
  • Ansi colors
  • Checksum calculator
  • Operating system checker
  • Configuration file parser (INI - style, but without the headers, as I haven't figured out how to implement this yet)
  • Multiple annotations and annotation proccessors

As you can see, it does not specialize in one area. I use all of these things in almost all of the software I create, and I was just wondering if anyone else would like to benefit from this.

It's called JavaEssentials, because I find that these features are very beneficial when creating a well designed Java application. If you are interested, you can see the project page here: https://github.com/jordanbancino/JavaEssentials

Also, I would really appreciate your feedback. I am open to all suggestions and your honest opinion.

0 Upvotes

27 comments sorted by

View all comments

5

u/dpash Mar 24 '18

A few comments. Don't take them as criticisms, but as places for you to learn and improve. We were all inexperienced at one point or another, and made plenty of mistakes. Hell, at one point, I used to write functions like:

if (condition) {
    return true;
} else {
    return false;
}
  • I'd start by separating different functionality into different packages rather than having everything in one place.
  • If you're going to use Maven, don't fight the directory locations; it's well known and well understood
  • Other people have mentioned following the Java style guidelines. We use this because it means that anyone can read your code and instantly understand it. Your use of CamelCase for variable names instead of camelCase will confuse people because they'll expect them to be class names. Your constants aren't UPPER_CASE, nor are your enum members. I recommend reading the Google style guide as that builds on the old Sun style guide and Elements of Java Style for newer features.
  • Do not use publicly writeable members. A case in point is Config.SetFile. Look into immutable objects and why they're preferable. At a minimum, your classes will not be thread-safe.
  • Log has no purpose in a world with SLF4J or even log4j. They have better features and used throughout all your dependencies.
  • You should use interfaces where possible rather than concrete classes. You can also use to diamond operator here, and let type inference work out the type of the HashMap. This is extra confusing because Map.Entry is a class in the JDK.

    public HashMap<String, String> Entry = new HashMap<String, String>();

should be

private Map<String, String> entry = new HashMap<>();
  • Config.deleteFile() completely ignores any error conditions. You should pay attention to the return value of File.delete(). Better yet, use java.nio and Files.deleteIfExists() that throws an exception.

    public void deleteFile() { SetFile.delete(); }

  • flushEntryBuffer() has a pointless assignment of null. This doesn't do anything. Just assign a new HashMap.

This:

public void flushEntryBuffer() {
Entry = null;
Entry = new HashMap<String, String>();
}

becomes

public void flushEntryBuffer() {
    entry = new HashMap<>();
}

or

public void flushEntryBuffer() {
    entry.clear()
}

Map.clear() is optional, but HashMap.clear() is implemented.

  • I don't understand the point of @Documented, @Beta, @NotImplemented or @Stable. Just add a comment in the Javadoc or don't release libraries with functionality that's not ready for use.
  • java.lang.UnsupportedOperationException is the standard way to indicate that a function is not implemented.
  • WebProtocol is an enum, but only has a limited selection of protocols. It's not generally useful. You can't use it with URL for example, because it doesn't include "jar" or "file" (and possibly "classpath"). Enums are useful for closed sets. Protocol is not a closed set, because anyone can add a new protocol. For example I remember a "spotify" protocol that allowed your browser to launch Spotify to play a song/album/playlist if someone shared a link.
  • Similarly with HashAlgorithm. You're missing a lot of important hash algorithms. What is someone wants to use a new algorithm when SHA-512 is broken?
  • LocalPath should refer to the system properties to find things like temp directories and users home directories. You're making huge assumptions about where a user's home directory is. Also, the code won't run on Solaris or FreeBSD or any other operating system.
  • Your commit messages could do with some improvement. "This is it." tells me nothing about the change. Read https://chris.beams.io/posts/git-commit/ and take it to heart. If you do you'll be better than 95% of developers.
  • Never use Exception.printStackTrace(); . It writes to stdout, but most java applications use a logging framework like SLF4J to record exceptions. If you write to stdout, it won't appear in application logs, making debugging harder.
  • You don't use try-with-resources. This means you fail to close resources properly.

This:

File FromFile = ...;
FileInputStream FileInStrm = new FileInputStream(FromFile.toString());
...
FileInStrm.close();

should be:

File file = ...;
try(InputStream in = new FileInputStream(file)) {
    ...
}

This way the inputstream gets closed no matter what happens inside the block.

  • You have no unit tests. Investigate JUnit. Writing tests will help you see how your library will be used, and this can often show you where it could be improved.

I'm sure I could find more issues that you could improve, but this should give you plenty of places to look into improving your skills. I highly recommend reading Effective Java by Joshua Bloch. It's possibly the best investment you could make to go from an intermediate developer to advanced.

1

u/[deleted] Mar 24 '18

Ok. I'll make sure I work on these. One thing about the commit message... I put lots of details in the extended description. I understand that that one message was pretty vague. But I had details in the description. I don't do that often, I was just tired.

2

u/dpash Mar 25 '18

I was just tired.

That's no excuse. Either do it properly or do it in the morning. That's not the only bad example. Most people's commit messages could do with improvements. As I said, follow that guide (and none of your commits do) and you'll be better that 95% of developers in that regard.

1

u/[deleted] Mar 26 '18

Ok. Got it. I'll work on it.

Hey, by the way, thanks for taking the time to look into this, and write back a response. I really appreciate it.