Categories: Java

3 Java 7 Features Aimed to Enhance Code Quality

There are several new features that have been released in Java 7 to take care of several developers’ requirements. However, interestingly, I have figured out that three of those 7 features are primarily aimed to enhance following key characteristics of code quality. Please feel free to share your opinion on them.

  1. Code Readability
  2. Code Duplication
  3. Code Complexity

Following is the detail of those 3 Java 7 features:

  • Underscore in Numeric Literals (Enhances Code Readability)
    This feature primarily takes care of code readability, and hence, code usability,  having large numbers having several digits. For example, credit card number, social security number etc. Many a times, we come across large number which seem to have an embedded pattern and when written as it is becomes difficult to read. This is when this feature comes handy. For example, credit card number. It is a 16 digit number which could be split into group of 4. Thus, the number assignment would look like following:long creditCardNumber = 4352_2314_8710_1210;
    long socialSecurityNumber = 8888
    You could read several such examples for long, float, byte variables in the Java doc page.
  • Catching Multiple Exception Types (Reduces Code Duplication)This feature primarily helps in reducing code duplication in relation with multiple catch blocks to handle more than one exception type. With this feature, a single catch can handle more than one type of exception. Take a look at following code samples:Prior to Java 7 release, one would be required to write in following manner to catch multiple exceptions. Note the code duplication aspect.

    catch block prior to Java 7

    Post Java 7 release, one needs to separate different exceptions with | such as following. Note the simplified code thereby reducing code duplication.

    catch block post Java 7

  • String in Switch (Reduces Code Complexity)
    This one is primarily aimed to reduce code complexity associated with usage of if-else for comparing string or mapping those String to final integer constant or char constant to use them inside switch. You may want to check StringSwitchDemo from JavaDocs to make yourself familiar with the code. However, it looks like same old Java switch statement with change that case statements could not have string literals which it could not have prior Java 7 release. Take a look at sample below:

    String in Switch

 

[adsenseyu2]

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com

View Comments

Recent Posts

Feature Engineering in Machine Learning: Python Examples

Last updated: 3rd May, 2024 Have you ever wondered why some machine learning models perform…

3 days ago

Feature Selection vs Feature Extraction: Machine Learning

Last updated: 2nd May, 2024 The success of machine learning models often depends on the…

3 days ago

Model Selection by Evaluating Bias & Variance: Example

When working on a machine learning project, one of the key challenges faced by data…

4 days ago

Bias-Variance Trade-off in Machine Learning: Examples

Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…

4 days ago

Mean Squared Error vs Cross Entropy Loss Function

Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…

4 days ago

Cross Entropy Loss Explained with Python Examples

Last updated: 1st May, 2024 In this post, you will learn the concepts related to…

4 days ago