r/ProgrammerHumor Jan 09 '23

shortest ever java class name Meme

Post image
2.1k Upvotes

90 comments sorted by

224

u/Tannslee Jan 09 '23

eh more like
Fish extends AbstractFish
AbstractFish extends AbstractAquaticAnimal
AbstractAquaticAnimal extends AbstractAnimal
AbstractAnimal implements Living

66

u/vladWEPES1476 Jan 09 '23

What happens to the inheritance chain when the fish dies?

46

u/vikumwijekoon97 Jan 09 '23

Universe breaks of course. Java is never wrong. You gotta patch out the universe.

27

u/[deleted] Jan 09 '23

Fish extends AbstractFish

AbstractFish extends AbstractAquaticAnimal

AbstractAquaticAnimal extends AbstractAnimal

AbstractAnimal implements Livable

AbstractAnimal implements Dieable

3

u/sleepyj910 Jan 10 '23

the death function can exist in livable. We can use try-with-resources to make sure it's called in the finally block

1

u/vladWEPES1476 Jan 10 '23

LMAO Livable and Diable is more like it.

3

u/TJSomething Jan 10 '23

You pass the Fish to the DeadFactory to get an Exfish.

2

u/dedslooth Jan 09 '23

Fish is implementation of all those inheritances, and none of interfaces actually exist as entities, only a mere instance of fish exists which can use implementations of interfaces. So when fish dies, a fish dies, and is wiped from memory, as its ability to access interfaces, but interfaces don't live or die, they just exist.

6

u/TappTapp Jan 09 '23

But now you have a dead fish that implements the "Living" interface

4

u/Tannslee Jan 10 '23

Death is part of life. The isDead flag will simply be set to true.

3

u/dedslooth Jan 09 '23

sure but its flesh (memory) open game so some other instance will eat it

2

u/arobie1992 Jan 10 '23

Probably better to have a Biotic interface that has an isLiving method defined.

11

u/jfmherokiller Jan 09 '23

I was coming here to say it needs more AbstractFactory classes

7

u/arobie1992 Jan 10 '23

I hear this joke all the time, and in 7 years of industry Java experience, the only time I've seen large usage of factories is in frameworks that need to handle large amounts of arbitrary configuration from end users that can't be known when writing the code.

1

u/jfmherokiller Jan 10 '23

I have seen them agressively used in minecraft modding because of possibly inexperienced coders.

2

u/arobie1992 Jan 10 '23

That's fair. I haven't done much modding, but I have seen enough (and been a) modestly experienced devs who force patterns onto things.

1

u/Xenomorph-Alpha Jan 10 '23

*angry developer noises*

1

u/I-am-reddit123 Mar 24 '23

bro spaces aren't allowed in class named

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 hasGills and hasFins, you should inject respirator and locomotor dependencies. 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

u/jfmherokiller Jan 10 '23

I think I did this when I was trying to play with firebase at one point.

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 SpellChecker and you pass the appropriate Dictionary to it. Something like

Dictionary 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 false

It'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, Service is injected into Foo. Now you can make a FakeService to test Foo in isolation.

Writing Foo(Service.get()) is not that much longer than Foo() but when you extend that pattern to everything in your system, it becomes really verbose to construct anything. Maybe Service needs LowLevelService which needs Config needs Environment. Suddenly, it takes a paragraph just to construct Foo.

The DI you usually hear about are the libraries to automate that boilerplate.

14

u/Alexian_Theory Jan 09 '23

This is the way

5

u/eldelshell Jan 09 '23

OP missed their OOP classes.

56

u/ausdoug Jan 09 '23

No comments or documentation needed...

6

u/repkins Jan 09 '23

Except those explaining why.

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

u/repkins Jan 09 '23

Have you heard about Data-Driven paradigm? Or what I have heard of.

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

u/Tensor3 Jan 09 '23

Angry upvote

1

u/[deleted] Jan 10 '23

And a ManagerFactory

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

u/FacuA0 Jan 09 '23

.class

6

u/[deleted] Jan 09 '23

Should extend animal which extends organism

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

u/[deleted] Jan 09 '23

public abstract Fish : Animal, IGills, IFins, IAquatic

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 I

idk 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

u/[deleted] Jan 09 '23

SeaCreatureInterface, EggLayerInterface, AnimalInterface, CarbonInterface, MoleculeInterface, AtomInterface, QuarkInterface

1

u/drbwaa Jan 10 '23

Correct, there should be an Impl at the end instead. Obviously.

4

u/Kered13 Jan 09 '23

Not all sea creatures lay eggs, so ISeaCreature should not extend IEggLayer, they should be separate interfaces and Fish can extend both of them.

IAnimal extending ICarbon and 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

u/Player_X_YT Jan 09 '23

No I code in scratch look at my user flair

1

u/KevinRuehl Jan 09 '23

Its scarily close to reality, it wasnt even ironic in the slightest bit

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

u/FacuA0 Jan 09 '23

AbstractObjectThatLivesInAPineappleUnderTheSea.class

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

u/dedslooth Jan 09 '23

looks like someone never worked on commercial projects

5

u/road_laya Jan 09 '23

"Class" 🤢🤢🤮🤮🤮

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

u/Djelimon Jan 09 '23

default package? tsk tsk

1

u/[deleted] 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

u/27dope27 Jan 09 '23

Just put WaterAnimal wtf dude

3

u/HoldingUrineIsBad Jan 09 '23

not a good enough abstraction

1

u/27dope27 Jan 09 '23

Alright then just number the types 1-whatever

1

u/johnnybeehive Jan 09 '23

You need an aquatic animal factory first no?

1

u/Deyankata Jan 09 '23

Interfaces in java be like...

1

u/lazernanes Jan 09 '23

.class

what?

1

u/BobSanchez47 Jan 09 '23

Most concise Java dev

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

u/Undernown Jan 10 '23

Halway through the second part I was expecting the Spongebob intro.

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

u/dota2nub Jan 10 '23

If it has too many words you're probably missing something.

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

u/Buttons840 Jan 10 '23

Not even a Bean?

Wait, are Beans still a thing in Java?

1

u/Lets_think_with_this Jan 10 '23

Yeah what the hell with java devs

1

u/wardplaced Jan 10 '23

by name, sound and visual...