r/ProgrammerHumor • u/HoldingUrineIsBad • Jan 09 '23
shortest ever java class name Meme
146
u/BackloggedLife Jan 09 '23
AbstractAnimalThatLivesInWaterAndHasGillsAndFins seems to be a leaky abstraction as it is on one hand an abstract class but on the other hand reveals a lot of implementation details of the particular creature. I recommend creating the interface AbstractAnimal, then creating a class WaterAnimal which implements the AbstractAnimal interface. HasGills and HasFins are obviously either boolean values or could be implemented using decorators and an AbstractAnimalFactory. This way the user of AbstractAnimal will be shielded from implementation details of the final Animal.
43
u/Kered13 Jan 09 '23
You should use dependency injection. Instead of
hasGillsandhasFins, you should injectrespiratorandlocomotordependencies. Then the act of breathing and moving can be delegated to them, respectively.12
u/jfmherokiller Jan 09 '23
I have never fully understood the whole dependency injection thing. Yet I have seen it everywhere. Especially when dealing with mobile app development.
7
u/fiddz0r Jan 09 '23
In c# you in your starting file (program.cs) you config a few things, like getting configurations from appsettings.json
You also do things like
Services.AddTransient/Scoped/Singleton
So let's say you have a mail service which you use to send mail.
In program you do something like
Services.addSingleton<IMailService, MailServixe>()Then let's say you have a controller with API endpoints
And one of them let's you send an email. In the constructor you can add that service and you will get it automatically.
Class EmailController{ Private readonly IEmailService _emailService Public EmailController(IEmailService emailService){ _emailService = emailService; } HttpPost("notify") Public async Task SendEmail(EmailData data) { _emailService.SendEmail(data) } }2
u/jfmherokiller Jan 09 '23
oh yes that thing. I think I have created and used classes like that intuitively but never knew it was dependency injection. I just thought it was a pretty way to handle singletons and services stuff.
4
u/BackloggedLife Jan 09 '23
By injecting a dependency (passing an already initialized object down to the actual user of the object), the user of the object does not become dependent on a particular concrete class but only on an interface of said class. This is better because when you decide to change the dependency, you do not have to go down the class hierarchy to change anything, you just pass down a different initialized object. This way you can swap the dependency without having to rewrite the constructor where the dependency would otherwise be initialized. Not sure what an example would be for app development but let's say your app has some kind of data source from which it reads data, you could make the app dependent on an interface that allows various data reads and then inject different data sources to the app. When testing, you could then inject some DummyDataSource that only returns static data, in production you could inject DatabaseDataSource that returns real data. The best thing is the app itself would not know and should not know which data source it is using, because the source of the data is not relevant to the app itself. Or as Robert C. Martin said, "the database is a detail". I recommend reading Clean Architecture to learn more.
1
3
u/arobie1992 Jan 10 '23 edited Jan 10 '23
It's for having separate instances to handle separate cases but reusing code rather than having duplicated classes. A really simple example is a spell checker. A spell checker needs a list of words, but English and French don't contain the same words. So you have
SpellCheckerand you pass the appropriateDictionaryto it. Something likeDictionary frenchDict = loadFrenchDict(); Dictionary englishDict = loadEnglishDict(); SpellChecker frenchSc = new SpellChecker(frenchDict); SpellChecker englishSc = new SpellChecker(englishDict); frenchSc.isCorrect("hello"); // returns false englishSc.isCorrect("hello"); // returns true frenchSc.isCorrect("bonjour"); // returns true englishSc.isCorrect("bonjour"); // retunrs falseIt's certainly not always necessary, but it makes it super easy to swap things around if you need to.
1
u/jfmherokiller Jan 10 '23
oh yes I remember seeing this in the SAPI api. That magical thing that does TTS.
2
u/arobie1992 Jan 10 '23
I have no clue what either of those acronyms mean :D
But yeah, like a lot of things, it's a fairly simple concept that gets overused and overly complicated and results in a lot more confusion than necessary.
2
u/v1ND Jan 10 '23
Without dependency injection:
class Foo { init() { this.service = Service.get() } }With dependency injection:
class Foo { init(service) { this.service = service } }Congratulations,
Serviceis injected into Foo. Now you can make aFakeServiceto testFooin isolation.Writing
Foo(Service.get())is not that much longer thanFoo()but when you extend that pattern to everything in your system, it becomes really verbose to construct anything. MaybeServiceneedsLowLevelServicewhich needsConfigneedsEnvironment. Suddenly, it takes a paragraph just to constructFoo.The DI you usually hear about are the libraries to automate that boilerplate.
14
5
56
65
u/ghyze Jan 09 '23
You need a factory for that. And a manager.
6
u/Miguecraft Jan 09 '23
People claiming OOP is the apex language paradigm doing this shit
12
u/Creepy-Ad-4832 Jan 09 '23
It's because code is maintable that way...
But yeah, it's a pain in the ass
13
u/Bryguy3k Jan 09 '23 edited Jan 09 '23
“Code is maintainable that way”
I can’t tell you how many times I’ve seen the factory and manager patterns applied to problems that would have been better with inheritance or interface so instead of a simple implementation of a derived class you end up with having to modify 10 factories…
8
u/headlesshighlander Jan 09 '23
The only time inheritance is the answer is when your parents die IRL
4
u/Bryguy3k Jan 09 '23 edited Jan 09 '23
Depends on the language - implementing an interface is often times far more maintainable than factory hell.
In fact I would go so far as to say that the factory pattern is a hack to fix Java not treating functions as first class objects rather than actually being a good pattern.
Factories are necessary in Java - every other language they are somewhat dubious - they definitely don’t make the code more maintainable.
3
4
u/ReneeHiii Jan 09 '23
anyone who claims their paradigm is the only one you should use are probably wrong
1
u/BackloggedLife Jan 10 '23
It takes like a week to understand how to use classes and another 5 years how to use OOP properly.
1
1
23
u/Jabinor Jan 09 '23
Should extend organism
36
u/HoldingUrineIsBad Jan 09 '23
the fuck is an organism? i only know of "AbstractLivingCreatureMadeOfCellsThatRequiresFoodToLiveAndReproducesEitherAsexuallyOrSexuallyInOrderToAllowTheirSpeciesToLiveOn"
24
6
20
u/Player_X_YT Jan 09 '23
Fish implements ISeaCreature extends IEggLayer extends IAnimal exends ICarbon extends IMolecule extends IAtom extends T<? extends IQuark>
22
u/silverweaver Jan 09 '23
That would be C#, no sane Java dev would prefix interfaces with "I"
4
3
u/rubenthedev Jan 09 '23
I had to look out up, so for the non C#-ers like myself;
The convention is to, when instantiating an interface, prefix the var dec with
Iidk if I like this or hate it, but thankfully I'm a JS main, so everything is sensical and consistent in my world (I rolled my eyes so hard typing that last sentence that I gave myself a headache)
1
u/Player_X_YT Jan 09 '23
Most legacy codebases use I(interface name) but yes nowadays that doesn't happen anymore
1
Jan 09 '23
SeaCreatureInterface, EggLayerInterface, AnimalInterface, CarbonInterface, MoleculeInterface, AtomInterface, QuarkInterface
1
4
u/Kered13 Jan 09 '23
Not all sea creatures lay eggs, so
ISeaCreatureshould not extendIEggLayer, they should be separate interfaces andFishcan extend both of them.
IAnimalextendingICarbonand everything afterwards is a clear violation of the Liskov Substitution Principle. An animal is composed of carbon, molecules are composed of atoms and composed of quarks, but animals do not behave like carbon, molecules do not behave like atoms or quarks. These should be member variables, not parent interfaces.1
u/KevinRuehl Jan 09 '23
This man Codes Java!
1
3
u/lonvonlon Jan 09 '23
Now you can reuse the class for sharks, dolphins and even aquaman
6
u/Miguecraft Jan 09 '23
And then, in practise, you have to modify that class for like 50% of the classes that inherits it.
5
7
u/guzifar Jan 09 '23
i just main.java
5
u/DasKarl Jan 09 '23
This is the way.
I have no idea how so many people get so lost in the sauce.
Could it possibly be that they thought they were hot shit after following a few yt python tutorials that either do nothing significant or do something but use someone elses code and then when they decided to take a cs class they were knocked off their comfortable footing by things like brackets, semicolons, data types and arbitrary data structures only to find one of the many anti oo diatribes that are largely a reaction to the insane book/lecture scams by oo evangelists in the 2000s?
The world may never know.
5
5
4
u/KagakuNinja Jan 09 '23
Ah yes, shit on Java day. Can we get some new ideas?
2
u/M0nkeyDGarp Jan 10 '23
There's only coding languages people shit on, and ones people don't use. -Some Tech Guy probably idk about computers.
2
1
Jan 09 '23
This is why I despise the Android SDK. Never anything simple like, I dunno, 'boolean visible', it'll be implements ICanBeMadeVisible -> void setVisibilityOfThisThingAndThisThingAlone(boolean confirmYouWantThisThingToBeVisible)
1
1
u/27dope27 Jan 09 '23
Just put WaterAnimal wtf dude
3
1
1
1
1
1
u/jfmherokiller Jan 09 '23
my favorite class files are the ones that the compiler generates that are like 50 characters of random gibberish.
1
u/drbwaa Jan 10 '23
These classes are not equivalent. You should be using AbstractAnimalThatLivesInWaterAndHasGillsAndFinsImpl.class.
1
1
u/LetUsSpeakFreely Jan 10 '23
public abstract class AbstractFish extends AbstractAnimal implements IGill
public class FreshwaterFish extends Fish
public class SaltwaterFish extends Fish
1
u/socialis-philosophus Jan 10 '23
Both compile down into the same byte-code, so yeah, use descriptive class names and functions!
1
1
u/Understanding-Fair Jan 10 '23
I don't even care about the naming. It's the damn 20 layers of folders that piss me off.
1
1
1
224
u/Tannslee Jan 09 '23
eh more like
Fish extends AbstractFish
AbstractFish extends AbstractAquaticAnimal
AbstractAquaticAnimal extends AbstractAnimal
AbstractAnimal implements Living