Skip to main content
60 votes

Do you need to think about encapsulation if you can ensure immutability?

I hate how encapsulation is always framed as preventing unauthorized access. If this were the best way to think of it, immutability would indeed eliminate most of the need for encapsulation. In fact, ...
Karl Bielefeldt's user avatar
54 votes
Accepted

Do you need to think about encapsulation if you can ensure immutability?

The question Casting your question to real life: Is it okay for your doctor to post your private medical records publicly to Facebook, provided no one (other than you) is able to change it? Is it ...
Flater's user avatar
  • 59.5k
47 votes

Is it a bad idea have make a class method that is passed class variables?

Calling a class method with some class variables is not necessarily bad. But doing so from outside the class is a very bad idea and suggests a fundamental flaw in your OO design, namely the absence ...
Christophe's user avatar
  • 82.1k
33 votes
Accepted

Is it a bad idea have make a class method that is passed class variables?

There are may things with the class that I would do differently, but to answer the direct question, my answer would be yes, it is a bad idea My main reason for this is that you have no control over ...
Peter M's user avatar
  • 2,097
33 votes

Do you need to think about encapsulation if you can ensure immutability?

Encapsulation could mean that you hide the actual storage of immutable data. E.g.: class Color { private readonly uint argb; public byte Blue => (byte)(argb & 0xFF); public Color(byte ...
Emond's user avatar
  • 1,258
24 votes
Accepted

How does encapsulation actually work?

Coupling ClassA relies upon the interface only, delegating this responsibility of passing the classB object elsewhere This is the idea. If ...
Theraot's user avatar
  • 9,261
22 votes

How does encapsulation actually work?

Your confusion is probably caused by focusing on meaningless class and interface names, with no real usage scenario behind it. So better let us make a concrete example (I prefer C#, but it is not ...
Doc Brown's user avatar
  • 220k
18 votes
Accepted

C# encapsulate field is a violation of YAGNI

There are two completely different kinds of software: libraries that need a stable binary interface across multiple versions, and applications or internal software where you can just refactor. For ...
amon's user avatar
  • 136k
17 votes
Accepted

Can method names give any implementation details and break encapsulation?

The actual inventory is still hidden from outside classes (i.e., the List<GameItem> inventory is private), so encapsulation is not broken. Whether it makes more sense to name the method has(...
mmathis's user avatar
  • 5,586
17 votes
Accepted

Can renaming a method preserve encapsulation?

I think you are missing the point. Its not saying you should rename the setter and getter, but to have methods which add and remove items from the fridge. ie public class Fridge { private int ...
Ewan's user avatar
  • 84.4k
14 votes
Accepted

Is it okay to have misleading struct and function names for the sake of encapsulation?

This is why we invented the word meta. You're packing the result structure with meta info that isn't strictly part of what you're returning. But the info is about what you're returning. Just carve out ...
candied_orange's user avatar
13 votes
Accepted

Are there any legitimate use cases for protected visibility?

In short The protected visibility is a legacy that was designed long before Barbara Liskov's LSP (more precisely its history constraint) demonstrated its many drawbacks. Its only meaningful use is for ...
Christophe's user avatar
  • 82.1k
12 votes
Accepted

Separation of concerns: When is it "too much" separation?

Your various examples of splitting out concerns into separate functions all suffer from the same issue: you are still hard-coding the file dependency into get_last_appearance_of_keyword. This makes ...
David Arno's user avatar
  • 39.6k
12 votes
Accepted

Encapsulation violation

Returning a pointer to an internal variable of a class is an encapsulation violation, once you have that pointer, you can modify the speed variable inside of car. To avoid that, return the value of ...
geocodezip's user avatar
11 votes

Should a class provide public mutators for all its private fields?

So I did some googling. Here is the rule you quote: To respect OO encapsulation concepts, private fields should always be accessed through accessors https://www.appmarq.com/public/changeability,...
Ewan's user avatar
  • 84.4k
10 votes

Is the Liskov Substitution Principle concerned with protecting state invariants?

No, I don't think so. LSP says that S can substitute for T. What that means in practice is that S fulfills the same contractual obligations that T does. The writer of S can expose internal state (...
Robert Harvey's user avatar
10 votes

C# encapsulate field is a violation of YAGNI

YAGNI isnt a principle, its an excuse for not doing stuff. Here we can use the syntactic sugar public DataAccess Service { get; set; } and its cost us 10 characters worth of typing to get the ...
Ewan's user avatar
  • 84.4k
9 votes

What is the best practice for a regression test that wants to know about encapsulated information?

You wrote Alpha constructs a new Bravo class during its own constructor and exposes no public visibility to it So whatever Alpha does, using Bravo for it is an internal implementation detail, right?...
Doc Brown's user avatar
  • 220k
8 votes

Is it appropriate to process a property in the setter?

This breaks the principle of least surprise. For a property with a getter and setter, you would assume them to be symmetric, i.e. you get the same thing as you set, at least semantically. Here you set ...
JacquesB's user avatar
  • 62.3k
8 votes

The ID of an object is null at start. Does that make the object state invalid and violate encapsulation?

Identify your single source of truth. That's what has the authority. If your program is the authority on what can and can't happen with an employee then the DB is nothing but a way to persist ...
candied_orange's user avatar
8 votes

Do you need to think about encapsulation if you can ensure immutability?

If you can guarantee Immutability, do you need to think about Encapsulation? Perhaps, but probably not in a way most people think. First, realize that encapsulation (the practice of hiding the ...
Theodoros Chatzigiannakis's user avatar
7 votes

Is it a bad idea have make a class method that is passed class variables?

When the intention of this design is that you want to be able to reuse this method for data which does not come from this class instance, then you might want to declare it as a static method. A ...
Philipp's user avatar
  • 23.5k
7 votes

Should a class provide public mutators for all its private fields?

No, you don't seem to have missed anything. That audit sounds misguided, among other things it implies immutable objects, like String, are "against OO encapsulation concepts".
Caleth's user avatar
  • 12.4k
7 votes

Is there anything wrong with writing getter/setter methods in C#?

C# properties are made of methods. Thus, by using methods instead of properties, you do not get any runtime advantages. Instead it will be all about developer productivity and readability of your ...
Theraot's user avatar
  • 9,261
7 votes

Is it okay to have misleading struct and function names for the sake of encapsulation?

I don't see why you consider it "dirty" to keep the original name, as that's exactly what OOP does - there's a public interface to an object, and there are some internal implementation ...
Philip Kendall's user avatar
7 votes
Accepted

Confused on how abstraction and encapsulation is helpful

the function doesn't rely on assimp as much meaning it would be easier to change the assimp code or replace it with another library, but I still find it confusing how it's useful or helpful it just ...
JimmyJames's user avatar
  • 30.9k
6 votes

Does encapsulation in OOP happen at run time or compile time?

Encapsulation is a design principle. It's about writing software, has nothing to do with running or compiling software. To address the two concrete questions: For the main implementation of Python (...
Alexander's user avatar
  • 5,195
5 votes

A problem of decoupling

If it is acceptable that the Shape classes know how to draw themselves onto something (without knowing which output device or graphics library is used underneath), then you could introduce a ...
Bart van Ingen Schenau's user avatar
5 votes

Is it appropriate to process a property in the setter?

No, this is not fine; however, it is not the worst thing in the world either. I would reject a PR with this in it, but I would not get mad at you ;) Let's say you deploy this to production... a few ...
TheCatWhisperer's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible