Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
tbh I wanna see that constructor, just because I'm curious about what kind of abomination it is
-
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
-
TobyAsE28116yIf you use Dependency injection, that could actually happen. But then you don't fill in the fields by yourself...
-
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. -
rant1ng44626y@growling
I fully expected that to be taken... And it wasn't. Now I own it
:D
*wonders if devrant has an api* -
@rant1ng damn... i've never thought of that domain name, anyway found an unofficial api here https://devrant-docs.github.io/
-
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. -
Sefie16446y@BorderKeeper increment for reading clean code. This should be a mandatory read for every professional programmer!
-
MarioC1856y22? 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. -
DannySu10886yI 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
-
duckWit56166y@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.
-
@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 ?
-
@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.. -
MarioC1856y@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. -
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.
-
geaz2896y@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.
-
geaz2896y@andros705 isnt like I didnt done that. But to be honest, I dont know what that has to do with doin proper programming?
-
geaz2896y@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).
-
rant1ng44626y@kuptenJackBard @growling
https://devrant.com/collabs/...
Check out the collab, let me know if interested :)
sorry to hijack this thread, but it's relevant.
Next time I see a constructor with 22 parameters. I'm gonna report whoever wrote that to the police
rant