this post was submitted on 13 Nov 2024
808 points (96.1% liked)

Greentext

4388 readers
1084 users here now

This is a place to share greentexts and witness the confounding life of Anon. If you're new to the Greentext community, think of it as a sort of zoo with Anon as the main attraction.

Be warned:

If you find yourself getting angry (or god forbid, agreeing) with something Anon has said, you might be doing it wrong.

founded 1 year ago
MODERATORS
 
(page 3) 50 comments
sorted by: hot top controversial new old
[–] WormFood@lemmy.world 17 points 1 day ago (4 children)

object orientated programming is the wrong idiom for almost all problems, and even in the few cases where it makes sense, you have to be very careful or it'll hurt you

[–] sum_yung_gai@lemm.ee 8 points 1 day ago (2 children)

I have been trying to be more functional but I still use classes for things like loading/modeling configs. What are some common situations where using an object is a good solution?

I use python if that helps at all.

[–] pivot_root@lemmy.world 11 points 1 day ago* (last edited 1 day ago) (2 children)

What are some common situations where using an object is a good solution?

It depends on what you mean by "object"

  • Some kind of structured data?
  • Some named type which fulfills an interface?

When you have some kind of structured data, having a class to represent it is fine. If you're able to give it type annotations, that's much better than passing around random dictionaries.

When you need polymorphism and have an interface where some method on an object needs to exist (e.g. car.honk()), that's also fine as long as you avoid creating subclasses and using inheritance. If you need some car that can honk like a truck and drive like a racecar, use composition.

What I would consider a good use of classes (more specifically, nominal types) is dependent types. The idea is that you use the type system to enforce invariants for data.

For example, suppose you have a string for a user email. It might be a valid email string, or it might be garbage like "z#%@("=))??". You have a function for updating the user email in a database, and it requires the email string to be valid.

One approach is to validate the email string after receiving it from the user. That works, but what if your coworker creates a new form and forgets to validate the email string there? Bad data gets passed downstream to functions that expect well-formed data.

Another approach is to validate the email string at the top of every function that expects well-formed data. That also works, but now you're validating the same string multiple times and pasting validate_email(email) everywhere.

With a dependent type, you have a ValidatedEmail type and a constructor for it. The constructor will return an instance of the ValidatedEmail if and only if the email string is valid. Any function that expects a valid email will only accept a ValidatedEmail, and not a string. If your coworker creates a new form and forgets to validate the email, the type system will complain about a string being passed instead of a ValidatedEmail. You also shift the responsibility of validating the email to wherever there is a boundary between validated and unvalidated data, avoiding unnecessary validation since you know a ValidatedEmail is already valid.

It's an extremely useful paradigm for avoiding logic errors, but it's unfortunately not as common as it should be.

load more comments (2 replies)
load more comments (1 replies)
load more comments (3 replies)
[–] Ab_intra@lemmy.world 29 points 2 days ago (1 children)

Just imagine how it must have been to code Minecraft 🤣

[–] joyjoy@lemm.ee 33 points 2 days ago* (last edited 2 days ago) (2 children)

They only had to deal with LWJGL. The corporate java world has to use Spring.

Edit: They also had to deal with all the fans saying they should've written it in C#.

[–] dev_null@lemmy.ml 6 points 1 day ago (2 children)

And much of the confusion and frustration at "Java" is actually because of Spring, or the "enterprise" nonsense making everything unnecessarily complex. You can just... write Java without any of that.

You shouldn't though, because Kotlin exists, which fixes everything that's wrong with Java while still being 100% compatible, so even in legacy projects you can mix and match and write new code in Kotlin without needing to rewrite any of the existing Java.

load more comments (2 replies)
load more comments (1 replies)
[–] lennivelkant@discuss.tchncs.de 4 points 1 day ago (14 children)

Aside from the general stupidity, Java is a heavily front-loaded language in my experience. I'm not going to engage in any tribalism about it or claim that it's better or worse than others. As a matter of personal taste, I have come to like it, but I had to learn a lot until I reached a level of proficiency where I started considering it usable.

Likewise, there is a level of preparation on the target machines: "Platform-independent" just means you don't have to compile the program itself for different platforms and architectures like you would with C and its kin, as long as the target machines have an appropriate runtime installed.

Libraries and library management is a whole thing in every general-purpose language I've dealt with so far. DSLs get away with including everything domain-specific, but non-specific languages can't possibly cover everything. Again, Java has a steep learning curve for things like Maven - I find it to be powerful for the things I've used it in, but it's a lot to wrap your head around.

It definitely isn't beginner-friendly and I still think my university was wrong to start right into it with the first programming classes. Part of it was the teacher (Technically excellent, didactically atrocious), but it also wasn't a great entry point into programming in general.

load more comments (14 replies)
[–] sentient_loom@sh.itjust.works 15 points 2 days ago

You're not stuck with it Anon. You can use something different!

load more comments
view more: ‹ prev next ›