Wednesday, June 3, 2015

About the code quality again

Last time I wrote about the quality of code (here: The Code Quality Guide). Because, during last few days I  got a few messages with questions about it (btw. it's nice to know that someone is reading my blog - thank you for that), I’ve decided to extend a bit my last post. Today I would like to show you my point of view in a few code quality cases. 

1. The most problematic Code Quality Tools rules

Increasing of the code quality is one of the most important issues things during every refactoring. Important are not only the architecture changes, but also each line of the code. Many employers force their programmers to use a predefined set of rules in specific projects. Such behavior provides sometimes the situations, where programmer tries to suit a really bad code according to the existing rules. The most popular example is the method and the class length. First "fast" solution is often cutting the comments and javadocs. I'm strongly against such behavior and also against that rule. I personally don't believe that is possible to define something like "the maximum length of the class". I rather enjoy the opinion that the code should be as short as possible (to not loose the readability). Just like that. 

In most cases a rule about code length is broken by UI parts, where (mostly generated) the code takes a lot of space. In fact, in good designed application on that layer there is nothing more than the user interface - no business logic, no additional stuff. A situation, where UI code is covered by PMD or CheckStyle length rule is quite unfair also if we compare it to the JEE platform, where *.jsf/*.xhtml etc. files are (mostly) not included to check by this tools.  

Additionally user interface is not something, what a standard programmer should take care of. Creation a good, readable, intuitive design for application is a kind of art and also a really difficult task. Because of that it should be created and planned on a different level.

I personally like the way, where after gathering business requirements and choosing the technology, the user interface fragment is prepared. Especially when technology is new for the team. That way has a few advantages:
- after first iteration there is something to show to the client,
- fast discover what are the limits of chosen technology,

- knowing the framework limitations could be really useful to plan deeper application layers.

2.  Return statements

If we are talking about code quality tool rules there is no possibility to talk about return statements. Probably there are no programmers working with PMD who didn't see: "A method should have only one exit point, and should be the last statement in method".  In my opinion here are two different schools:

    private Boolean validPorts(final PortBean bean) {
        final List<String> ports = bean.getRport();
        for(final String port : ports) {
            if(!port.matches(REGEX) || !portValidator.isValidPortRange(port)) {
               return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }

and

    private Boolean validPorts(final PortBean bean) {
        Boolean flag = Boolean.TRUE;
        final List<String> ports = bean.getPorts();
        for(final String port : ports) {
            if(!port.matches(REGEX) || !portValidator.isValidPortRange(port)) {
                flag = Boolean.FALSE;
                break;
            }
        }
        return flag;
    }

Both of them have their advantages and disadvantages. Multiple return points provide often more readable code but also there are more problems during extension or refactoring. Additionally, they can be harder to debug, because of shorter than expected end of the function.

The reason why I prefer the version with one return point over the multiple return points is quite different. Disadvantage of the single return point method is more complex code to read (in most cases). This is also a great advantage. Multiple end points of the method can simplify and hide the complexity of your solution. There wouldn't be a red light: "You are going too far my friend. It's time to refactor". Instead of that you will get many short parts of the code ended with a return statement. Everything can seem simple, but it can be only an illusion. To fully understand my point you can try to refactor one of the methods with multiple exit points you created some time ago (to the method with only one exit point) and check if you didn't create a hidden monster. ;-)

3. Inline comments enemies

As I said in my previous post, I'm not a inline comments fan. There are two main reasons against inline comments - they can be lost in a number of generated by your IDE inline comments (for example generated by Eclipse RCP //$NON-NLS-1$ for internationalization process) and I’d rather prefer to get support from an unknown framework than looking for tips in its source code:

Javadoc can make life easier.
4. Grouping methods

Popular opinion provided by most of the programming gurus is that the code should be written in a way, which makes it possible to read it as a good book - or something like that. That was quite a nice idea for structural and early object-oriented programming, but currently in Java, with growing number of frameworks using annotation and reflection mechanisms and with more support for functional programming, it seems to lose its importance. In fact methods inside the class should be grouped somehow. Standard situation where function is going just below method which call it for the first time can be good enough, however it might be worth considering to choose another method order (for example by their function: listeners together, user interface building methods together etc.).

5. White spaces

There are a lot of opinions in the internet that if a single method contains white spaces, then it should be refactored. And in most cases they're right, however there are a few situations where white spaces aren't wrong. I'm talking here about the generated code. The simplest way to make the generated code more readable is to separate it with white spaces. Let's see an example:

 @Override
    public void createControl(final Composite parent) {
        final Composite container = new Composite(parent, SWT.NULL);
        this.setControl(container);
        container.setLayout(new GridLayout(1, false));

        final Label mainLbl = new Label(container, SWT.NONE);
        mainLbl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
        mainLbl.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
        mainLbl.setText(Messages.getString("ActiveWizardPage.mainLbl.text"));
        new Label(container, SWT.NONE);

        enabledBtn = new Button(container, SWT.RADIO);
        enabledBtn.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
        enabledBtn.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
        enabledBtn.setSelection(true);
        enabledBtn.setText(Messages.getString("ActiveWizardPage.enabledBtn.text"));

        final Label enableLbl = new Label(container, SWT.NONE);
        final GridData gdEnableLbl = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
        gdEnableLbl.horizontalIndent = 15;
        enableLbl.setLayoutData(gdEnableLbl);
        enableLbl.setText(Messages.getString("ActiveWizardPage.enableLbl.text"));
        new Label(container, SWT.NONE);

        disabledBtn = new Button(container, SWT.RADIO);
        disabledBtn.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
        disabledBtn.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
        disabledBtn.setText(Messages.getString("ActiveWizardPage.disabledBtn.text"));

        final Label disableLbl = new Label(container, SWT.NONE);
        final GridData gdDisableLbl = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
        gdDisableLbl.horizontalIndent = 15;
        disableLbl.setLayoutData(gdDisableLbl);
        disableLbl.setText(Messages.getString("ActiveWizardPage.disableLbl.text"));

    }

As you can see, each generated UI component is separated from the others. A complexity of this page is too low to split createControl(...) method. In fact with currently existed IDE white spaces in code are not a problem anymore (in older code editors navigate with Home/End/Page up or Page down have problems with trailing white spaces - that was the main reason to avoid such practices).

Conclusion

There are many code quality rules. Today we saw a small part of things, that should stay in mind before code creation or refactoring. In fact there are no gold rules, which will always work. Shown by me practices are just a tips, which working best for me. Everyone of you can have a different opinion and different experiences. The most important thing is to keep your code consistent, provide some rules and use them in every part of your project. That will not guarantee a good code, but it's one step closer. I hope you'll enjoy this post and, as always, I'm waiting for the comments with your code quality experiences. 

0 komentarze:

Post a Comment