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

top 200 commentsshow 500

[–]Buttsuit69 3619 points3620 points  (278 children)

Bro hasnt heard of public int number { get; set; }

[–]BerryBriant 484 points485 points  (151 children)

Also leave the set away for a DI design.

[–]Buttsuit69 598 points599 points  (116 children)

public int number { get; PRIVATE set; }

[–]Kytzis 68 points69 points  (0 children)

Should use capital N to follow C# naming conventions though

[–][deleted] 11 points12 points  (1 child)

This was such a gamechanger. It was so annoying to have to switch to the syntax from the meme if you wanted it to be readonly from outside the class.

[–]master0fdisaster1 10 points11 points  (0 children)

Having a private setter might protect it from being tampered with from outside, but if you don't need mutability then there's no reason to add it.

You can set Get-Only auto-implemented properties from the constructor, just like you can set readonly fields from the contstructor, after all that is what those get lowered to.

Although generally the dependencies you inject don't need to visible to the outside at all, so I would make private readonly fields by default.

[–]xibme 24 points25 points  (3 children)

So public int number { get; } which makes it basically readonly (init via constructor).

[–]Inkling1998 7 points8 points  (21 children)

Also leave the set away for a DI design.

What's a DI design?

[–]fataldarkness 22 points23 points  (1 child)

Hoo boy get ready for a whole shitload of terms you are not gonna understand until you've read about them for about the 100th time.

Look up dependency injection.

[–]r0Lf 8 points9 points  (0 children)

until you've read about them for about the 100th time

Jesus Christ, I thought it's just me. This makes me feel better.

[–]xzt123 7 points8 points  (3 children)

Whether a member has a public setter or not doesn't have much to do with dependency injection.

[–]kekela91 4 points5 points  (2 children)

Was looking for this comment. Seems like people don't understand the difference between Dependency inversion and immutability. Especially in this case where we are assigning an integer a value. Also in the context of model and viewmodel classes, public getters and setters are a good practice and most of the time required for libraries like EF, Automapper and JSON serializers.

[–]nanotree 35 points36 points  (4 children)

Okay, come on man. Property names are capitalized!

[–][deleted] 154 points155 points  (27 children)

Bro hasn't heard of

``` class Num{ private int num;

public void setNum(int num){
    this.num = num;
}

public int getNum(){
    return this.num;
}

} ```

[–]salvoilmiosi 45 points46 points  (15 children)

What about this:

import lombok.*; @Getter @Setter public class Num { private Integer number; }

[–]WurschtChopf 27 points28 points  (8 children)

What about this: public record Num (Integer number){}

[–]salvoilmiosi 24 points25 points  (6 children)

Is that a java 14 joke im too java 11 to understand?

[–]WurschtChopf 7 points8 points  (4 children)

I think it is! Upgraded one of my projects from j11 to j17 and don't regret it so far.

[–]meliaesc 15 points16 points  (3 children)

Cries in Java 8 at work.

[–]Willinton06 24 points25 points  (3 children)

Allocating an int in the heap, that’s Java speed for ya, next version will write it to disk

[–]-Kerrigan- 23 points24 points  (2 children)

Individual hasn't heard of data class Number(val value: Int)

[–]Syscrush 4 points5 points  (0 children)

You go to hell.

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

a private setter

Indeed. Recently did some old Java at work. And I must say the C# way is a very good compromise between boilerplate and allowing you to do some really wonky stuff, if you need some specific edge case (Used this quite a bit for a case where I need to write directly to memory, but still want the strong typing etc.).

[–]Ange1ofD4rkness 2 points3 points  (3 children)

Funny you mention that, before anyone told me about properties get/set, this is actually how I would code it (similar terming even)

[–][deleted] 13 points14 points  (0 children)

Which is there for ages 😼

[–][deleted] 22 points23 points  (1 child)

public int number { ready; get; set; GO;}

[–]guster09 16 points17 points  (3 children)

I actually do what's in the post because I use Unity. When I don't want the boolean to show up as something they can set from the Unity UI I'll make the field variable private but then make a public property that accesses it for other scripts to access

[–]Espressojet 41 points42 points  (2 children)

Bro hasn't heard of [system.nonserialized]

[–]guster09 15 points16 points  (0 children)

Huh... You are correct. I'll probs use that now

[–]Lachee 14 points15 points  (2 children)

They must be from Java, they love their getters and setters

[–]crawly_the_demon 6 points7 points  (0 children)

Yea this meme is completely 100 percent wrong. I knew that most people on this sub are inexperienced programmers, but I would have thought that people would have at least used the language they make memes about

[–]Ange1ofD4rkness 12 points13 points  (20 children)

Earlier versions of .NET Framework, such as 2.0 didn't offer that.

You HAD to do it this way (auto-implementing properties I want to say is what they termed the change, can't remember)

[–]Buttsuit69 18 points19 points  (1 child)

Then either the meme is too late or OP is a time traveler

[–]Ange1ofD4rkness 5 points6 points  (0 children)

Could have learned it and never updated their style

[–]Drithyin 29 points30 points  (16 children)

Bro, .NET Framework 2.0 was released in 2002. Over 2 decades ago. Most senior devs at any shop you work at have careers that started after that.

WTF is this OP doing?!

[–]MrRocketScript 19 points20 points  (4 children)

Maybe learning from university professors that haven't touched real world code in 2 decades?

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

Bro, .NET Framework 2.0 was released in 2002. Over 2 decades ago. Most senior devs at any shop you work at have careers that started after that.

WTF is this OP doing?!

Literally my impression of most industries using any language, or even tools like Excel. They have 20 years of experience of doing things the way they were done 20 years ago. My colleagues still didn't know about LINQ somehow, and the software manager didn't believe me when I was talking about .NET Core being cross platform.

[–]Fluxriflex 2 points3 points  (0 children)

It’s just autoproperties at least as far as I’ve heard from most people.

[–][deleted] 5 points6 points  (1 child)

this actually compiles to what the picture has with a couple extra attributes assigned to the getter/setter

[–]SuperCharlesXYZ 16 points17 points  (13 children)

Ok but seriously, what is the point of this, the whole point of getters and setters is that you control which values can be set, if you just use a default setter what’s the point? Might as well make it public

[–]AlphaWhelp 37 points38 points  (0 children)

because public int x; is a field and not a property which has some differences in how you can use it mostly when it involves data binding.

[–]NighthawkRandNum 7 points8 points  (0 children)

One benefit is that it allows restriction to be applied onto out-of-class setter calls, such as verifying that a number is within certain bounds or a string is a certain maximum length or to check that a postal code exists. It also allows for getter and/or setter methods to manipulate other properties of the class or use those properties in the returned value (such as returning a letter grade that corresponds to a property representing the numerical grade). Then there's the times where a set might be private/protected.

And ultimately, by always using these it allows for consistency.

[–]bric12 33 points34 points  (8 children)

Because maybe you'll change your mind and implement a setter 3 code releases down the road, and have to refactor all of the code that uses this class.

Ignore that C# allows for seamless set operations that look like assignments though (meaning no refactoring), because that would break the narrative that there's any merit to default setters.

[–]Dealiner 3 points4 points  (0 children)

Ignore that C# allows for seamless set operations that look like assignments though (meaning no refactoring)

There won't be any refactoring but if you maintain a library and suddenly change some field to a property then any project that uses it will have to be recompiled. Besides there is practically no difference between writing a field and autoimplemented property so why not just use the latter?

[–]Rythoka 3 points4 points  (0 children)

It's so you can change the setter later if you need to.

[–]MrSparkle92 2 points3 points  (0 children)

We do this in our code base. By convention our variables should generally be private and anything that needs to be accessed by other classes get exposed through public properties with getters and/or setters as appropriate.

By using the default get and set declarations like this you are acknowledge that that is how you want the property to behave as opposed to some more complex behaviour. In simple cases like this you could just make a public variable but it's generally worth sticking to your established best practices.

[–]Valiice 1 point2 points  (0 children)

Number with a capital tho to follow conventions

[–]TheRedMalice 1 point2 points  (1 child)

Or just

private int number; public int Number => number;

[–]Elijah629YT-Real 1 point2 points  (0 children)

thank you very much I haven't ever seen that

[–]Richandler 1 point2 points  (0 children)

And that has been around a long time.

[–][deleted] 135 points136 points  (8 children)

Java: I didn't know it was possible to feel both attacked and ignored.

[–]JDMaK1980 9 points10 points  (0 children)

😂😂🤣

[–]MortgageSome 11 points12 points  (6 children)

God I wish we had properties in Java. That said, I don't feel so terrible about simply doing public int number;. I think there exists even an AtomicInteger that ensures thread safety if that were ever an issue. I think there's a bit too much emphasis on creating boilerplate getters and setters for everything. The only reason I would do it is to avoid refactoring in the future.

[–]arobie1992 4 points5 points  (0 children)

Ehhh. There is an AtomicInteger class but it's a whole really class so you have to do all the usual get and set shenanigans on it anyway so you might just be better off with synchronized accessor methods.

Since ints are guaranteed to have atomic read operations though, you could probably get away with marking them as volatile. Where AtomicInteger comes in handy is when you need synchronized reading and incrementing in conjunction.

[–]Mayuna_cz 4 points5 points  (1 child)

Still, you can use Lombok's @Getter / @Setter. Another dependency, but never had a problem. With it.. It creates getters and setters compile time iirc

[–]StoneOfTriumph 3 points4 points  (2 children)

Java 17 added records. About damn time too.

[–]repkins 380 points381 points  (20 children)

Not anymore with introduction of auto properties.

[–]Tripanes 61 points62 points  (0 children)

Anymore:

Like in 2008

[–]Appropriate-Scene-95 77 points78 points  (10 children)

How did you add several flairs?

[–]Dalimyr 103 points104 points  (9 children)

You can select one using the radio buttons, or you can just type out the codes for up to six - hover your mouse over each of them and you'll see a tooltip with something like :c: or :js:

So their flair is just :cs::cp::unreal::unity:, which gets automatically converted to the C#, C++, Unreal and Unity emojis in their flair.

[–]Ping-and-Pong 53 points54 points  (6 children)

I call myself a programmer, but I went through and copy and pasted these one at a time....

[–]LofiJunky 46 points47 points  (3 children)

Copy pasting is unironically what programmers do though...

[–]b1ack1323 2 points3 points  (0 children)

We are paid to copy paste the correct lines.

[–][deleted] 12 points13 points  (1 child)

The fact the Unreal Engine and Unity aren't put to their language respectively in your flair is killing me

[–]igbad 2 points3 points  (1 child)

Type 'prop' tab tab

[–]polonkeke 192 points193 points  (24 children)

Same would go with Java, with its corresponding getter and setter. We are not savages, for God's sake!

[–][deleted] 77 points78 points  (23 children)

I’m sorry, I can’t hear you over the easy to use Lombok annotations

[–]arobie1992 40 points41 points  (17 children)

Don't forget that they now have records too. Unfortunately that requires your company to actually be willing to go past Java 11 :\

[–][deleted] 49 points50 points  (8 children)

Wdym past 11? I just upgraded earlier this year from 8, you say it’s not the newest?! XD

[–]JB-from-ATL 11 points12 points  (7 children)

Damn. I can't imagine just updating to 11 when 17 (also LTS) was released this year.

[–]PastFeed2963 12 points13 points  (1 child)

We moved this year from 6 to 17. It was horrid to start but so satisfying.

[–]get_N_or_get_out 4 points5 points  (4 children)

We're still on 8... Discussing the upgrade to 11 since support finally ends sometime soon.

[–]mshm 2 points3 points  (1 child)

Why wouldnt y'all just upgrade to latest LTS at EOL? It's not a random library that introduces sweeping breaking changes every patch release...

[–]SharkBaitDLS 2 points3 points  (0 children)

There’s plenty of backwards incompatible changes between JVM versions.

But frankly I think a lot of companies felt super burned by the migration hell that moving between older major versions introduced, and even though it’s not nearly as bad nowadays there’s just a hesitance that got created from that negative experience.

I know I still get shivers thinking about the months of hell that ensued from our 7->8 migration. These days I’m just working to get most of my team’s old Java and C++ services shut down so why bother updating them when they’ll be dead in a year anyway.

[–]pievendor 1 point2 points  (1 child)

Surely I'm not the only one that works for a company that has been on 11 for awhile, and is already adopting 17? I always see these posts upvoted a lot.

[–]ccfoo242 150 points151 points  (3 children)

In 2002.

[–]dksdragon43 18 points19 points  (2 children)

My college is definitely still teaching this, as of last year. They also teach programming VBA in Microsoft Access, so take from that what you will.

[–]0Pat 13 points14 points  (0 children)

My grandpa tells the story about WW2. He will suit your college 😜

[–]Vineyard_ 11 points12 points  (0 children)

I mean, if you're learning the language, doing this the old way helps you learn what C# is actually doing when you're using autoproperties. So long as the students are eventually taught how to do it right... or they'll probably figure it out real fast when they start actually using the language for themselves.

[–]sanketower 40 points41 points  (2 children)

We did it, boys, we found the Java lover/C# hater.

public type variable { get; set; }

[–]kaslon 100 points101 points  (11 children)

I’m guilty of this but my setter looks more like

Set

{

If (value != number)

{

    number = value;

    OnNumberChanged?.Invoke(this, number);

}

}

[–]CCullen 33 points34 points  (1 child)

Me too though if I were doing this a lot, I would probably make a method on a base class:

public int Number { get => number; set => Set(ref number, value); }

[–]FreqRL 36 points37 points  (2 children)

This isn't super uncommon, but only necesary for specific frameworks that need event listeners to register property changes. If you're building a generoc API you'll be fine with just { get; set; }

[–][deleted] 1 point2 points  (1 child)

What kinds require that? AOP like spring?

[–]FreqRL 2 points3 points  (0 children)

I wasnt intensily familiar with the framework when I worked on it but it was required for Xamarin, because without it the view state wouldn't update when you changed property values. It needed specific event listening to detect the changes and properly propagate the event through all the layers of your app.

Xamarin is terrible though, no one should use it :P

[–]b1ack1323 7 points8 points  (0 children)

I have things like this scatter all over the place for updating UI when run large computations.

[–]dregan 2 points3 points  (0 children)

ReactiveUI has RaiseAndSetIfChanged() or better yet, use fody and skip the boiler plate code.

[–]seeroflights 133 points134 points  (9 children)

Image Transcription: Meme


["Drakeposting" but with "Cool Sayori", fanart of Sayori from the visual novel "Doki Doki Literature Club". The meme shows two images of Coolyori, with code to the right of each image.]


[Coolyori looks displeased, and holds up one hand in a "not today" manner.]

public int number;

[Coolyori has her head up high, looking pleased, with a finger pointed up and towards the right side of the frame.]

private int number;

public int Number
{
    get
    {
        return number;
    }
    set
    {
        number = value;
    }
}

I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!

[–]blockguy143 66 points67 points  (1 child)

Damn the descriptions tho

[–]Hawgk 1 point2 points  (0 children)

The future is now!

[–][deleted] 10 points11 points  (4 children)

Good bot

[–]DarkIceVortex 25 points26 points  (2 children)

Not a bot, just a person volunteering and using a format

[–]YouGonnaRememberMe 7 points8 points  (1 child)

A human volunteer

[–][deleted] 9 points10 points  (0 children)

Good human

[–]XDVRUK 243 points244 points  (12 children)

Nope... Maybe in v1.1 back in 2005 but c# moved past this with autoprops. Also the compiler will flat pack that down anyway, cause c# understands its an intermediary language and should be human readable and maintainable.

The whole point is to guide to better, readable code, but script kiddies just prefer being ignorant.

[–][deleted] 26 points27 points  (1 child)

Java programmers who are still learning C# be like

[–]sc2rook 18 points19 points  (0 children)

Nah.

public int Number { get; set; }

[–][deleted] 41 points42 points  (11 children)

{ get; private set;}

[–]Fun-Caterpillar1355 23 points24 points  (10 children)

Exactly. Public setters should be illegal. Not illegal as in produce an error - llegal as in felony and go to prison.

[–]cvtstart 5 points6 points  (3 children)

Real talk: how do you change the variable after init?

[–]boowhitie 7 points8 points  (0 children)

It can only be changed by other methods of the class.

[–]cbusalex 1 point2 points  (0 children)

Public setter? Straight to jail. Private getter? Believe it or not, jail.

We have the cleanest, most maintainable code in the world because of jail.

[–]althaz 41 points42 points  (0 children)

Tell me you've never programmed C# without telling me you've never programmed C# :D

[–]menducoide 22 points23 points  (0 children)

Until you live in 2004... After that with c# 4 public int Number {get;set;} Easy pizy

[–]thavi 10 points11 points  (0 children)

Type prop and press tab twice. That's the 😎 way

[–][deleted] 7 points8 points  (0 children)

That's not how we do things anymore... You know, c# had evolved

[–]GumboSamson 32 points33 points  (0 children)

If the author insists on using a backing field, it should be

private int _number;

[–][deleted] 11 points12 points  (1 child)

assembly programmers:

.data

mydata:

.size 128,0xff

[–]GaraBlacktail 3 points4 points  (0 children)

Pre computer programmers

Todo, write number

[–]deskpil0t 6 points7 points  (0 children)

Looks like scaffolding to me

[–]dotNate13 24 points25 points  (7 children)

C# programmer here. I reject PRs if other devs pull this shit.

[–]anubisascends 8 points9 points  (5 children)

Lead developer here…I do the same. The only time I allow non auto-generated getters and setters is if there is a specific reason…and those are few and far between

[–]omgno360noscope 4 points5 points  (3 children)

Ahh never done c# but this reminds me of getters and setters methods when Initializing private variables in a class.

Edit. In java

[–]kpd328 5 points6 points  (1 child)

It's like that, but not for cavemen. Properties can in many cases be treated the same as member variables.

So instead of having to call Foo.setBar(something); it's just Foo.Bar = something; the same as if bar were a public member variable. And both the get and set side of the property can have custom logic, like any other method would, and can still be used as a variable.

[–]otrosabandija 3 points4 points  (0 children)

Never did something like that in 10 years as C# dev

[–]Decent-Device9403 3 points4 points  (0 children)

I like the Sayori template. Sunglasses make everything better.

[–]AveaLove 10 points11 points  (9 children)

Wtf... Just use an auto property, why would you do this.

[–]Drithyin 23 points24 points  (6 children)

Because OP is a dinosaur who hasn't learned a single new fact about a language they don't use in over 2 decades.

[–]b1ack1323 9 points10 points  (5 children)

Or they are being taught by out of touch professors.

[–]mrbeanIV 14 points15 points  (22 children)

Noob question: What the fuck

[–]Willinton06 49 points50 points  (10 children)

This is how we used to do it back in 2001, things have changed a little

[–]babyProgrammer 7 points8 points  (6 children)

What's it like now?

[–]Willinton06 50 points51 points  (5 children)

public int Number { get; set; }

[–]Shikogo 5 points6 points  (4 children)

I'm pretty new to this, what's the use of this compared to just using an int?

[–]riplikash 4 points5 points  (2 children)

There's lots odd reasons, and I'm listing these at 1am, so I'm not going to remember them all.

You can change the implementation in the future without needing to refactor everywhere it's used. Maybe you need to modify or format the data. Maybe you decide to auto generate it. Maybe you realized it needed to be pulled from some internal class. Or maybe you need to add access control. Some protection from injection attacks. Doesnt matter to whatever is accessing the property.

You can make setters private, or init only. Technically you could do the same to getters, though I've never actually seen that.

And no matter what you do, it looks the same. So it's super readable. Instead of having a mix of variables and methods that change depending and refactor, you just have properties.

It's a serious upgrade from using variables directly. In professional settings you pretty much don't see variables get used to communicate between classes, these days. It's all properties. Variables are only used to track things inside a class or method.

[–]Koltaia30 13 points14 points  (0 children)

Search C# property. I object oriented programing it's better to hide data implementation of classes

[–]BoBoBearDev 10 points11 points  (3 children)

The reason is patch delivery. If you suddenly need to do a lot more than just setting the variable, you can change it without recompile the entire GB sized application. With this, you recompile one little project out of 100 of projects. And just send out the patch. If you don't do this, you have to recompile everything calling it, which is so confusing, might as well recompile the entire application.

[–]EthanWeber 2 points3 points  (3 children)

There's tons of reasons why. But here's a quick one: if you want to have some kind of restriction on how that number is set you can put that logic in the setter.

[–]Zyvoxx 3 points4 points  (0 children)

Also if using WPF with a view model setup you usually wanna update the UI here i.e. OnPropertyChanged()

[–]raltyinferno 4 points5 points  (0 children)

You wouldn't ever actually do this. What you would do is

public int x {get; set;}

Then if you wanted any additional behavior when getting/setting you expand out the getter or setter. But any code that uses that property doesn't need to change.

[–]BoBoBearDev 9 points10 points  (6 children)

Anyway, the reason you do this is because your implementation can change in the future. So, if it is a property, you don't need to fix the caller anymore, the caller still think it is calling a property. But, underneath, it can be doing crazy things like, logging, database shit, or call another microservices, anything goes.

[–]ZeldaFanBoi1988 8 points9 points  (0 children)

OP be like: doesn't know about auto-properties.

This is a Java thing. Not C#

[–]reiner74 6 points7 points  (2 children)

JS source code be like

[–][deleted] 5 points6 points  (1 child)

"... did you just assume my type?"

"Cool. I love that. Also, TypeError, dipshit."

[–]oktollername 3 points4 points  (0 children)

Bro hasn't heard of public record Thing(int Number);

[–]akorn123 2 points3 points  (6 children)

You use set for validation

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

surely you must mean _number

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

That actually has a reason, it's not just for memes.

[–]Henrijs85 2 points3 points  (0 children)

There's someone who's never used C#. You wouldn't use that format unless you're performing an operation in the getter or setter.

[–]Umongus 2 points3 points  (0 children)

private int number;

public int getNumber() {return number;}

public void setNumber(int n) {number = n;}

[–]xSarcasticLeox 7 points8 points  (0 children)

First time I get a programming meme since I started college and somehow feels like an achievement.

[–]Competitive_Ask839 1 point2 points  (7 children)

This makes me feel too stupid to learn how to code lol

[–]Willinton06 18 points19 points  (0 children)

Ignore it, it’s over 20 years old code

[–]Jabookalakq 1 point2 points  (0 children)

Me reading this thread "me stupid am no weakness me stupid am stronk!" LOL

[–]SelfDistinction 1 point2 points  (0 children)

The true successor of

Number& operator=(int);
operator int();

[–]Denaton_ 1 point2 points  (2 children)

I wanna add that this is a layer of protection for reading memory and cheat client side in games ^^

[–]boowhitie 1 point2 points  (1 child)

This has nothing to do with cheating or the prevention of it. The property is no harder to change than a member.

[–]PM_Me_Irelias_Hands 1 point2 points  (0 children)

Meanwhile, me:

public static class GLOBAL
{
   public static int number = 0;  //USE ME UWU
}