Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PMD : DD anomaly for variable

Tags:

java

pmd

I am using Eclipse with the PMD Plug-in (4.0.0.v20130510-1000) and got a lots of those violations and solved most of them, but i don't understand why DD anomaly occurs into below code

private boolean createFile(final String szFileName){
    final File oFile = FileUtils.getFile(szFileName);
    boolean bStatus;
    try {
        FileUtils.touch(oFile);
        bStatus = true; // DD anomaly
    } catch (IOException e) {           
        log.error("Failed to create new file. "+ e);
        bStatus = false;
    }
    return bStatus;
}

I can't use multiple return statements, because multiple exit point rule gets break.

Tried another way, but getting DD anomaly to another line,

private boolean createFile(final String szFileName){
        final File oFile = FileUtils.getFile(szFileName);
        boolean bStatus = true; // DD anomaly
        try {
            FileUtils.touch(oFile);
        } catch (IOException e) {           
            log.error("Failed to create new file. "+ e);
            bStatus = false;
        }
        return bStatus;
    }
like image 922
Kaustubh Khare Avatar asked Mar 17 '26 08:03

Kaustubh Khare


1 Answers

An alternative approach is to have createFile throw an IOException and handle it upstream. Then you could have createFile return void, or better yet have it return the file it created. That will get rid of the DD anomaly you are receiving.

Alternative createFile:

private File createFile(final String szFileName) throws IOException {
    final File oFile = FileUtils.getFile(szFileName);        
    FileUtils.touch(oFile);

    return oFile;
}
like image 81
Jonny Henly Avatar answered Mar 19 '26 22:03

Jonny Henly