423

Next time I see a constructor with 22 parameters. I'm gonna report whoever wrote that to the police

Comments
  • 9
    That's y use python !
    kargs for the rescue!
  • 20
    Police: ??
  • 0
    Haha! Well said.
  • 30
    tbh I wanna see that constructor, just because I'm curious about what kind of abomination it is
  • 5
    I would love seeing it as well^^
  • 2
    i wanna post it but I'm not sure if the company would allow that, probably just the constructor is fine. but its just a "model" in js, not sure why json won't suffice for that tho
  • 5
    @rEaL-jAsE let's burry those old code and grab a drink my friend
  • 4
    Sounds like some feels too entitled to work at factories?
  • 7
    If you use Dependency injection, that could actually happen. But then you don't fill in the fields by yourself...
  • 0
    What about a model that take every fields of a db collumn?
  • 4
    And I thought 5 was bad...
  • 7
    My team lead strongly argues that's the only right way, even if you end up having 20+. Regardless of if the args are being injected or provided "manually".

    Glad I'm not the only one finding that extreme.
  • 1
    LOL

    and you just gave me an awesome idea... *registers domain *
  • 3
    All the more reasons to use a builder or factory pattern lol.
  • 7
    @rant1ng programmingpolice.com
  • 6
    @growling

    I fully expected that to be taken... And it wasn't. Now I own it
    :D

    *wonders if devrant has an api*
  • 3
    This is why I like c#’s properties
    They make constructors great
  • 5
    Sounds like you could have quite a number of arguments with them. 🙃
  • 1
    @rant1ng damn... i've never thought of that domain name, anyway found an unofficial api here https://devrant-docs.github.io/
  • 5
    Reading Clean Code at the moment and it's funny how in the eyes of the author two parameter function are the most you would ever need and even those are necessary evil.

    By the way our trusty old GetHoldings function at work, written in 2002 in VB, has over 50 parameters, where 48 are optional. Beat that.
  • 4
    @BorderKeeper increment for reading clean code. This should be a mandatory read for every professional programmer!
  • 5
    22? Try more like 255.
    That's the upper limit of parameters for a constructor in Java.

    How do I know this?
    SOAP, .wsdl, a legacy API I must work with, and someone who doesn't knows what the fuck was he doing.
  • 0
    I saw that type of constructor once from a API demo, it takes around 15 parameters. The class is just a model with all fields exposed as read only. I first guess of this design is that all the fields are "mandatory". It feels wrong, but cant think of a better way to do it though
  • 1
    @DannySu I haven't seen the API you're talking about but it sounds like it could possibly be broken up into smaller pieces (more endpoints, more models). Hard to say without being familiar with it but that's usually a good answer. Maybe some can be optional, too.
  • 0
    A very good use of the number of params you are able to pass
  • 0
    I wrote a PL in which I set limit of 32 params to any function.
  • 1
    @MarioC but java does supports variadic functions right ? meaning we could pass unlimited arguements of the same datatype using ' . . . ', then how is there a 255 args limit ?
  • 2
    @TitanLannister https://docs.oracle.com/javase/...

    Maybe this sheds some light on it :o
    But yea, variadic is just a list or array of the values so they should count as one for the matter of the topic in the link..
  • 1
    @TitanLannister
    Because you can only pass one type of class as varargs with '...' and it must be the last parameter. I could do that if I had for example, 2 Strings and 400 booleans.

    The trouble was that, as it is automatically generated and given to me by a third party, I don't have control over the constructor. So, at the end it looked something like this:

    ClassX(boolean, String, CustomClass1, int, int, long, String, CustomClass2, float, boolean)

    That with 255.

    What we did, is override it with an empty constructor and set all with Lombok's setters and getters.
  • 0
    That's why you use Haskell. Because it's better
  • 4
    Having to pass that many dependencies in your constructor usually is an indication that the class is doing way too many things. There are quite some techniques to fix this, e.g. refactoring out related things into their own components or introduce parameter objects.
  • 0
    @andros705 done wrong
  • 0
    @andros705 20+ parameters constructors are a huge code smell! Its a hint, that the single responsibilty of a class is broken. Most of the times you are able to refactor functionality of these classes out to a new, more concret class.
  • 0
    @andros705 isnt like I didnt done that. But to be honest, I dont know what that has to do with doin proper programming?
  • 0
    @andros705 its ok to code fast, but most of the time, refactoring this code smell is not very time consuming. And it also helps with maintaining and extending the code base. I know what you want to say, but it always made my life easier to remove code smells (if it was not too time intense).
  • 0
    @kuptenJackBard @growling

    https://devrant.com/collabs/...

    Check out the collab, let me know if interested :)

    sorry to hijack this thread, but it's relevant.
  • 0
    @rant1ng if it's relevant, you're not hijacking
  • 1
    Is constructor for what ? Nasa main algorithm ?
  • 0
    Autovalue to the rescue
  • 0
    @rant1ng lol this is amazing, sorry I just respawned today mate
Add Comment