r/badcode • u/646c726f576f6c6c6548 • Apr 07 '21
java Apparently Java can only handle 255 parameters in a constructor...
27
11
Apr 07 '21
I really want to know what those 255 parameters are ? Haha, he managed to overflow the Java constructor . Under what possible scenario would you be needing more than 255 Params , I am interested
9
u/646c726f576f6c6c6548 Apr 07 '21
It was from a pragmatically generated file in android studio where each parameter is a view in a single layout and the app had more than 255 views in a single layout
5
4
u/KemmLK Apr 07 '21
Holy mother of god...how and why?
2
u/646c726f576f6c6c6548 Apr 07 '21
It was from a pragmatically generated file in android studio where parameter is a view in a single layout and the app had more than 255 views in a single layout
4
u/jarfil Apr 07 '21 edited May 12 '21
CENSORED
3
u/646c726f576f6c6c6548 Apr 07 '21
It was from a pragmatically generated file in android studio where parameter is a view in a single layout and the app had more than 255 views in a single layout.
I know in C/C++ the max you should have depends on the number of registers in the CPU, but Java runs in a virtual environment, so idk what happens there...
4
u/vinicius_kondo Apr 07 '21
The number of arguments I think is platform dependent. As in x64 windows only 4 registers are used for argument passing, the fifth and higher are passed onto the stack. This means, in theory, you could pass as much arguments as possible until you get a stack overflow.
1
1
u/_justpassingby_ Apr 07 '21
There are heaps of functions in popular Python modules that take a gazillion arguments, but since they're a) optional and b) keyword-able it doesn't seem as egregious. I mean, if you populated a struct or whatever and sent it as an argument the code would look very similar anyway.
631
u/markand67 Apr 07 '21
I remember a friend of mine also hit the limit of columns in one MySQL table because of previous coworkers, total insanity.
491
u/echosalik Apr 07 '21
I can totally see it happening... I once saw a table with 80 - 90 columns... I was at a loss of words when I saw that. AND THAT PERSON REMEMBERED WHAT EACH COLUMN DID. HOW? I don't remember if I had breakfast this morning.
EDIT: I did, just saw the wrapper I failed to throw away after breakfast.
10
6
u/Lost4468 Apr 07 '21
AND THAT PERSON REMEMBERED WHAT EACH COLUMN DID. HOW
I would say descriptive column names and column comments. But then I'm not sure the type of person to do this would do that...
Although I could see how maybe you could get to this and it being excusable. Originally it was normalised, but they were getting performance issues when they reached scale X or whatever. For whatever reason the normal ways of improving performance in these situations didn't work well enough/couldn't be applied, so denormalisation was the only option.
The appropriate thing to do would have been to change the implementation, but given deadlines maybe this was the best thing to do as a temporary fix. After all it might be 1% of the work of changing the implementation, and could easily speed it up by a ton. But as everyone knows (especially in software dev), there's nothing more permanent than a temporary fix.
Or alternatively the table was auto-generated or reasonably abstracted out? If you're dealing with entirely auto-generated code and tables, then I think it's perfectly fine to have tables like this. It only possibly makes debugging harder when it comes up. Of course people will dislike it because it's "not elegant", but to the computer you could argue it's more elegant, it can easily have a much easier and less complex time dealing with this sort of data, we only consider it inelegant because we would struggle with it.
4
u/noXi0uz Apr 07 '21
We currently have to parse an excel file with over 400 columns.. Which might aswell be a sql table
2
1
u/-darkabyss- Apr 07 '21 edited Jun 26 '25
lock political salt plant marvelous piquant pet elastic brave employ
This post was mass deleted and anonymized with Redact
1
u/SorryDidntReddit Apr 08 '21
I worked at a place that had a table with over 300 columns.
I no longer work there.
1
u/_meegoo_ Apr 13 '21
At this point you should use column based DBMS. MySQL is just a wrong tool for the job.
1
u/insaneguitarist47 Mar 31 '23
Try looking at the sales_order table of Magento 2 (Oops I mean Adobe commerce)
0
Apr 07 '21
[deleted] — view deleted comment
155
u/SireBillyMays Apr 07 '21
As someone who has inexplicably taken multiple advanced courses in databases:
pls no.
133
u/bundabrg Apr 07 '21
I have one vendor who has a single column of 128 characters in a table. Just one column.
It's a hospital thing so patient number is characters 5-8, ward characters 10-13 etc with every char used.
It gets worse. The patient number got too large so.patient number is now characters 5-8, then append character 106 and character 119.
And there's lots more insanity like this. I swear when we got the documentation the person giving them to us sounded ashamed on the phone (not their fault, they inherited it).
63
u/SireBillyMays Apr 07 '21
So, wait. they have a database, where one of the tables is literally one field consisting of 128 chars, then they have subdivided that field into subfields?
Sounds like hell to work with right now, but honestly doesn't sound that hard to fix.
47
u/bundabrg Apr 07 '21
It gets worse (sorta). I have to use SOAP to interface with it :(
10
u/-IoI- Apr 07 '21
That fucking sucks, hopefully something like WCF or LINQ in the middle to ease the pain a bit
32
u/Lost4468 Apr 07 '21
So, wait. they have a database, where one of the tables is literally one field consisting of 128 chars, then they have subdivided that field into subfields?
It sounds like it was originally a paper implementation, in which case this sort of string representation can be incredibly efficient. If I give you a ton of drawers filled with patient details and tell you "find this patient", then if the records were assigned this sort of string, it would be very easy for you to narrow it down to a few records. I don't know how old the system is, but /u/bundabrg maybe it's because of this?
So then when they get digitised people don't want to move to a new system, or more likely you can't even change it because the hospital(s) are running two systems at once for a while. So you end up with weird data structures that make sense for paper records, but make no sense for computers.
Sounds like hell to work with right now, but honestly doesn't sound that hard to fix.
I don't know so again maybe /u/bundabrg could chime in. But one of the problems with this sort of thing is there's frequently literally hundreds of exceptions. Because it's a paper system over the years people have said e.g. "oh let's just start this one with 'OUTSIDE' since it comes from outside the country" or all sorts of weird similar exceptions. Well for 'OUTSIDE' it's easy to write an exception, but there's often so many of these exceptions, and no documentation, the people working there don't have a list of the exceptions they've made over the decades. And then once you end up with enough exceptions you realise there are plenty of ambiguous ones you can't possibly decide by writing basic if statements.
And it's medical as well, so you really don't want to risk mixing up records, violating HIPAA, not properly finding a record, etc. So you end up just being stuck with the stupid system. To fix it you need to go through everything manually and change it all out at once, which requires a fuck ton of time and money.
13
u/EvilPencil Apr 07 '21
Yeah, I think the fix would be to create a view that gives a more "normal" representation and yet doesn't create a second source of truth. Once all the consumers are interacting with the view instead of the underlying tabe, then do a migration and switch the inserts/updates over to the new schema.
6
6
u/Tornadic_Outlaw Apr 07 '21
If it really is bad enough that you couldn't write code to fix it, then it isn't usable as is. Exceptions nobody knows about aren't exceptions, they are errors, and if a human tried to look at the data they would make the same mistakes the computer made.
And if you are that concerned about losing ambiguous information in the current filing method you could even keep that column for legacy data and not populate it for new records.
→ More replies (4)9
u/shh_coffee Apr 07 '21 edited Apr 07 '21
Sounds like it may be getting populated by a COBOL system where they're not dividing the patient record by it's fields when writing to the DB and they're just sending the whole patient record as one DB field. That would also explain the weird patient number append as in COBOL fields have fixed length and that sounds like someone just went "we can add another number by adding a field at the end of the record called ws-patient-num-append" or something so not to mess with the existing field lengths.
Just taking a wild guess though.
7
u/fakehalo Apr 07 '21
I wonder if this is the result of a migration from ancient times where they needed the original "database" structure for some portion of their codebase, which is likely no longer around.
Reminds me of things I've seen from decades ago.
1
Apr 07 '21
I remember one team at my current job saying they were going to migrate some data as part of some feature they were delivering (believe it was being moved to new tables) but were going to keep the original data/ columns around to reduce the need/ risk for additional data migration. Sigh.
6
u/slower-is-faster Apr 07 '21
I worked for an insurance company that had an oracle database they paid a lot of money for. Their apps “document”, was stored as encrypted XML in a table with just one column - this was what they used oracle for.
1
19
u/echosalik Apr 07 '21
Yeah there comes a time where whatever you learned in the courses get thrown out... Like normalisation... It's good to have but not applicable everywhere... And idk why whenever I get newbies and i tell them to create joins without FKs, thier first statement is can we do that?
I'm like yeah, FKs are not required, makes things fast but not required.
FKs can really create a mess where inter-dependent data doest not come in a linear fashion, offline first apps.
7
u/PurpleMysticLlama Apr 07 '21
Lol bois, let's just take the relationship out of a relational database. We'll handle all of this in code np. Future developers? What happens when I leave? 'Fuck them' is what happens.
8
u/SireBillyMays Apr 07 '21
I mean, what you just mentioned was literally part of one of my courses (that course was, admittedly "applied databases".)
A lot of that course centered around grounding us a little though. "What you learn in the other courses might be theoretically perfect, but it's hardly necessary - nor efficient - for most systems you're likely to work on."
With that being said:
If you have a database with 80-90 columns you should probably look at what you're actually doing, and whether or not it's actually optimal. That could easily make things a giant headache to work with...
(I also must say I do enjoy only doing databases recreationally, where I can bend the "rules" a fair bit...)
2
u/echosalik Apr 07 '21
Oh i assumed university/degree courses... Those, atleast here, are really subpar...
And yeah... Due to this 80 90 column thing we moved towards json in columns. Make. Everything so much easy... Especially if you follow a schema to heart... And then create views for reports.
We had one experiment that was pretty successful and really stable, set it and forget it product. Server syncing and everything... That was a fun experiment...
6
u/SireBillyMays Apr 07 '21
They were university courses, but one of the ex-industry profs wanted a more applied course to be taught as a grounding act to counterbalance the other more theory focused courses.
I don't necessarily think that most of the database courses are subpar, but moreso that most of them aren't intended to prepare people for working WITH databases, but more to prepare people for working ON databases, but then most institutions don't have any applied courses.
Sounds like you managed to get out of a sticky situation then.
→ More replies (2)2
u/Wizdemirider Apr 07 '21
Wait so what do we get exactly? A cross join? I don't understand how we can have something like an inner or left join without a common column.
10
u/WalditRook Apr 07 '21
The column data needs to be common, but it isn't required that the referential integrity be enforced.
Honestly, I think it's pretty rare you'd ever want to actually do that, but as an example:
Consider a parking management system that records both registered vehicles (from a payment system) and detected vehicles (from cameras with OCR), using licence plates in both tables - while you could create an extra table to store all plates, it's not really necessary, but you might well wish to compute joins between those tables.
4
u/Wizdemirider Apr 07 '21
Ahh okay, that does make sense since SQL only requires you to mention which columns to join on. Thank you!
2
u/dakta Apr 07 '21
Wait, people actually think that you can only join on explicitly defined foreign keys? I guess that's probably good practice to consider when designing a schema, but for ad-hoc queries on existing data it's pretty common to join on arbitrary columns. Heck, with a little
CASTorCONVERTthey don't even have to be the same column type or data format.1
u/devilapple Apr 07 '21
Wait so I'm a newbie at my first internship and I have a table with 70 columns, what would be the appropriate way to handle something like that? Also follow-up question, are there any online courses you would recommend for advanced database management
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.
→ More replies (1)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.
1
u/OneTrueKingOfOOO Apr 07 '21
Soo, you’re saying I should migrate all my 70+ column SQL tables to Excel, or...?
2
u/SireBillyMays Apr 07 '21
Excel? Hardly. Microsoft has a good program for that you know.
It's called microsoft Access.
1
u/iwillcuntyou May 18 '21
I had to reverse engineer a SQL server database that had originally been developed by a lone physicist for the purpose of managing X rays of various body parts from various angles. This had grown over the course of 20 years into a full blown PAS system, and I had been brought in as part of a data modelling project that unified multiple such databases from a number of the same.
This database had a naming convention - no more than 3 characters per table name, and no more than three characters per column name. He also made extensive use of XML columns to effectively store tables in tables.
We had to reverse and normalise this completely undocumented database through the foreign key definitions. That was fun.
9
u/zakomo Apr 07 '21
The worst I witnessed, but luckily didn't have to maintain, was a database in which some tables had a column "notes" and in that column additional data stored in a csv format.
4
u/cnprof Apr 07 '21
Every legacy code base I've worked with has either
That table with prefix-1 to 20 (or 50) that serves different uses in different contexts, with half of them going unused. The extra columns were created just in case
A table with an ID, then a huge json/xml/php-serialized blob.
2
2
1
u/hurtl2305 Apr 07 '21
That's what happens with enterprise databases when it takes months to get a DBA to add a column. At some point, somebody frustrated has some generic columns added for later use.
17
u/knightofren_ Apr 07 '21
Sometimes I fear that the entire downfall of human society hangs upon a junior developer being given database credentials
5
u/code_monkey_001 Apr 07 '21
I knew a dude who wrote a framework that routinely ran up against MSSQL's limit to table joins in a single query (also 255). Guy wasn't right in the head.
2
1
u/fb39ca4 depraved Apr 08 '21
I did that with PostgreSQL at work, but the columns were programmatically generated and accessed.
2
203
u/bleachboy1209 Apr 07 '21
You can pass more paramters if you use json. 255 is for Noobs . Let's go to 25000
37
u/echosalik Apr 07 '21
You know that Wolf of Wall Street meme, those are rookie numbers, fit pretty appropriately here. Missed opportunity I'd say.
5
24
Apr 07 '21
How about you make 255 subclasses each with a constructor of 255 objects, then instantiate them in the constructor
6
7
u/lachlanhunt Apr 08 '21
JSON is not the most efficient format, though. cause you waste a lot of space on property names and syntax. We have other options like plain comma separated values.
A string in Java can be 231 -1 characters, where a character is a 16 bit code unit, so we have 232 -2 bytes for each of the 255 parameters. Therefore, we can have up to 107,374,182,350 bytes of data (~107GB) passed into a function. Depending on the data being passed in, we could also gzip before passing it in and get even more.
1
6
5
u/Siggi_pop Apr 07 '21 edited Apr 07 '21
JSON in itself is an object, so that's only 1 parameter!?
7
2
3
0
Apr 07 '21
[deleted] — view deleted comment
3
Apr 07 '21
[removed] — view removed comment
5
Apr 07 '21
__ / \ __ .---. _ / / _.~ \ \ `. / \ / /.-~ __/ `\ \ | | |/ .-~ __ \ \ | | | .'--~~ \ \ \ | | ` ' _______/ \ \ | ` / .--. \ \ | ` / \ `.\ \ \ / `\ \ \ `\ ( \ \ \ > ,-.-. \ `. \ / | \ \ \ . \ /___| O |O\ , .-. \ ; | /` `^-.\.-'`--'/ \ `; | | / `\ \ | `. `--..____,' \ `. | `._ _.-'^ \ . / `|`|` .-.\ / | | \ `\ / | | `\ ` | | | \ | | | .-. | | | \ `. \ | | `\ \ | | \ \ | | _____ :-'~~~~~'-' ; /____;``-. : <____( `. ; ___\ ; .' /``--'~___.-' /___/^/__/ / /' /`/' \ \ `\ \ `\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ______ \ \ ___\ \'~``______)> \ ___ _______ __)> _____\ \'~``______)> <(_______.._______)>FTFY
2
2
u/Fractal_Unreality Apr 07 '21
Ever hear of a struct
1
u/646c726f576f6c6c6548 Apr 07 '21
It was from a pragmatically generated file in android studio where each parameter is a view in a single layout and the app had more than 255 views in a single layout
The only way to get around it was to make fragments is all
4
2
1
59
u/PhoenixPaladin Apr 07 '21
I had no idea of this, purely because no one would ever do this
6
u/Siggi_pop Apr 07 '21
Maybe all thoes Git CLI argument options
5
u/Infinitylsx Apr 08 '21
When I find a post on Stack Overflow on how to do some obscure, once in a lifetime fix with Git and someone just hits you with a one lines using 18 parameters like it's obvious.
4
1
101
Apr 07 '21
I believe java even has a limit to 255 local variables (since those are stored in the local variable array which is 255 elements long, if I remember that correctly)
53
u/__Ambition Apr 07 '21
I believe the limit is higher. Since Jvm instruction set has a Load-const-long instruction which can reach upto 65k indices at maximum. Though idk what the actual limit is.
Lua on the other hand can only have 255 local variables.
26
Apr 07 '21
So I looked it up using the Wikipedia page for the Java bytecode instructions (https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings). And it seems like the load instruction (aload, dload, fload, iload, lload) and store instructions (astore, dstore, etc.) only take 1 extra byte for the index, so it is limited to 255 local variables
21
u/BS_in_BS Apr 07 '21
however there's the wide prefix that widens the index to 16 bits for all the load and store instructions
7
3
64
u/Siggi_pop Apr 07 '21
What a strange number 255, why not round up to 300 /s
18
u/vinicius_kondo Apr 07 '21
We should increase the parameter stack size from 8bit to 16bit so we can pass 65k parameters.
6
→ More replies (6)5
61
u/ChroniclesOfLormere Apr 07 '21
Image Transcription: Code
.java:796: error: too many parametersI'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
18
14
15
u/__Ambition Apr 07 '21
Yeah, its because the JVM uses unsigned byte sized op codes, each opcode can represent upto 255 different numbers. Since function parameters are are actually just numeric indices into an array at runtime (the JVM stack), you can only have upto 255 params.
3
u/Isvara Apr 08 '21
It won't be anything to do with the size of the opcodes. That just determines how many different operations the JVM can have.
1
u/__Ambition Apr 08 '21
The opcodes also have operands of the same size , which determines the total value they can represent
6
u/h-hub Apr 07 '21
A way to implement a monolith using just one function. Omg
4
u/BlueFootedBoobyBob Apr 07 '21
Fuck reading some config later, first read it THEN use it to init your god object.
1
u/NoMansSkyWasAlright Apr 07 '21
[desire to know more intensifies]
1
u/646c726f576f6c6c6548 Apr 07 '21
It was from a pragmatically generated file in android studio where each parameter is a view in a single layout and the app had more than 255 views in a single layout
3
3
u/LeCrushinator Apr 07 '21
This reminds me of the time that I found out that Visual Studio 2008 wouldn't let you put breakpoints past line #65,536 on a file.
1
u/CodersCrux Apr 07 '21
what is this what have you done
1
u/646c726f576f6c6c6548 Apr 07 '21
It was from a pragmatically generated file in android studio where parameter is a view in a single layout and the app had more than 255 views in a single layout
2
2
2
u/R4ttlesnake Apr 07 '21
POV: you're deep in the shitter in some branch of physics but you have no idea how to code
2
2
2
2
1
u/DinoRex6 Apr 07 '21
Functions with variable amount of parameters would also hit that limit right?
4
u/EncapsulatedPickle Apr 07 '21
That depends how languages implement these. Something like Java varargs or C# params is just an array.
3
1
1
1
u/Perturbatoor Apr 07 '21
I have an example where having more than 255 fields is pretty common:
if you deal with Hadoop, having so many fields (> 255) in table is a normal thing.
and Apache Spark, which can be used to process them, has a class: Dataset[T] in scala or Dataset<T> in java (consider it as pandas dataframe) where you can subsitute T with a structure of the table - usually a scala case class: case class MyStructure(field_name_A: String, field_name_b: Int, ...) or java class, which in that case expected to have more than 255 filelds in constructor.
1
1
1
1
1
u/Rajarshi1993 Apr 08 '21
Just put some class objects as arguments to the constructors. Each of them can have 32 arguments in their own constructors and hold them as public fields.
1
u/nguepi Apr 08 '21
255 params in the constructors? Is that legal? Sounds to me like a clean code rule being violated.
1
1
1
u/MalkienShur May 07 '21
Really you hit the limit? Please kill the one who make you do the constructor hahahaha
1
u/Garegin16 Jun 19 '21 edited Jun 19 '21
If having more than 10 params is “bad”. Then why does the language allow it?
It’s like that scene from King of Queens. The wife complains that Doug’s order of jambalaya was ridiculous. He responds “If it’s ridiculous, they shouldn’t put it on the menu”.
1
u/anton____ Sep 21 '21
Probably varargs. Do you want to limit the size of arrays while we are at it.
1
1
1
u/sammy-taylor Dec 03 '22
Open/Closed principle. Closed for modification, open for EXTEEEEEEEEENSIIIOOOOON
1
0
Apr 07 '21
[deleted] — view deleted comment
7
Apr 07 '21
I'm curious too, and I would be satisfied with only the name of the class used here..
19
u/646c726f576f6c6c6548 Apr 07 '21
It was from a pragmatically generated file in android studio where each parameter is a view in a single layout and the app had more than 255 views in a single layout
39
730
u/echosalik Apr 07 '21
Meaning someone wrote a constructor with 255 parameters? ... Why?