views:

1506

answers:

5

DataflowAnomalyAnalysis: Found 'DD'-anomaly for variable 'variable' (lines 'n1'-'n2').

DataflowAnomalyAnalysis: Found 'DU'-anomaly for variable 'variable' (lines 'n1'-'n2').

DD and DU sound familiar...I want to say in things like testing and analysis relating to weakest pre and post conditions, but I don't remember the specifics.

NullAssignment: Assigning an Object to null is a code smell. Consider refactoring.

Wouldn't setting an object to null assist in garbage collection, if the object is a local object (not used outside of the method)? Or is that a myth?

MethodArgumentCouldBeFinal: Parameter 'param' is not assigned and could be declared final

LocalVariableCouldBeFinal: Local variable 'variable' could be declared final

Are there any advantages to using final parameters and variables?

LooseCoupling: Avoid using implementation types like 'LinkedList'; use the interface instead

If I know that I specifically need a LinkedList, why would I not use one to make my intentions explicitly clear to future developers? It's one thing to return the class that's highest up the class path that makes sense, but why would I not declare my variables to be of the strictest sense?

AvoidSynchronizedAtMethodLevel: Use block level rather than method level synchronization

What advantages does block-level synchronization have over method-level synchronization?

AvoidUsingShortType: Do not use the short type

My first languages were C and C++, but in the Java world, why should I not use the type that best describes my data?

+6  A: 
erickson
+1  A: 

DataflowAnomalyAnalysis: Found 'DD'-anomaly for variable 'variable' (lines 'n1'-'n2').

DataflowAnomalyAnalysis: Found 'DU'-anomaly for variable 'variable' (lines 'n1'-'n2').

No idea.

NullAssignment: Assigning an Object to null is a code smell. Consider refactoring.

Wouldn't setting an object to null assist in garbage collection, if the object is a local object (not used outside of the method)? Or is that a myth?

Objects in local methods are marked to be garbage collected once the method returns. Setting them to null won't do any difference.

Since it would make less experience developers what is that null assignment all about it may be considered a code smell.

MethodArgumentCouldBeFinal: Parameter 'param' is not assigned and could be declared final

LocalVariableCouldBeFinal: Local variable 'variable' could be declared final

Are there any advantages to using final parameters and variables?

It make clearer that the value won't change during the lifecycle of the object.

Also, if by any chance someone try to assign a value, the compiler will prevent this coding error at compile type.

consider this:

 public void businessRule( SomeImportantArgument important )  {
      if( important.xyz() ){
          doXyz();
      }
      // some fuzzy logic here
      important = new NotSoImportant();
      // add for/if's/while etc 

     if( important.abc() ){ // <-- bug
         burnTheHouse();
     }
  }

Suppose that you're assigned to solve some mysterious bug that from time to time burns the house.

You know what wast the parameter used, what you don't understand is WHY the burnTHeHouse method is invoked if the conditions are not met ( according to your findings )

It make take you a while to findout that at some point in the middle, somone change the reference, and that you are using other object.

Using final help to prevent this kind of things.

LooseCoupling: Avoid using implementation types like 'LinkedList'; use the interface instead

If I know that I specifically need a LinkedList, why would I not use one to make my intentions explicitly clear to future developers? It's one thing to return the class that's highest up the class path that makes sense, but why would I not declare my variables to be of the strictest sense?

There is no difference, in this case. I would think that since you are not using LinkedList specific functionality the suggestion is fair.

Today, LinkedList could make sense, but by using an interface you help your self ( or others ) to change it easily when it wont.

For small, personal projects this may not make sense at all, but since you're using an analyzer already, I guess you care about the code quality already.

Also, helps less experienced developer to create good habits. [ I'm not saying you're one but the analyzer does not know you ;) ]

AvoidSynchronizedAtMethodLevel: Use block level rather than method level synchronization

What advantages does block-level synchronization have over method-level synchronization?

The smaller the synchronized section the better. That's it.

Also, if you synchronize at the method level you'll block the whole object. When you synchronize at block level, you just synchronize that specific section, in some situations that's what you need.

AvoidUsingShortType: Do not use the short type

My first languages were C and C++, but in the Java world, why should I not use the type that best describes my data?

I've never heard of this, and I agree with you :) I've never use short though.

My guess is that by not using it, you'll been helping your self to upgrade to int seamlessly.

Code smells are more oriented to code quality than performance optimizations. So the advice are given for less experienced programmers and to avoid pitfalls, than to improve program speed.

This way, you could save a lot of time and frustrations when trying to change the code to fit a better design.

If it the advise doesn't make sense, just ignore them, remember, you are the developer at charge, and the tool is just that a tool. If something goes wrong, you can't blame the tool, right?

OscarRyz
A: 

AvoidUsingShortType: Do not use the short type

  • List item

    short is 16 bit, 2's compliment in java

  • a short mathmatical operaion with anything in the Integer family outside of another short will require a runtime sign extension conversion to the larger size. operating against a floating point requires sign extension and a non-trivial conversion to IEEE-754.
  • can't find proof, but with a 32 bit or 64 bit register, you're no longer saving on 'processor instructions' at the bytecode level. You're parking a compact car in a a semi-trailer's parking spot as far as the processor register is concerned.
  • If your are optimizing your project at the byte code level, wow. just wow. ;P
  • I agree on the design side of ignoring this pmd warning, just weigh accurately describing your object with a 'short' versus the incurred performance conversions.
  • in my opinion, the incurred performance hits are miniscule on most machines. ignore the error.
DefyGravity
A: 

Wouldn't setting an object to null assist in garbage collection, if the object is a local object (not used outside of the method)? Or is that a myth?

The only thing it does is make it possible for the object to be GCd before the method's end, which is rarely ever necessary.

Are there any advantages to using final parameters and variables?

It makes the code somewhat clearer since you don't have to worry about the value being changed somwhere when you analyze the code. More often then not you don't need or want to change a variable's value once it's set anyway.

If I know that I specifically need a LinkedList, why would I not use one to make my intentions explicitly clear to future developers?

Can you think of any reason why you would specifically need a LinkedList?

It's one thing to return the class that's highest up the class path that makes sense, but why would I not declare my variables to be of the strictest sense?

I don't care much about local variables or fields, but if you declare a method parameter of type LinkedList, I will hunt you down and hurt you, because it makes it impossible for me to use things like Arrays.asList() and Collections.emptyList().

What advantages does block-level synchronization have over method-level synchronization?

The biggest one is that it enables you to use a dedicated monitor object so that only those critical sections are mutually exclusive that need to be, rather than everything using the same monitor.

in the Java world, why should I not use the type that best describes my data?

Because types smaller than int are automtically promoted to int for all calculations and you have to cast down to assign anything to them. This leads to cluttered code and quite a lot of confustion (especially when autoboxing is involved).

Michael Borgwardt
A: 

What advantages does block-level synchronization have over method-level synchronization? Synchronize a method is like do a synchronize(getClass()) block, and blocks all the class.

Maybe you don't want that

damian