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

7

u/SireBillyMays Apr 07 '21

TL;DR: 70 columns could be necessary, but probably isn't. If it's a low-cost system just for internal use it's honestly probably fine to not touch it. The way to reduce/optimize the database could be quite complex, and not necessarily distill-able to a single comment. I took the courses through uni, so I have no clue about online resources.

If it's a pre-existing solution for the org. you're interning at I would probably be a bit careful suggesting changes to that. Other than that, I can give some generic advice. There are "formal" ways to identify, categorize and optimize the tables, but if you haven't taken any courses I probably wouldn't jump straight to that. The TL;DR: way is to look and identify duplicated/unecessary data, and then find ways to split that data out into it's own tables. A simple example can be:

+------------+-----------+--------+------------+
| CustomerID | modelname | colour | colourcode |
+------------+-----------+--------+------------+
|          1 | Barboo    | blue   | #0000ff    |
|          2 | Fizz      | blue   | #0000ff    |
|          3 | foobar    | green  | #00ff00    |
|          4 | Fizz      | green  | #00ff00    |
+------------+-----------+--------+------------+

In the above "database", the colour:colourcode information is directly linked, where the "colour" field is really just adding information to the colourcode-field (or vice versa.) Let's split the table above into two distinct tables:

The table with customer ID and a simple colourcode:

+------------+-----------+------------+
| CustomerID | modelname | colourcode |
+------------+-----------+------------+
|          1 | Barboo    | #0000ff    |
|          2 | Fizz      | #0000ff    |
|          3 | foobar    | #00ff00    |
|          4 | Fizz      | #00ff00    |
+------------+-----------+------------+

And then a separate table linking the colourcodes to the colour names:

+------------+------------+
| colourcode | colourname |
+------------+------------+
| #0000ff    | blue       |
| #00ff00    | green      |
+------------+------------+

For this small example, it doesn't really save that much, but imagine we instead had 1000 entries. That's a lot of unnecessary repetitions that #0000ff is blue.

As for courses, I've only taken database courses at a university. No clue about online resources, unfortunately. But I will tell you that every book I've seen in the field is unnecessarily wordy and tends to cover minutiae that is hardly ever relevant, so I would definitively recommend learning it "distilled" through someone else.

Best of luck!

4

u/dakta Apr 07 '21

Just for human readability, in your example, I would map the colorname in the original table and use that to look up the code. The semantic intention behind the relationship is likely to give a color name that is descriptive, and the exact hex code is an implementation detail. It'll be easier to modify the color palette by changing the hex codes if they're looked up by name.

1

u/SireBillyMays Apr 07 '21

I actually thought about it a little myself, but couldn't quite decide which solution I found better. I ended up setting the colourcode as the (effectively) FK, so that different products could be different shades, but still be described by the same colour (you can have multiple shades of blue, but only one hexcode.) In cases where a "human readable" name isn't present in the second table, then straight up presenting the colourcode might be better than misrepresenting the colour with a not-descript-enough name.

I think both approaches are OK, but subject to consideration depending on what is likely to change.

2

u/devilapple Apr 07 '21

I see, thank you, I really appreciate the advise. I'm still learning a lot, so being able to get other perspectives is pretty helpful.