258 votes

Why have private fields, isn't protected enough?

In OOP, sooner or later, you are going to make a subclass of a class This is wrong. Not every class is meant to be subclassed and some statically typed OOP languages even have features to prevent it, ...
user avatar
235 votes
Accepted

Why have private fields, isn't protected enough?

Because as you say, protected still leaves you with the ability to "modify the implementation completely". It doesn't genuinely protect anything inside the class. Why do we care about "genuinely ...
user avatar
  • 27.2k
66 votes

TDD Red-Green-Refactor and if/how to test methods that become private

A lot of people think that unit testing is method-based; it's not. It should be based around the smallest unit that makes sense. For most things this means the class is what you should be testing as a ...
user avatar
  • 48k
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, ...
user avatar
57 votes
Accepted

Why do we need enums in dynamically typed languages?

A benefit is that the compiler can let you know if you accidentally type "ADRESS" or "FEILDSET", and letting you fix it immediately instead of behaving in a nonsensical way at runtime. While the ...
user avatar
  • 27.2k
52 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 ...
user avatar
  • 37.5k
50 votes

TDD Red-Green-Refactor and if/how to test methods that become private

The fact that your data-gathering methods are complex enough to merit tests and separate enough from your primary goal to be methods of their own rather than part of some loop points to the solution: ...
user avatar
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 ...
user avatar
  • 67.8k
44 votes
Accepted

TDD Red-Green-Refactor and if/how to test methods that become private

Units I think I can pinpoint exactly where the problem started: I figured, I'll need a method that finds all the non-numerical fields in a line. This should be immediately followed with asking ...
user avatar
  • 6,763
39 votes

Using third-party libraries - always use a wrapper?

By wrapping a third party library you add an additional layer of abstraction on top of it. This has a few advantages: Your code base becomes more flexible to changes If you ever need to replace the ...
user avatar
39 votes
Accepted

Should I place functions that are only used in one other function, within that function?

I'm generally in favor of nested functions, especially in JavaScript. In JavaScript, the only way to limit a function's visibility is by nesting it inside another function. The helper function is a ...
user avatar
38 votes
Accepted

Do you generally send objects or their member variables into functions?

Neither is generally better than the other. It's a judgment call you have to make on a case-by-case basis. But in practice, when you're in a position that you can actually make this decision, it's ...
user avatar
  • 27.2k
37 votes
Accepted

What is the usefulness of private variables?

Within software development, privacy (of software entities) is usually defined as restricting access to that variable/function/method. If a variable is private, then only functions or methods that ...
user avatar
34 votes

Why have private fields, isn't protected enough?

Yes, private fields are absolutely necessary. Just this week I needed to write a custom dictionary implementation where I controlled what was put into the dictionary. If the dictionary field were to ...
user avatar
  • 9,617
33 votes
Accepted

What does it mean when one says “Encapsulate what varies”?

You can write code that looks like this: if (pet.type() == dog) { pet.bark(); } else if (pet.type() == cat) { pet.meow(); } else if (pet.type() == duck) { pet.quack() } or you can write code ...
user avatar
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 ...
user avatar
  • 1,967
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 ...
user avatar
  • 1,248
30 votes

TDD Red-Green-Refactor and if/how to test methods that become private

Personally, I feel you went to far into the implementation mindset when you wrote the tests. You assumed you would need certain methods. But do you really need them to do what the class is supposed to ...
user avatar
  • 6,947
27 votes

Why combining getters, setters and private access modifier is not enough to hide implementation?

Object-Oriented Data Abstraction is all about Behavioral Abstraction. The implementation details are hidden behind a behavioral interface, i.e. an interface that does something. A variable is not ...
user avatar
26 votes

Do you generally send objects or their member variables into functions?

This isn't an exhaustive list, but consider some of the following factors when deciding whether an object should be passed to a method as an argument: Is the object immutable? Is the function 'pure'?...
user avatar
  • 9,704
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. ...
user avatar
  • 8,661
22 votes

Why do we need enums in dynamically typed languages?

Enums are useful for situations where you have a fixed set of values/entities that are sensible. They are self-documenting and allow the compiler validate things that would otherwise be left to run-...
user avatar
  • 22.1k
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 ...
user avatar
  • 185k
19 votes

What does it mean when one says “Encapsulate what varies”?

"Varies" here means "may change over time due to changing requirements". This is a core design principle: To separate and isolate pieces of code or data which may have to change separately in the ...
user avatar
  • 54.7k
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 ...
user avatar
  • 124k
17 votes

Should I place functions that are only used in one other function, within that function?

You should put it at global scope, for several reasons. Nesting a helper function into the caller increases the length of the caller. Function length is almost always a negative indicator; short ...
user avatar
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(...
user avatar
  • 4,415
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 ...
user avatar
  • 64.6k
16 votes

What does it mean when one says “Encapsulate what varies”?

Both of the current answers seem to only partially hit the mark, and they focus on examples that cloud up the core idea. This is also not (solely) an OOP principle but a software design principle in ...
user avatar
15 votes

What is the usefulness of private variables?

Sure, a developer with access to the source code could start manipulating whatever they want. And in the process make something very fragile and unmaintainable. By making as much private as possible, ...
user avatar
  • 5,632

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