Is there any technique for applying code maintenance?

1

In addition to applying high cohesion and low coupling, how to deal with codes like this? They may be readable but this excess of logical operators, I do not see it possible to refactor or even with switch-case .

                        if (!entry.isDirectory()
                        && !entry.getName().startsWith("__MACOSX/")
                        && !entry.getName().startsWith("_images/")
                        && !entry.getName().contains("/_images")
                        && !entry.getName().contains("/._")
                        && !entry.getName().contains("/_img")
                        && !entry.getName().contains("/imgs")
                        && !entry.getName().contains("/_old")) {
                    String filename = Util.buildFilename(nome);
                    filenameMap.put(filename, nome);

                    if (!nome.startsWith("[.]")
                            && (nome.toLowerCase().endsWith(".swf")
                            || nome.toLowerCase().endsWith(".gif")
                            || nome.toLowerCase().endsWith(".jpg") || nome
                            .toLowerCase().endsWith(".jpeg"))) {
                        files.add(dest + nome);
                    }

                    File destFile = FileUtils.getFile(dest, filename);

                    copyInputStream(zipFile.getInputStream(entry),
                            new BufferedOutputStream(new FileOutputStream(destFile)));
                } 

SonarQube

  

Expressions should not be too complex

     

The complexity of an expression is defined by the number of & & and   condition ifTrue: ifFalse operators it contains. The single   expression should not be too high to keep the code   readable.

    
asked by anonymous 06.04.2015 / 21:00

3 answers

2

In this case, little can be done. These code analyzers do not distinguish intent. What might be best is to create the condition in a method and in if just call the method that will return a boolean. I do not know if it will totally satisfy the parser or it will just change the complaint of place, but leave the code more semantic.

In fact, switch would not make sense there.

The other answer gives you a solution but you have a joke that says that when you try to solve a problem with RegEx you have two problems. This has its true background there, although that may not be the case for what you are doing specifically.

I would particularly create a generic function that checks by passing a list of texts. But you can try with RegEx to see if you like the solution.

Note that it may be that logic does not have the desired result in all situations. As I do not know the exact situation but it seems to me that there is a chance of producing false positives in some cases where path might be in a different order.

    
06.04.2015 / 21:24
1

You're basically doing Strings comparisons to set the code flow. Regular expressions can make your code cleaner.

Lesson: Regular Expressions

Java Regex - Tutorial

However, regular expressions can become confusing to read and difficult to crawl during debugging. I recommend isolating the method that does the test and implementing a good unit test to validate it.

A little code to inspire you:

public static void main(String[] args) { 
    String regularExpression =
            "(" + 
            "(__MACOSX/.*)" +
            "|(_images/.*)" +
            "|(.*/_images.*)" +
            "|(.*/\._.*)" +
            "|(.*/_img*)" +
            "|(.*/imgs*)" +
            "|(.*/_old*)" +
            ")";

    System.out.println("__MACOSX/".matches(regularExpression));
    System.out.println("_images/".matches(regularExpression));
    System.out.println("/_images".matches(regularExpression));
    System.out.println("/._".matches(regularExpression));
    System.out.println("/_img".matches(regularExpression));
    System.out.println("/imgs".matches(regularExpression));
    System.out.println("/_old".matches(regularExpression));
}
    
06.04.2015 / 21:13
1

There is no standard technique unfortunately, but there are several techniques, I do not know if this is the best, but it is something we can apply (based on SOen ) would create a variable and test one condition at a time, for example:

boolean returnValue;

returnValue = !entry.isDirectory();
returnValue &= !entry.getName().startsWith("__MACOSX/");
returnValue &= !entry.getName().startsWith("_images/");
returnValue &= !entry.getName().contains("/_images");
returnValue &= !entry.getName().contains("/._");
returnValue &= !entry.getName().contains("/_img");
returnValue &= !entry.getName().contains("/imgs");
returnValue &= !entry.getName().contains("/_old");

if (returnValue) {
    String filename = Util.buildFilename(nome);
...

These operators are called bitwise / bit-by-bit:

  • & ( AND ) - Example: a & b - returns true if a and b are both true . Otherwise it returns false. Both expressions a and b are always evaluated.

  • | ( OR ) - Example: a | b - returns true if a or b is true . Otherwise it returns false . Both a and b expressions are always evaluated.

  • - ^ - returns OR if a ^ b is true and a is false or vice versa. Otherwise it returns true

You can also do a line-by-line scan by reusing b :

boolean returnValue;

returnValue = !entry.isDirectory();
returnValue = returnValue &&!entry.getName().startsWith("__MACOSX/");
returnValue = returnValue &&!entry.getName().startsWith("_images/");
returnValue = returnValue &&!entry.getName().contains("/_images");
returnValue = returnValue &&!entry.getName().contains("/._");
returnValue = returnValue &&!entry.getName().contains("/_img");
returnValue = returnValue &&!entry.getName().contains("/imgs");
returnValue = returnValue &&!entry.getName().contains("/_old");

if (returnValue) {
    String filename = Util.buildFilename(nome);
...

In my view, this second example is easier to do.

    
06.04.2015 / 21:39