I'm voting to close this question as off-topic because it is about mathematical induction / recurrence relation / Mathematics instead of programming or software development. — Pangjust now
@RobertHarvey If I remember correctly, the original image was animated and had the cloud standing alone with its arm extended (take a moment to look at the image above and imagine the cloud, alone). A few seconds later the monitor would fade in and, it would appear, uh, grab the cloud. The static version is far less confusing.
@RobertHarvey part of the problem is that I may not have ran into a case where a property has really helped over a field, but I've ran into plenty of cases like this where properties leaked implementation details like this. Though again for non-final classes I definitely agree that the ability to override a property is great, for final classes properties feel like they violate YAGNI at best.
Another thing that comes to mind is a certain class representing a connection implemented an IsConnected property that seemed to imply that the socket was still there but in reality it was only set on an initial connection and was never reset when the socket was disposed.
While these are probably unambiguously bad designs if the guidance for properties wasn't so overwhelming, this bad code would have been easier to find, I have a hard time just trusting the semantic contract an object after this.
also for final classes properties feel like they violate YAGNI at best. I meant for properties that are just plain get/set.
@jrh That post says, "Don't call private methods or fields using Reflection; expose a public method or property instead." Yes, reflection can expose implementation details if you use it, but that's not the fault of properties.
@jrh That's just a badly-designed class.
@jrh Think of it this way: you routinely lock the doors and windows to your house, for security reasons. Someone who is really determined could still break into your house. But that doesn't mean you simply give up and leave your doors and windows unlocked.
@jrh That isn't the fault of properties, that's just a lack of encapsulation. In particular, this is what the Law of Demeter is about: by returning a Socket, the complete Socket interface has become part of the PlayerConnection interface.
Well... as I mentioned getting state gets murky for more complex objects (is it a copy, or THE thing), I rarely set state in objects, it's usually more configuration settings, not really "state".
Instead of returning the entire socket, that guy could have just exposed some useful property on the socket object, using a get-only property in his own class.
Here's a better example: a class that contains a collection, like a dictionary. You can expose the collection as a public property, but you can also write an indexer instead. That indexer could do things like, for example, see if a key is already present in a dictionary before adding it. In an ordinary dictionary, that would be two steps, so now you have a dictionary that's a little bit smarter.
But at the end of the day, if you need to expose an ImageList or PictureBox as a property, then that's what you do.
Indexers are great, I think the IEnumerable interface definitely has its uses; though regarding ImageList and PictureBox, I'm not exposing the Controls, I'm talking about situations similar to the Image property ON those controls when I design classes with similar resource requirements.
@jrh If the object you're exposing has “unsafe” methods that you don't want to expose, the usual solution is to create an interface for the safe methods, then use the object adapter pattern to connect the interface to the actual object.
There's several cases where an Image property's ambiguity (copy or return the actual image?) on a class could have made the code very hard to read, I'd almost need both a GetCopy and GetImage method.
C# returns object references by value. It's almost always the actual object, not a copy. If you want copy semantics, you have to specify that explicitly.
What is "GetImage?" If you're coming from the Java world, that's not how it works in C#. It's simply Image.
I agree, for reference types the assumption is never that I own that instance and are responsible for cleanup, unless I've created the instance or asked for a new copy.
Kind of a tangent, but since the image is returned by reference, how would you know as a caller when it's safe to Dispose PictureBox.Image? Would it even be possible to determine if other PictureBoxes were using it? (it's easy if the image is a local resource but if it's set in code it gets a little less obvious)
we probably need a new meta faq / canonical, "Why was my blatantly off-topic question quickly deleted by a moderator?"
@amon you are right, but if this site has a «be nice policy», I think that by imediatly delete other peoples questions doesn't fit in those «be nice policies». If I'm in a Math forum and someone ask me: «Why 2+2=4?» I'll try to approach the level of thinking of that person. There are no such thing of "stupid question" and all question deserves an answer. — user2560835 mins ago
@jrh Why was my question closed or down voted? - this is it. But apparently some askers wonder why their questions are not only closed and voted down but deleted. They would of course prefer it to hang at the site for a while and gain answers (in comments if it is closed). See above ^^^ "all question deserves an answer"
@RobertHarvey you probably haven't read the question text yet, top hit is this: "Can you please explain me how can I get an answer for my question, let me HAVE TIME to read the answer and then delete my question if you want?" Lovely... how lovely!
@ThomasOwens it's right there in your answer to duplicate question. Scroll to the words "deleted on the advice of Shog9, one of the Stack Exchange CMs..." - there's a link and quote
...just in case if you feel proud of being personally targeted by garbage droppers, don't. One post at MSE (deleted) suggests that Yannis is most evil mod over here, not you
I guess he has stopped already. Now that all moderators do quick deletions there is just not enough garbage posts left for the regular deletion raids like he did in the past (long before Shog recommended this). Though maybe it's too late, he did too much harm to poor askers already and this will keep him top-evil forever
@amon I guess in that case the class with the method that called the constructor of the unmanaged resource would be the only one responsible for freeing it? The other ones would just be taking the resource "by reference". I prefer to keep unamanged resources as local variables within a single function (so I can use using) but it just doesn't work all the time, e.g., if I have to display an image.
Looking back that was what I did in the code, I didn't really follow that deliberately but that's just sort of where the resources fell into place.
If there's no chance of misunderstanding (i.e., it's generally accepted that an image would be returned by reference) then that makes my life easier, I can give access to the Image to display it but I don't have to worry about client code disposing the image being displayed because they would understand that it might be used in other places.
@jrh Yes. Some languages are a bit more helpful and have an explicit concept of “owning” and “borrowing” a reference.
However, a method that creates a resource needing cleanup may also transfer that responsibility, for example a factory won't also do cleanup. Ideally that's obvious from context and clearly documented, but then again there is this thing called the real world…
It would be extremely confusing if accessing an object through a property would transfer ownership, but if it's a method called CreateFoo(), transferring ownership seems sensible.
You're not changing the human. He can still go on vacation. Like I said, give supervisors the ability to unlock files and assign them to someone else. If you look at the answers posted so far, I am now the third person who has recommended this. — Robert Harvey5 mins ago
[sigh] -- The only thing worse than asking misguided questions is not taking the advice of the people you're asking. It suggests that all you want is to have your preconceived answer validated by others.
@amon makes sense, thanks. The idea of owning and borrowing a reference having language support is very interesting, what languages support this? (Also: for some reason in the back of my mind I assumed the idea of "Ownership" was not as important in C# as it was in C or C++, due to the GC I guess)
That wasn't a correct assumption, obviously. It only made things more confusing.
@jrh C++ pioneered this to a degree. If you have an object by value, you own it (duh). A reference is always borrowed. A pointer has no inherent ownership semantics. But there are various smart pointers like unique_ptr (single owner), shared_ptr (refcounted, multiple owners). Rust elaborated on that system and made ownership/lifetimes part of the type system. References can be qualified with lifetimes; the type checker prevents you from holding a reference longer than it exists.
Escape analysis is also used by some garbage-collected language implementations to avoid GC overhead for local variables – if only I use that object, I can allocate it on the stack.
Because C++ has RAII (deterministic destruction allows us to couple resource management with object lifetime) resource management is straightforward. GC only automated management of memory resources. Java's Autoclosable+try-with-resource, C#'s IDisposable+using statement, Python's context managers+with statement all add resource management back to the language, but it's not as safe as RAII: now the using programmer has to write extra code to ensure correctness, and the compiler can't help.
@amon interesting analysis, I think I got a little confused about ownership when I no longer had to differentiate between pointers to things and the things themselves for reference types in C#.
(I get stuck in VB land sometimes, I go back and forth between C#, C and VB.NET and you can tell which one I'm Using When I Capitalize Every Single Thing)
@RobertHarvey for general resource management? RAII is safer. If I write a class that needs to do stuff in the destructor, I can be sure the destructor will be invoked at a specific time (for local variables: when the current scope is left, for instance variables: after the enclosing instances destructor has run). GC may only happen much later. The only way to mess that up is to create an object with the new operator, which in C++ screams “I know what I'm doing!”.
In C# or Java, it's easy to mess up an IDisposable/AutoClosable object: I just create it and forget to put it into an using/try-with-resource statement.
@amon I've picked out the header space but the rest of it seems to be purposefully garbled (even a small change to the file in it's special IDE means almost every byte is modified), in what order should I test cyphers?
I'm first implementing a Caeser cipher decoder/encoder but I can tell from a quick glimpse that this isn't the case.
@amon Does that pass muster or am I better off with r/programming?
@HireThisMarine It seems you're asking for debugging help. Software Engineering is more about software development concepts, design questions, development methodologies, …. So no, this is not the place for your questions.
@amon Sure. When I questions on Reddit that don't really fit they're usually just ignored, as opposed to being tarred and feathered on stack. Good day.
Oh, I didn't realize that this was a self-answered question. Sorry. Then, may I ask, how is this related to programming? If anything, this is software engineering/design and would perhaps be more appropriate on Software Engineering. — Cody Gray48 secs ago
@RobertHarvey Not related to what we were talking about earlier, but adding to the other two articles I linked before you might be interested in this. I only read the first couple articles in the list, but they seem to bring up some new things that I haven't read elsewhere.
I guess the other two people making the same suggestion are wrong too, then. If you aren't open to advice, then why did you ask the question? — Robert Harvey2 mins ago
@glennjackman Actually, SO is not an IT help desk. Fixing found code for non-programmers is too broad and therefore off-topic for this site. — TigerhawkT311 secs ago