Avatar billede fredand Forsker
11. juni 2009 - 20:52 Der er 2 kommentarer og
1 løsning

How to avoid DD - anomaly ?

Hello!

I try to have time to use some code-inspecting program like PMD before I deliver the code.

Sometimes I get this warnings or "i":s from PMD saying:

Multiple markers at this line
- Found 'DD'-anomaly for variable 'retValue' (lines '28'-'36').
- Found 'DD'-anomaly for variable 'retValue' (lines '28'-'32').




27    public boolean accept(final File file) {
28        boolean retValue = false;
29
30        if (file.isDirectory() )
31        {
32            retValue = true;
33        }
34        else if( checkTheFile(file) )
34        {
36            retValue = true;
37        }

        return retValue;
    }

...but I'm not sure what they mean?

In the example above I have also tried like:

public boolean accept(final File file) {
    boolean retValue = false;

    if (file.isDirectory() ||(readConfigurationAssembly(file)) )
    {
        retValue = true;
    }

    return retValue;
}

But then I get:
Found 'DD'-anomaly for variable 'retValue' (lines '28'-'32').

I found this explanation but I do not sure I understand it, acctually I do not understand it at all:
DD - Anomaly: A recently defined variable is redefined. This is ominous but don't have to be a bug.
...at:
http://pmd.sourceforge.net/rules/controversial.html

Is there a better way to write this code?
Btw PMD doesn't like diffrent return points either.

Love to get some comments about how to make PMD happy.

Best regards
Fredrik
Avatar billede arne_v Ekspert
11. juni 2009 - 21:05 #1
What about:

public boolean accept(final File file) {
    boolean retValue;
    if (file.isDirectory() ||(readConfigurationAssembly(file)) ) {
        retValue = true;
    } else {
        retValue = false;
    }
    return retValue;
}
Avatar billede fredand Forsker
11. juni 2009 - 22:10 #2
Hello!

I'll be darned!

I have always tried to remember that a local variable should be initialized, "before use". But now I see it just not compile if I do not like:

    public boolean accept(final File file) {
        boolean retValue;
       
        if(retValue)
        {
            System.out.println("Hello!");
        }
       
        ...

So the correct way is to do it like you showed!

Thanks alot arne!
PLease give a svar so I can reward you!

Best regards
Fredrik
Avatar billede arne_v Ekspert
11. juni 2009 - 22:20 #3
I will not necessarily call the code in #1 better than any of the other flavors.

But it seems to be the flavor that PMD prefers.

We get away with not initializing the local variable in the code in #1, because the compiler can see that the if else construct will always get it initialized.

So you can either code that way.

Or you can kill that rule in PMD.

I would be tempted to do the last !!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester