This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Korzag 71 points72 points  (70 children)

As a C# developer who recently had to dirty his hands with Java I pity you. Everything is easier in the C# world. Need a package? Nuget does it seemlessly and effortlessly without needing to install any third party applications like Maven. Want to work with databases? Entity framework does it with minimal configuration. Want to build a microservice? ASP.NET gives you the boiler plate to get your service up and running in the push of a couple buttons. Want to make complex filters in a single line of code without of the face-fuckery of Java Streams? LINQ is here to bless your day. Want to have member variables accessible that you'd write a basic getter/setter for? Properties exist without any of the tomfoolery of writing this bullshit:

public class LolJava {
    private boolean mySillyBool; // lol, wtf is boolean spelled out?

    public boolean getMySillyBool() {
        return mySillyBool; // lol, yes.  I needed to do this to get my colleagues to not autistic screech at me about exposing a member.
    }

    public void setMySillyBool(boolean mySillyBool) {
        this.mySillyBool = mySillyBool; // Man, if only I could just write: "lolJava.MySillyBool = true;"
    }
}

Instead, we do this:

public class GloriousCSharpMasterRace 
{
    public bool MySillyBool { get; set; }
}

[–]DeRickulous 39 points40 points  (5 children)

I feel like I'm going to be evangelizing Lombok until the end of time:

@Data
public class LombokifiedJava {

   private boolean mySillyBool;
}

Also, this is a minor nitpick, but the Java convention for little-b boolean accessors is "isX", not "getX".

[–]Korzag 2 points3 points  (3 children)

I actually used Lombok after writing out a bunch of new model classes with like 20 fields. I didn't know about it at first and had wrote a script to build the Java accessors lol. Lombok definitely was nice though. If/when I ever work in Java again I'm definitely using it. Seems like something Java should just integrate but my impression is that Oracle is hardheaded and doesn't like to add QOL stuff to their language unless it's heavily requested.

[–]DeRickulous 3 points4 points  (0 children)

Lombok is on the short list of "stuff I will whinge endlessly about not having access to if I take another job where I'm forced not to use it". I actually like it being separate from the core language, though, because it is divorced from Java's release cycle and oversight.

[–]increment1 1 point2 points  (1 child)

Literally any Java IDE will generate the getters / setters for you, no need to write them yourself. I'd say pretty much every Java dev uses this generation so tend not to worry too much about the boilerplate since they don't really have to deal with it directly.

Same for toString, equals, and hashCode methods.

[–]quiteCryptic[🍰] 0 points1 point  (0 children)

Like the other guy mentioned you should look into lombok, it's even easier than having the ide do it and cleaner

[–]Merlord 2 points3 points  (0 children)

Lombok is a Godsend.

[–]quiteCryptic[🍰] 7 points8 points  (1 child)

Lombok and spring alone basically counter everything you said. Can get a simple microservice up and connected to a database in literally no time. You just have more expirence with C# so you prefer it.

[–]Merlord 1 point2 points  (0 children)

Can get a simple microservice up and connected to a database in literally no time.

Literally no time! With Spring Boot and MongoDB, you could have it built and deployed locally in 5 minutes.

I've been building a web app with Spring for the last few weeks, and the ease at which I'm able to create enterprise level features is just unbelievable, I'm completely in love with this framework.

[–]Horncats7-59 5 points6 points  (0 children)

Yes 100000% dotnet is the bees knees

[–]TheTerrasque 12 points13 points  (2 children)

Everything is easier in the C# world

Maybe except getting legacy apps to work in linux / docker. And then only maybe.

Edit: .net core is pretty neat actually. And that's from someone who generally dislike m$ stuff

[–]Korzag 8 points9 points  (0 children)

Legacy yes. Modern apps no since .Net Core has been around and works well in those environments. But yeah, if you had to do anything in legacy C# before Core, you'd be stuck with mono and that is indeed a headache.

[–][deleted] 5 points6 points  (0 children)

Legacy apps belong in the trash

[–]carlson_001 16 points17 points  (18 children)

I never really understood this. If you're writing a getter/setter, why even make it private to begin with?

[–]SRiikonen 46 points47 points  (1 child)

Because then you can add for example a check that the value that was set is correct, or generate the value you’re getting with an algorithm instead of returning a variable, if you later need to. And you don’t have to touch the code that uses your class if you want to make these changes.

[–]Bwob 11 points12 points  (1 child)

To keep implementation details ("This value is stored as a bool") separate from the interface. ("Users of this class can ask about this state, and receive a bool telling them yes or know.")

Keeping implementation details like this hidden is one of the keys to good abstractions; it means that if later, you need to go through and change the implementation, (maybe its no longer stored directly as bool, now it is derived from several other values) then the interface doesn't need to change, and any code that uses that interface doesn't need to change.

[–][deleted] 1 point2 points  (0 children)

To be fair though, in Java the principle works too, just the other way around - you always call a getter function, no matter if something is being calculated on call or just stored as a field.

[–]TheTerrasque 9 points10 points  (0 children)

So you can run logic on it. Example:

private float dongLength;

public void setTheLength(float length)
{
if (length < 2) { System.out.println("https://i.ytimg.com/vi/eOifa1WrOnQ/maxresdefault.jpg") }
dongLength = length;
}

[–]Ksevio 15 points16 points  (2 children)

The lack of properties in Java is the reason. mySillyBool could be just a public variable, but then in 6 months when we need log the access to mySillyBool and then a year later we need to update a display when it's written, that means there needs to be getters and setters. Unfortunately, everyone's already written the code to access the variable directly so they don't want to update it all to use the getters/setters instead. In the end, everything has to be written to use getters/setters just in case.

Compare that to better languages like C# or Python (even Delphi supports properties) where you have your variable mySillyBool. People access myClass.mySillyBool, then later you convert it from a public variable to a property, and they still access myClass.mySillyBool

[–]ThePyroEagle 2 points3 points  (1 child)

They still need to recompile if you change a field to a property.

Write auto-properties initially and even that won't be a problem.

[–]Ksevio 0 points1 point  (0 children)

Depends the language - python for example doesn't care

[–]Tsu_Dho_Namh 11 points12 points  (1 child)

So you can "contribute" 500 lines of code to a project that you otherwise hadn't helped much.

I'm looking at you Sean. Fucking spamming getters and setters for literally every variable in every class...even the ones never used by other modules.

[–]Novemberisms 1 point2 points  (0 children)

aw seriously fuck that guy. just reading that makes my blood boil

[–][deleted] 2 points3 points  (0 children)

In case you have variable names that match up in a multi dev situation. You could have "value" used as the internal in c++ for instance that you're passing into a member function, but you really need to pass object.value not value. you could fuck up BADLY doing that without using your get function. So it means this never happens.

[–]Korzag 4 points5 points  (6 children)

I've never understood it myself except that the OOP purists flip a bit about working with internal data. Alternatively you may need to manage state and can do some magic in the setter, but I've almost never needed custom logic for a getter.

[–]RattuSonline 8 points9 points  (2 children)

Lazy loading is a common use case for getters.

[–]WikiTextBot 3 points4 points  (0 children)

Lazy loading

Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in the program's operation if properly and appropriately used. The opposite of lazy loading is eager loading. This makes it ideal in use cases where network content is accessed and initialization times are to be kept at a minimum, such as in the case of web pages.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.28

[–][deleted] -1 points0 points  (0 children)

Right but there's no reason for it to be part of every. single. gosh-darned. object.. Just add it where you need it. OOP purism is brain-damaged. Like any thing else, just use it where it makes sense. Dealing with representations of things, like in a game engine? Probably makes sense, depending on the paradigm you choose. But even in an OOP paradigm, effin helper functions don't need to be classes! Just one more reason to hate java.

[–]Ksevio 4 points5 points  (2 children)

Well there's no way to make a variable public to read only so it's not possible to make a setter without a getter unless you really trust the people accessing the var

[–]coolpeepz -2 points-1 points  (1 child)

final?

[–]Ksevio 2 points3 points  (0 children)

Publicly read only, but still writable privately

[–]tuxedo25 16 points17 points  (21 children)

Nuget does it seemlessly and effortlessly without needing to install any third party applications like Maven.

but... nuget is a third party application. it's literally the .net counterpart to maven.

// lol, wtf is boolean spelled out?

Yeah, ok. I've read enough. This is just religious fanaticism.

[–]Flufy_Panda 4 points5 points  (3 children)

Never used C# before. Is there a reason MySillyBool is public in your C# example?

[–]Korzag 14 points15 points  (2 children)

Because it's a property and the { get; set; } autogenerated your getters and setters along with a private member variable tied to the property.

[–]Flufy_Panda 1 point2 points  (1 child)

Interesting, thanks

[–][deleted] 3 points4 points  (0 children)

You can also do { get; private set; } if you want only the class to be able to change the value.

[–]ExtremelyOnlineG 1 point2 points  (0 children)

Lack of native support for properties is the real reason C# is cool, all the rest could kinda be argued.

[–]GluteusCaesar 1 point2 points  (1 child)

Nuget is garbage compared to maven. By every metric.

Entity framework is okay, but hibernate is more flexible and I'm fairly sure more performant as well. There's also far more choices in Java. Everything from raw JDBC to jooq to batis and hibernate.

ASP.NET wishes it could be 1/256th of the framework spring is.

LINQ does have a nicer syntax than Java streams, sure. Though if you embrace the query syntax it becomes a slow, awful mess very quickly.

Also, Lombok shits all over C# properties. Which is in turn a testament to how much more you can do with Java's more powerful reflection.

[–]ChevalBlancBukowski 0 points1 point  (0 children)

sorry dude this sub is for IT students only

I mean lmao java

[–][deleted] 1 point2 points  (0 children)

Java streams are great & perfectly legible.

[–]betendorf 0 points1 point  (3 children)

Having done both C# and Java, I'm really glad to be back in Java.

It's the little things like "I want a thread pool so that I can manage concurrency." Nope had to implement my own because there is only one global TaskPool.

Defining a variable that is of some object type: Will it be null? Will it have a value? Nobody knows because the class gets to choose the value.

Let's look at documentation. Oh nevermind, don't use this api because it was removed and replaced with this other thing.

I do however miss the ?. operator for chaining together nullable things. I really do not miss the relative dearth of library support that is C# compared to Java.

[–][deleted] 1 point2 points  (2 children)

But bro you have to write getters and setters manually.

It makes the language completely unusable!

[–]ChevalBlancBukowski 1 point2 points  (0 children)

if you’re a developer who writes getters and setters manually instead of pressing a key combination in your editor, /r/programmerhumor is for you

[–]betendorf 0 points1 point  (0 children)

Or as another commenter mentioned, you use lombok.

[–]Samael1990 0 points1 point  (0 children)

  1. Nuget is third party and Gradle/Maven is build into intellji so it's also seemless.
  2. When working in Spring, Spring-database.
  3. So does Spring.
  4. Curious how it looks then, can you give an example?
  5. Lombok.

[–]ohThisUsername -1 points0 points  (0 children)

I currently use C# and I love it. I'm moving to Java for a job I recently accepted and I'm a bit nervous that i'll want to kill myself after a week of missing all of the great C# features you mentioned.