|
Nude posted:
Having jumped between Java and C# for a few days in a row, I appreciate how much less typing I have to do with properties. Edit: Also, if you can overload operators, just have the drat thing always return true.
|
# ? Jan 17, 2018 01:42 |
|
|
# ? May 21, 2024 01:39 |
|
Nude posted:
Why? Genuine question. How is code:
code:
code:
|
# ? Jan 17, 2018 02:58 |
Properties also have the advantage of letting you refactor an interface more easily. You can start with a publicly available property and then refactor it to setters/getters as your requirements change while breaking less code.
|
|
# ? Jan 17, 2018 03:03 |
|
Taffer posted:Why? Genuine question. Because accessing a variable and having it potentially do an expensive operation or cache something or increment or whatever it's doing is a scary thing when you have no idea that it might be doing that without checking every variable to see if it has a custom getter. At least functions give you an idea they have the possibility of doing something (and can even be named appropriately).
|
# ? Jan 17, 2018 03:12 |
Jewel posted:Because accessing a variable and having it potentially do an expensive operation or cache something or increment or whatever it's doing is a scary thing when you have no idea that it might be doing that without checking every variable to see if it has a custom getter. At least functions give you an idea they have the possibility of doing something (and can even be named appropriately). Hmm. I'm not sure if I agree, but that's a strong argument.
|
|
# ? Jan 17, 2018 03:20 |
|
It's because it's really weird to have two different syntaxes for accessing a property of an object, just based on whether or not it's a raw field. See also, why c# has indexers instead of making you write a get(int) method. Also, setSomeLongPropertyName(getSomeLongPropertyName() + 1) is horrible garbage compared to just SomeLongPropertyName++ (or += 1).
|
# ? Jan 17, 2018 03:29 |
|
Jewel posted:Because accessing a variable and having it potentially do an expensive operation or cache something or increment or whatever it's doing is a scary thing when you have no idea that it might be doing that without checking every variable to see if it has a custom getter. At least functions give you an idea they have the possibility of doing something (and can even be named appropriately). Except that in Java every public field is supposed to be accessed through getters or setters, so it just amounts to more typing, longer lines, and more boiler plate.
|
# ? Jan 17, 2018 03:54 |
|
C# properties are awesome. Yes, they can be abused by amateur or bad coders but so can everything else.
|
# ? Jan 17, 2018 04:16 |
|
KernelSlanders posted:Except that in Java every public field is supposed to be accessed through getters or setters, so it just amounts to more typing, longer lines, and more boiler plate. It doesn't have to be that way. It's just common convention among Java devs. C++ devs are more likely to expose data members as public as long as changing them won't break an object's state. This has the side benefit of telling you that a setter method is probably doing some other stuff behind the scenes if it was implemented that way. e: Java famously has AWT using public variables when it's not really a good idea to do so, and obviously could never change it after the fact once it got into client code. So there's precedent for encapsulating absolutely everything because you can't exactly go back on your decision to expose too much to the caller. The Phlegmatist fucked around with this message at 05:07 on Jan 17, 2018 |
# ? Jan 17, 2018 05:05 |
|
Jabor posted:It's because it's really weird to have two different syntaxes for accessing a property of an object, just based on whether or not it's a raw field. That depends on the language. Swift and kotlin for example put extended getters and setters on the property itself, so everything is accessed the same way, even if some operation is required. It's cleaner, more concise, and has far less boilerplate. The arguments posted don't really explain why a getter is better to me, but maybe that's because of the basis in languages that don't do property access that way. I'm planning to learn C# soon so I guess I'll learn that. But coming from Java, I really hate enforced getters and setters.
|
# ? Jan 17, 2018 05:27 |
|
Jewel posted:Because accessing a variable and having it potentially do an expensive operation or cache something or increment or whatever it's doing is a scary thing when you have no idea that it might be doing that without checking every variable to see if it has a custom getter. At least functions give you an idea they have the possibility of doing something (and can even be named appropriately). This is my main reason, although I admit in c# it's not like I'm going to go out of my way and not use the convenient option. But I don't think I'm gonna win many people over with csharp getter/setters muddles the difference between a variable and a method. As KernelSanders is right it's based off of Java's standard, so if you go off of that this is a logical step to take. I'm just baffled by how much you can make what looks like a regular variable, do pretty much anything under the hood (including cast it to another type and back and stuff like that). But to C#'s credit I've never seen this out in the wild so far when using other people's libraries, so admittedly it's weak argument. I appreciate your responses either way, was genuinely curious on everyone's thoughts.
|
# ? Jan 17, 2018 05:30 |
|
I like how all the OOP classes were like "encapsulate everything so you can change the underlying code and not break code that uses your class!" when 1) 90% of the member data in classes you're gonna write in life aren't gonna change, 2) 90% of the classes that do change are in your dumb code that you have total refactoring control over, and 3) who gives a poo poo when stuff is trending toward immutability by default now anyway. I wonder how many of our "best" design practices are still rooted in not having an IDE and thinking our code is going to be used by anybody else ever.
|
# ? Jan 17, 2018 06:12 |
|
C# properties are nice for making noun-y things look noun-y. Like if you have an Image class, Height, Width, NumPixels, BitDepth, NumBytes, and RowStride are all reasonable things to expose, and the implementation would probably have some of them be variables and some be calculated. Making them properties makes them all accessed the same and (in non-crap code) is strongly suggesting that accessing them is idempotent and inexpensive.
|
# ? Jan 17, 2018 07:26 |
|
Doom Mathematic posted:Well, I wasn't in the interview, we only have the candidate's paraphrase for it, but "They said nothing is impossible. It happened 2 weeks back" says to me that it happened. One place i was working at , we actually gave candidates real issues to solve as part of their interview, So this may have been to find a candidate who knows how it can happen to make sure it can't again.
|
# ? Jan 17, 2018 10:29 |
|
The possibility of a property doing something unpleasant when accessed seems no worse than the possibility of a getX() method doing something unpleasant. Especially in C# where there's no const.
|
# ? Jan 17, 2018 10:45 |
|
Rottbott posted:The possibility of a property doing something unpleasant when accessed seems no worse than the possibility of a getX() method doing something unpleasant. Especially in C# where there's no const. Is there a subtle difference between const and static final that I'm not grokking?
|
# ? Jan 17, 2018 13:22 |
|
Nth Doctor posted:Is there a subtle difference between
|
# ? Jan 17, 2018 13:28 |
|
Sometimes I write a method that will load or create an expensive value on the first invocation, then cache the result and return that for the object's lifetime. I'm always unsure if that kind of lazy loader should be a (read only) property or a function. The former suggests that it's just accessing a field, but the latter conversely suggests that it's doing work every time, neither of which is correct. Currently I lean towards properties because the method is effectively idempotent.
|
# ? Jan 17, 2018 13:35 |
|
Yeah there is, you just write them to not modify any of the object's fields. It's not like method bodies are created by some mystical process outside your control. Sure, if you want to communicate to callers of a method that a particular method doesn't modify the object, then sure, you only have conventions rather than anything actually enforced by the language. But that's true of a great many things in pretty much every language you care to name. For example, it's only convention that setX() and getX() are at all related in any way.
|
# ? Jan 17, 2018 13:36 |
|
Rottbott posted:final isn't a thing in C# is it? C# methods (i.e. GetX()) can't be made const which means there's no way to prevent them from mutating the object. final is a thing, on properties, at least.
|
# ? Jan 17, 2018 13:52 |
|
Nth Doctor posted:final is a thing, on properties, at least.
|
# ? Jan 17, 2018 14:54 |
|
SirViver posted:No. The closest things C# has to Java's final are sealed and readonly. The first is a class modifier and makes it so you can't inherit from that class anymore, and the latter is a field (not property) modifier that ensures that field can only be written to in the field initializer/constructor. For auto-implemented properties the equivalent to readonly is only specifying { get; }, which makes it functionally the same as a readonly field. This will teach me to look at programming stuff before having coffee. const is in fact a thing in C#: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/const
|
# ? Jan 17, 2018 15:22 |
|
canis minor posted:Oh, you kind of can - for `+` (but that's not proper overloading, but rather casting (?)). I think operator overloading won't come soon to JS, if at all. Still people try to do it Phew, I thought that meant it was accepted in ESnext - glad to hear it hasn't been raminasi posted:I'm pretty sure they're just syntactic sugar for getters and setters. I don't think that the CLR has any special use for them. They are, but I figure a clever optimizer could do something around the fact that they're a guaranteed getter/setter rather than just another method that happens to begin with 'get' or 'set'. Maybe not, though, maybe it's 'just' a vastly more pleasant syntax.
|
# ? Jan 17, 2018 15:25 |
|
Alex Martelli identifies the benefits of properties when it comes to Python here. In Python properties are usually preferred to getters/setters. In langauges like Java where getters/setters are expected, it's still unexpected for there to be much going on in the getter/setter other than returning/setting the value...you really don't want to be doing a lot in there. This is just a convention. Properties are the same dealio. You could of course kick off a lot of processing in a property, but the convention, just like with getters/setters is to not.
|
# ? Jan 17, 2018 15:37 |
|
Thermopyle posted:In langauges like Java where getters/setters are expected, it's still unexpected for there to be much going on in the getter/setter other than returning/setting the value...you really don't want to be doing a lot in there. java.util.Date IIRC has a getter in it that mutates the object's state. Which is something rather unexpected. It's been deprecated for 20 years and I still see it in production code every once in a while.
|
# ? Jan 17, 2018 16:29 |
|
If getters and setters help enforce encapsulation, it’s because they make it just a tiny bit more tedious to show the rest of the world fields that they maybe don’t need access to. Maybe things would be fine with mostly immutable state that’s all set by the constructor through dependency injection, reading from a config file or something else that doesn’t expose to the code using it what precisely it needs to complete its designated task.
|
# ? Jan 17, 2018 18:36 |
|
Nth Doctor posted:This will teach me to look at programming stuff before having coffee. const is in fact a thing in C#: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/const
|
# ? Jan 17, 2018 22:48 |
|
Munkeymon posted:They are, but I figure a clever optimizer could do something around the fact that they're a guaranteed getter/setter rather than just another method that happens to begin with 'get' or 'set'. Maybe not, though, maybe it's 'just' a vastly more pleasant syntax. What do you mean by "a guaranteed getter/setter?"
|
# ? Jan 18, 2018 01:07 |
|
You want a coding horror? C++ has a const modifier suffix for methods, which tells all and sundry that nothing in the method body is going to change anything about the object... Except that you can change members if you declare them with a mutable modifier prefix! C++ code:
|
# ? Jan 18, 2018 04:37 |
|
Not a horror at all. Static enforcement of design contracts is great, even if there are escape hatches.
|
# ? Jan 18, 2018 04:41 |
|
I mean, Rust has that too with a bit more gymnastics and checking. Rust has RefCell which is runtime-borrow checked, but the use case is meant to be for things like caching slow computations likecode:
Linear Zoetrope fucked around with this message at 04:47 on Jan 18, 2018 |
# ? Jan 18, 2018 04:44 |
|
Linear Zoetrope posted:That is, "side effects" that are invisible but beneficial to the user and don't meaningfully affect the program state. Oh, you mean like caching memory access during branch prediction?
|
# ? Jan 18, 2018 04:47 |
|
Absurd Alhazred posted:You want a coding horror? C++ has a const modifier suffix for methods, which tells all and sundry that nothing in the method body is going to change anything about the object... If you start declaring data members as mutable I'm going to murder you after work in the parking lot. That's how encapsulation is done in C++.
|
# ? Jan 18, 2018 04:54 |
|
Absurd Alhazred posted:Oh, you mean like caching memory access during branch prediction?
|
# ? Jan 18, 2018 04:57 |
|
If you didn't have mutable then you couldn't have an internal mutex with const getters because you'd have no way to lock it.
vOv fucked around with this message at 07:02 on Jan 18, 2018 |
# ? Jan 18, 2018 05:41 |
|
For what it's worth, Microsoft have published some guidelines on when to use properties here and here. It's mostly sensible stuff, although I disagree with a couple of things (in particular DO allow properties to be set in any order even if this results in a temporary invalid state of the object). A lot of the recommendations are mostly language-independent, so have a look if your lang has properties.
|
# ? Jan 18, 2018 06:59 |
|
redleader posted:It's mostly sensible stuff, although I disagree with a couple of things (in particular DO allow properties to be set in any order even if this results in a temporary invalid state of the object). Is that such a bad idea? Properties are meant to be pretty simple and act like fields right? So requiring changes to be made in some specific order would be confusing and a bit magical If you had plain fields you'd have to do the combined state validation at showtime too, just sounds like they're recommending the same behaviour. If you need to police state changes as they happen, methods that take all the parameters (or data classes or builders) are probably the way to go I'd have thought?
|
# ? Jan 18, 2018 07:41 |
|
baka kaba posted:Is that such a bad idea? Properties are meant to be pretty simple and act like fields right? So requiring changes to be made in some specific order would be confusing and a bit magical Yeah, good point. I was thinking from the point of view that you should try to avoid objects from being in invalid states whereever possible. But you're right, the fact that properties are intended to look like and work like fields is probably a more important concern. And if it's that important to keep an object in a valid state, you probably wouldn't expose the fields directly either
|
# ? Jan 18, 2018 09:22 |
|
redleader posted:Yeah, good point. I was thinking from the point of view that you should try to avoid objects from being in invalid states whereever possible. But you're right, the fact that properties are intended to look like and work like fields is probably a more important concern. Read-only properties exist for this. The getters can be brain-dead accessors, the setters need to go through validation. code:
|
# ? Jan 18, 2018 11:17 |
|
|
# ? May 21, 2024 01:39 |
|
raminasi posted:What do you mean by "a guaranteed getter/setter?" That they're guaranteed to have a certain narrowly defined structure surrounding some user code where that structure is slightly more narrowly defined than the mostly-equivalent hand-rolled methods. Off the top of my head, you could write a setter like C# code:
|
# ? Jan 18, 2018 15:49 |