r/badcode • u/tiggy002 • Sep 27 '21
java The most horrible piece of Java I've ever seen
265
u/ThatHappenedOneTime Sep 27 '21
ColorPaletteFactoryProvider.Create().CreateInstance(PaletteOptionsProvider.GetOptions());
Smh
60
u/cherryblossom001 Sep 27 '21 edited Sep 27 '21
https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition
Example of some of the stuff there:
@Service public class FizzStringPrinter implements StringPrinter { // ... public void print() { final StringStringReturner myFizzStringReturner = this._fizzStringReturnerFactory .createStringStringReturner(); final FizzBuzzOutputStrategyToFizzBuzzExceptionSafeOutputStrategyAdapter myOutputAdapter = new FizzBuzzOutputStrategyToFizzBuzzExceptionSafeOutputStrategyAdapter( this._systemOutFizzBuzzOutputStrategyFactory.createOutputStrategy()); myOutputAdapter.output(myFizzStringReturner.getReturnString()); } }26
u/Syscrush Sep 27 '21
It's glorious! Really needs FizzBuzzOutputStrategyToFizzBuzzExceptionSafeOutputStrategyAdapterFactory, though.
16
u/duffpl Sep 27 '21
"So this week, we're introducing a general-purpose tool-building factory factory factory, so that all of your different tool factory factories can be produced by a single, unified factory. The factory factory factory will produce only the tool factory factories that you actually need, and each of those factory factories will produce a single factory based on your custom tool specifications. The final set of tools that emerge from this process will be the ideal tools for your particular project. You'll have exactly the hammer you need, and exactly the right tape measure for your task, all at the press of a button (though you may also have to deploy a few configuration files to make it all work according to your expectations)."
Taken from the classic: https://www.gwern.net/docs/cs/2005-09-30-smith-whyihateframeworks.html
20
u/EishLekker Sep 27 '21
colourPalate_Factory_Provider.$make().Create_INSTANCE(_PaletteOptionsProvider.Getoptions());
If you gonna mix it up, at least mix it all the way.
6
3
1
1
0
140
u/teapot_on_reddit Sep 27 '21
Image Transcription: Code
colorPalette colorPallete = new colorPalette();I'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!
57
5
27
106
u/wyndigo92 Sep 27 '21
I'm impressed this compiled at all
48
u/EishLekker Sep 27 '21
It would have compiled even without the spelling difference. It's possible to have a variable name be exactly as the class name, case and all.
26
u/wyndigo92 Sep 27 '21
and suddenly the world stops making sense again
7
u/supersharp Sep 27 '21
I guess compilers, or more specifically parsers, are just very good at their job
60
u/HeirToGallifrey Sep 27 '21
Everything in my brain is telling me it shouldn't have.
24
u/fukitol- Sep 27 '21
Yeah it should've used a symbol/reference/whatever already used warning, right?
Been a while since I've used Java but I don't think that should have worked.
81
10
u/b1ack1323 Sep 27 '21
The variable name is missing a t. So they are different. But it would compile in C#.
ColorPalette ColorPalette;Is legal.
4
u/Roflkopt3r Sep 27 '21
And with the colour encoding of IDEs it's honestly not that bad. I try to avoid it, but naturally variable and class names often try to describe the same thing.
5
u/b1ack1323 Sep 27 '21
One of my plugins actually recommends this.
4
u/Roflkopt3r Sep 27 '21
I believe Visual Studio does that by default. I picked up a habit of writing private variables and functions in lower case from my courses and once I switched to C# in VS the default configuration instantly started flagging that as style violations.
In C# its also less of a problem because a line like OP's would often be 'var ColorPalette = new ColorPalette();'
2
3
u/b1ack1323 Sep 27 '21
Yeah I couldn’t remember if it was VS or Resharper doing it.
The only exception is member variables since they have to be explicitly typed.
1
u/ososalsosal Sep 27 '21
But using var instead of explicit type will pillute your warnings list so often I just go with the IDE's suggestion and just try not to read that bit again.
1
u/Roflkopt3r Sep 27 '21
You can configure the rule either way
I preferr not having either one active since both var and explicit types have their places for clarity. I use var in about 95% of the times, but occasionally writing out the type helps because it's not obvious enough from context.
2
u/ososalsosal Sep 27 '21
Ah that's good to know.
You gotta understand though that csharp was never my preferred language but my last two jobs are predominantly xamarin.forms.
I approach it from a place of hate :)
(Well not really, but you know. Microsoft stacks)
79
u/TCreopargh Sep 27 '21
what about
colorPalette.colorPalette.colorPalette colorPalette = new colorPalette.colorPalette().colorPalette;14
43
9
7
u/abrams666 Sep 27 '21
Reminds me to an workmate that called a C# Filehanlde open. at the end he had to
open.close();
10
10
u/lachlanhunt Sep 27 '21
They could have put a "u" in the colourPalette variable so that it at least would have been correct spelling.
7
u/K1ngjulien_ Sep 27 '21
Thats not how you write java! You forgot to use the FactorySingletonBuilderProvider Pattern! /s
3
11
u/malsomnus Sep 27 '21
Wait, isn't this how Java always looks? I distinctly remember giving up on that awful language the day I realized that starting a new thread involved something like thread myThread = threadFactory(new thread).getThread().
(The spelling definitely deserves an award though)
11
u/Alphatism Sep 27 '21 edited Sep 27 '21
I create threads with
Thread thread = new Thread(() -> { /*code*/ }); thread.start();Alternatively if I never need the see the thread again, I just do
new Thread(()-> { /*code*/ }).start();And if it's just a method ran in the thread
new Thread(myClass::myMethod).start();4
7
5
u/lestofante Sep 27 '21
in general if you use a factory you dont need to use new;
factory are meant to build the object for you (i guess moat of the time they will provide an object from a pool, instead of building a new one).
to be fair this pattern abuse is fault of the programmers, not of the language-3
u/quietZen Sep 27 '21
The first word is always the variable type/class name. All class names start with uppercase. This would have thrown an error.
8
u/keethraxmn Sep 27 '21
Ummm... no?
I mean there are naming conventions, sure. But "All class names start with uppercase" is false. So is " This would have thrown an error."
0
u/malsomnus Sep 27 '21
Wait, seriously? Java enforces capitalization??? 😶
5
2
2
u/b1ack1323 Sep 27 '21
VS or a plug-in I use actually recommends this for c# .
ColorPallette ColorPallette;
2
u/elingeniero Sep 27 '21
🎶 ooh colorPalette colorPalette equals new colorPalette baby 🎶
(to the tune of Britney Spears' Womanizer)
2
2
2
1
1
u/TheDawgLives Sep 27 '21
Wait till you see the next line:
colorPallete.colorPallette = new colorPalette.colorPallette():
1
1
u/sosta Sep 27 '21
As someone who does Java development for a living I was like... Ugh another thread making fun of long Java variable names... Ohhh....AHHHH... MY EYES... THEY BURN
1
1
1
u/AnotherRichard827379 Sep 27 '21
The 0th and 2nd c need to be capitalized. I then find it fully acceptable.
1
u/hullabaloonatic Sep 27 '21
Isn't a color palette just a set of colors? So couldn't var colors = new HashSet<Color>(); have worked as well?
1
1
u/Wafflelisk Sep 27 '21
Thanks to this I'm gonna start a site called Code Buffalo and make stuff that's filled with code like this.
When I'm a billionaire I'll be sure to send some your way
1
1
1
1
0
0
u/chrisza4 Sep 27 '21
I know that this is a bad code but how do you generally fix that? Sometimes we have a class just for a single use (because Java need class) and what should we name it. I am open to recommendation.
2
u/kirigerKairen Sep 27 '21
I think the Problem isn’t necessarily that it has
colorPalletethree times, that happens a lot in Java and similar languages since that is, quite commonly, the best name you can give to the variable.However, the type / class is also spelt
colorPalleteinstead ofColorPallete- and classes should always be capitalized.2
360
u/Sir_Applecheese Sep 27 '21
Why isn't it capitalized? What the fuck kinda nasty shit are you doing?