GithubHelp home page GithubHelp logo

Comments (10)

ThrawnCA avatar ThrawnCA commented on June 2, 2024

That's not how the Builder pattern is supposed to work. User isn't supposed to know about Builder. User should have a constructor that accepts the individual parameters (userId, firstName, lastName), instead of accepting a Builder. (The parameter validation should happen inside User, too.)

The way you have things set up, it's impossible to construct a User directly; you have to create a Builder first. That shouldn't be the case; Builder should just be a convenient shortcut.

So the bug detector is correct.

from fb-contrib.

mebigfatguy avatar mebigfatguy commented on June 2, 2024

Or builder should have a build () method that constructs a user and sets fields on it

from fb-contrib.

jsotuyod avatar jsotuyod commented on June 2, 2024

Actually, that implementation is perfect. Read Effective Java, 2nd edition, item 2

http://www.google.co.th/url?sa=t&rct=j&q=effective+java&source=web&cd=8&cad=rja&uact=8&ved=0CFQQFjAH&url=http%3A%2F%2Fuet.vnu.edu.vn%2F~chauttm%2Fe-books%2Fjava%2FEffective.Java.2nd.Edition.May.2008.3000th.Release.pdf&ei=V2xmVcWoMsK7uATbmYLwDw&usg=AFQjCNHaTVJ7cBfChZStN4QTEuewWLo98Q

from fb-contrib.

jsotuyod avatar jsotuyod commented on June 2, 2024

Probably, the best would be to ignore CD when it concerns inner classes (either static or not). After all, being an inner class, they are already tightly coupled and the CD report adds little value.

from fb-contrib.

christopherpicard avatar christopherpicard commented on June 2, 2024

I agree with Jsotuyod that this check should not apply to inner classes. I am sure there are other cases other than the builder pattern that this check falsely reports.

As far as the other comments about the builder pattern, the whole point of the pattern is to ensure that you always have a fully populated object. It is used a lot when creating immutable objects like this one rather than requiring large number of constructor arguments which is bad practice in both Clean Code and Effective Java.

from fb-contrib.

mebigfatguy avatar mebigfatguy commented on June 2, 2024

anon classes are already not reported. I will extend to all inner classes - that's fine.

Point of note tho, imo, User shouldn't know about the builder class.

public User build() {
     if (userId == null) {
        userId = UUID.randomUUID();
     }
     if (StringUtils.isEmpty(firstName)) {
        throw new IllegalStateException("First name is not set");
     }
     User u new User();
     u.userId = userId;
     u.firstName = firstName;
     u.lastName = lastName;
     return u;
  }

from fb-contrib.

ThrawnCA avatar ThrawnCA commented on June 2, 2024

Well, it's correct that the Builder pattern as implemented matches Effective Java. That's not the same thing as being "perfect". I would still put it to you that it would be better for the constructor to accept individual arguments, and have the Builder provide them all, so that the builder remains an optional part of the process.

But I also agree that it makes sense to drop the CD check for all inner classes.

from fb-contrib.

jsotuyod avatar jsotuyod commented on June 2, 2024

What is the status of this? Need any help?

from fb-contrib.

mebigfatguy avatar mebigfatguy commented on June 2, 2024

sorry, kind of forgot about this. I thought i had fixed this, and running this class, seems like it is not reported now, so... still broken for you?

from fb-contrib.

jsotuyod avatar jsotuyod commented on June 2, 2024

Truth be told, I haven't checked. I'm preparing for next week's "week of linting" at the office, trying to come up with a nice colection of things to do on tools such as FindBugs, fb-contrib, PMD, Checkstyle, and our findbugs-plugin and came accross this issue still being open and thought it might be a good fit.

I just checked it and it seems to be gone, so this one could be closed.

If you have anything you think may be interesting for us to help you on, feel free to contact me directly. Thanks!

from fb-contrib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.