r/badcode • • Apr 07 '21

java Apparently Java can only handle 255 parameters in a constructor...

Post image
4.0k Upvotes

249 comments sorted by

View all comments

Show parent comment

38

u/chronos_alfa Apr 07 '21

If the constructor had 255 parameters, that means the object probably had 255 properties... JESUS

53

u/Epacnoss Apr 07 '21

255 properties at very minimum

48

u/chu121su12 Apr 07 '21

No need that much properties. You can do toString and concat them all thus storing them as single string.

11

u/paradoxon Apr 07 '21

Or keep a map of name value pairs with matching string parsing

11

u/SapphicRain Apr 07 '21

I hate this. You're doing too much. You need more classes to break up the workflow. Or find a way to use Arrays

4

u/GregariousFrog Apr 07 '21

What if the class is 40 string variables and constructor and nothing else? I'm making an object for every row in a 40 column csv file for an assignment, if that's important. It feels kinda stupid, but I just can't tell. Should I just use arrays?

4

u/SapphicRain Apr 07 '21

I'd probably just feed a string array at that point. Depending on how the data is used. Or make a class that represents the data contained in a row so I can process data in a self explanatory way (less need for over documenting things)

2

u/ColdPorridge Apr 07 '21

Why use a class instead of something more functional like a DataFrame?

1

u/GregariousFrog Apr 07 '21

Didn't occur to me to be honest. DataFrames probably make more sense for my application, but what I'm really worried about is, is it bad practice to have 4000 objects at runtime if the only purpose of the objects is to hold <1kb string data each? It's not going to scale up, number of objects and data size is going to stay the same.

3

u/ColdPorridge Apr 07 '21

I would probably use a dataframe. Pandas if you’re using Python. It will be the simplest answer and is a marketable skill to have.

9

u/pentaduck Apr 07 '21

Not necessarily. A few parameters could've been used to initialize a single member.

1

u/Lost4468 Apr 07 '21

I mean there could be actual legitimate uses for having that many or more. Large mathematical data structures could hold things like that and be somewhat appropriate.

I could actually think of a few ways it might actually be an ok or even a decent idea to do this in python. But not Java.

4

u/deegeese Apr 07 '21

Large mathematical data structures use actual data structures like arrays or trees.

Having 255 properties is a pretty strong sign that the designer has failed to properly encapsulate unrelated classes.

1

u/Lost4468 Apr 08 '21

Large mathematical data structures use actual data structures like arrays or trees.

It depends on what exactly we're talking about. It's not uncommon to see this, and can actual be useful. E.g. you need a ton of parameters for the maths, but they're not related, it doesn't really make much sense to put them in an array there. It's quite common in python, because you can just create a dictionary of the parameters, and then pass them as arguments.

2

u/deegeese Apr 08 '21

Can you name a mathematical function/relationship that actually needs more than a dozen non-array parameters?

1

u/Lost4468 Apr 08 '21

Not off the top of my head because it's hard to recall something I could also write here. One thing that's not uncommon to see is large structures like matrices or similar have a parameter for every part of themselves.

But I don't know why you're acting like this is rare in languages like python? It's very common to pass around a large number of parameters, it's not bad design in python.

2

u/deegeese Apr 08 '21

You’re proving my point.

A math function that takes a dozen parameters for the matrix elements is a bad design because they could just pass an array.

When Python functions take a ton of parameters, it’s usually because most of them are optional and it’s just syntactic sugar vs passing a dict for configs.

1

u/Lost4468 Apr 08 '21

A math function that takes a dozen parameters for the matrix elements is a bad design because they could just pass an array.

Most do, they allow both. There's plenty of valid reasons you might want both.

And yes there are still plenty of maths functions where it does not make sense to house them in an array.

When Python functions take a ton of parameters, it’s usually because most of them are optional and it’s just syntactic sugar vs passing a dict for configs.

Literally what I already said.

1

u/fakeplasticdroid Apr 08 '21

Not necessarily -- maybe some of those parameters are used to construct properties and some of those properties take hundreds of parameters themselves.