DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. NetBeans IDE 7.1's Unused Assignment and Dead Branch Hints

NetBeans IDE 7.1's Unused Assignment and Dead Branch Hints

Dustin Marx user avatar by
Dustin Marx
·
Jan. 25, 12 · Interview
Like (0)
Save
Tweet
Share
7.49K Views

Join the DZone community and get the full member experience.

Join For Free

One of the new code hints provided by NetBeans 7.1 is the Unused Assignment hint. A simple code sample that will cause this hint to be displayed in NetBeans 7.1 is shown next.

Demonstrating Unused Assignment
   /**
    * Demonstrate NetBeans 7.1 code hint for situation in which variable
    * assignment is made but never used.
    * 
    * @return Integer.
    */
   private static int demoUnusedAssignment()
   {
      int i = 2;
      // ... do some good stuff, but without changing i's assignment or
      //     accessing i's value ...
      i = 3;
      return i;
   }

In the code above, the local variable "i" is initialized to 2, but is never used and then is initialized again, making the first initialization unnecessary. The next image is a screen snapshot that shows NetBeans 7.1 displaying a warning code hint for the unused assignment.

As the above image indicates, NetBeans 7.1 warns of "The assigned value is never used."

The New And Noteworthy in NetBeans IDE 7.1 page mentions this hint among others and states:

Unused Assignment

A new pair of hints, Unused Assignment and Dead Branch, was introduced. The Unused Assignment finds values that are computed and assigned to a variable, but never used by reading the variable. The Dead Branch hint searches for branches of code that can never be executed.

Oh Dead Branch Hint, Where Art Thou?

The Unused Assignment Hint seems to work as suggested based on the example shown above. However, I have not been able to generate code that demonstrates the "Dead Branch" hint. I wonder if the Dead Branch hint is not yet supported and text related to it is not supposed to be under the "Unused Assignment" heading.

The following code contains a listing with several methods that I would expect might potentially lead to a warning about a dead code branch. None of these cause this code hint to appear in any form (warning or error) in my installation of NetBeans 7.1.

Methods With Compilable Dead Code Branches
package dustin.examples;

import static java.lang.System.out;

/**
 * Class demonstrating NetBeans 7.1's hint for dead code.
 * 
 * @author Dustin
 */
public class DeadCode
{
   /**
    * Nonsense method that includes an "else" clause that can never be executed.
    * 
    * @param someInt Any primitive int.
    */
   private static void neverExecutedElse(final int someInt)
   {
      if (someInt < 0)
      {
         out.println("The integer you provided is less than zero.");
      }
      else if (someInt > 0)
      {
         out.println("The integer you provided is greater than zero.");
      }
      else if (someInt == 0)
      {
         out.println("The integer is equal to zero.");
      }
      else
      {
         out.println("Unable to categorize provided integer.");
      }
   }

   /**
    * Nonsense method that has executable code following conditions that are
    * always true and always lead to premature termination of the method.
    */
   private static void codeAfterAlwaysTrueConditional()
   {
      if (true)
      {
         throw new RuntimeException("Ouch!");
      }
      final String nothingHere = "Nothing here.";
      out.println(nothingHere);

      if (true)
      {
         return;
      }
      final String nothingHereEither = "Nothing here either.";
      out.println(nothingHereEither);
   }

   /**
    * Nonsense method that prints text to standard output only if the provided
    * boolean can be both {@code true} and {@code false} at the same time.
    * 
    * @param boolValue Boolean value of no consequence.
    */
   private static void cannotHaveItBothWays(final boolean boolValue)
   {
      if (boolValue)
      {
         if (!boolValue)
         {
            out.println("Make up your mind (true or false)!");
         }
      }
      else
      {
         if (boolValue)
         {
            out.println("Make up your mind (false or true)!");
         }
      }
   }

   /**
    * Runs else-if that is same as previous if such that one conditonal
    * prevents the second conditional's implementation from ever being executed.
    * 
    * @param boolValue A boolean value of no consequence.
    */
   private static void pesterUntilItWorks(boolean boolValue)
   {
      boolValue = true;
      if (boolValue)
      {
         out.println("It matches once.");
      }
      else if (boolValue)
      {
         out.println("It matches twice.");
      }
      else if (!boolValue)
      {
         out.println("No match once.");
      }
      else if (!boolValue)
      {
         out.println("No match twice!");
      }
   }

   /**
    * Cover all conditions in first conditional and include a never-can-happen
    * else portion of conditional.
    */
   private static void completeConditionalCoverage()
   {
      boolean boolValue = true;
      if (boolValue || !boolValue)
      {
         out.println("I'm always going to happen.");
      }
      else
      {
         out.println("I'm never going to happen.");
      }

      if (boolValue && !boolValue)
      {
         out.println("I'll never happen either.");
      }
      else
      {
         out.println("I'll also always happen.");
      }
   }

   /**
    * Main executable function.
    * 
    * @param arguments Command-line arguments: none expected.
    */
   public static void main(final String[] arguments)
   {
      if (false)
      {
         neverExecutedElse(5);
         codeAfterAlwaysTrueConditional();
         cannotHaveItBothWays(true);
         pesterUntilItWorks(true);
         demoUnusedAssignment();
         completeConditionalCoverage();
      }
   }
}

Although none of the methods in the directly previous code listing lead to the Dead Branch code hint, NetBeans 7.1 does include configuration options for the Dead Branch hint. This is shown in the next screen snapshot (selecting Tools->Options followed by the "Editor" tab and then selecting "Hints").

The NetBeans 7.1 News and Noteworthy page shows examples of other new hints, but does not show an example of the Dead Branch hint. Also, the text talking about Dead Branch is mixed with the section on Unused Assignment and under a heading that only talks about Unused Assignment. As my previous code listing demonstrates, I attempted to come up with a code sample to demonstrate the Dead Branch hint, but have not been able to do so. The purpose of this hint ("search[ing] for branches of code that can never be executed") sounds like a nice complement to compiler errors such as "unreachable statement" and "exception already caught" and other NetBeans "green" warnings such as "variable such-and-such is not used."

I have blogged about NetBeans hints before and the addition of new hints in NetBeans 7.1 is welcome. If anyone knows of a code sample that will demonstrate the Dead Branch hint in NetBeans 7.1, please share!

 

From http://marxsoftware.blogspot.com/2012/01/netbeans-71s-unused-assignment-and-dead.html

Branch (computer science) NetBeans Integrated development environment

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java REST API Frameworks
  • 10 Things to Know When Using SHACL With GraphDB
  • 4 Best dApp Frameworks for First-Time Ethereum Developers
  • Best Practices for Setting up Monitoring Operations for Your AI Team

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: