r/java • • Jun 29 '17

Basic neural network Java implementation, which can train itself, for beginners to try out and learn how machine learning works.

https://github.com/sebig3000/MachineLearning
264 Upvotes

28 comments sorted by

View all comments

29

u/ron_krugman Jun 30 '17

A few remarks on style:

  • Always put a block with curly braces after if, while, etc. even if there is just a single statement within and braces are technically optional.

  • Prefer enums over int constants. They are strongly typed in Java, which makes it much harder to accidentally pass an invalid value.

  • Throw a java.lang.IllegalArgumentException in case of an invalid input. It is more concise and saves you the trouble of checked exceptions.

  • Also, you probably shouldn't ever instantiate a java.lang.Exception directly. Throw a java.lang.RuntimeException if you're not sure about what type of exception to throw.

7

u/[deleted] Jun 30 '17

Can you provide some justification for that last bullet point? I've been writing Java for a while now and have never heard that.

8

u/locatedtaco Jun 30 '17

I could be wrong but I think the point is that throwing an Exception isn't expressive. It doesn't clue in the developer about what went wrong. Also, if you throw an Exception the client code can't selectively catch it and handle it specifically.

4

u/ron_krugman Jun 30 '17

That's exactly what I meant, thanks! Of course it's fine to instantiate and throw for example java.io.IOException etc. although personally I would very much prefer a java.io.UncheckedIOException.

I'm not going to lie: I really fucking hate checked exceptions. They're one of the worst parts of Java.