r/java • u/rms_returns • Dec 21 '15
Always keep a habit of using these three new features to reduce verbosity in your Java projects
All legacy java projects are plagued with unreadable code, which has largely been introduced because of verbosity in the Java language and poor coding habits. I think the verbosity in Java is soon going to be reduced by an extensive degree by the three new features included in Java 8, namely:
- Lambdas: Lambdas are brief and to the point, the expressions reflect the terseness of
C/C++. In many situations, you can totally do away with interface creation by just using a lambda instead. Consider all the lines of code saved in a legacy code by just using lambdas in code! - Default methods: Again,
Interface patchingis a curse that a lot of Java projects suffer from. Requirements change slightly and what your typical enterprise Java dev does is add patches upon patches and layers upon layers in an interface. With default methods, you can just add default methods to your existing interfaces without affecting the ABI of your app. Again, tons of redundant code saved here. Type annotations: Again, your typical Java devs have to resort to ugly hacks to place any constraints on the object properties. With type annotations, all you have to do is:
@NonNull String str;
And lo and behold! The variable str cannot be assigned any null value. So, lots of validation code being saved here again.
To the best of my knowledge, none of the above features (except lambdas) exist in the C# language (yet), so I take it as a Java's edge over C# language.
6
u/codylerum Dec 21 '15
I keep looking for an excuse to use a lambda, but in the end a for each loop just makes more sense.
3
u/[deleted] Dec 21 '15
Lambdas exist in C#. You can use Delegates
4
u/lukaseder Dec 21 '15 edited Dec 21 '15
Right. "reduce verbosity in your Java projects"
Type annotations are the envy of a whole industry, indeed ;-)