The tag has no usage guidance.

learn more… | top users | synonyms

0
votes
2answers
105 views

Private fields and encapsulation

I'm reading the head first c# book and don't quite understand what this means. "Any class can see private fields in another instances of the same class"
4
votes
2answers
604 views

Use a global variable, a singleton, or something else

Preface: I am working in PHP (Abandon hope all ye who enter here). Background: There exists a large set of global functions in PHP, a number of which are miscellaneous system calls, like sleep (and ...
1
vote
1answer
54 views

Chain accessors or return the value of private pointer

hopefully the title isnt too confusing, I am having trouble thinking through what best-practice dictate I do in this situation. Say I have the following class structure Class A { public: bool ...
138
votes
13answers
88k views

Why do we need private variables?

Why do we need private variables in classes? Every book on programming I've read says this is a private variable, this is how you define it but stops there. The wording of these explanations always ...
0
votes
0answers
12 views

JPA: Property/encapsulated objects instead of fields

I'm moving to JPA (JavaEE, Eclipselink) from a very large C# project. The classes were not using simple fields but Property objects. Example: //C# public class State : DataObject<State> //base ...
2
votes
2answers
63 views

Testing properties with private setters

Currently in a part of my project a domain object like below exists: public class Address { public virtual string HouseName { get; set; } public virtual string HouseNumber { get; set; } ...
5
votes
5answers
306 views

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

Which is generally accepted practice between these two cases: function insertIntoDatabase(Account account, Otherthing thing) { database.insertMethod(account.getId(), thing.getId(), thing....
30
votes
5answers
6k views

Why do we need enums in dynamically typed languages?

I was reading some code here and saw that an enum is used to store names of html tags. Why do we ever need to do this? What benefit do I get using this strategy? I know that how useful enums are in ...
0
votes
2answers
242 views

Don't auto-implemented properties violate encapsulation?

In C#, when a field getting or setting requires logic, we use properties. For example: private int _age; public int Age { get { Console.Writeline("Club member is {0} years old", _age);...
4
votes
2answers
177 views

How far should encapsulation in JavaScript go?

I have a variable which I want to use in only one function. I can write my code like this: var theAnswerToLife = 42 var multiplyIt = function(x) { return ++theAnswerToLife * x } I have some ...
1
vote
1answer
108 views

Should I actually be using encapsulation here?

Problem Statement I am going to write a library to control a whole bunch of relays, and I am just trying to think conceptually right now. Here are my design constraints: We have an exact number of ...
2
votes
1answer
144 views

Public versus private inheritance when some of the parent's methods need to be exposed?

Public inheritance means that all fields from the base class retain their declared visibility, while private means that they are forced to 'private' within the derived class's scope. What should be ...
3
votes
2answers
197 views

How do I deal with unneeded inherited members?

Consider the following UML diagram: In a nutshell: ChildClass1 and ChildClass2 use all of the members of ParentClass. ChildClass3only uses Member1 and Member2 and the value of Member5 is a ...
18
votes
5answers
7k views

If a variable has getter and setter, should it be public?

If I have a class with a variable that is private and the class have getter and setter for that variable. Why don't make that variable public? The only case I think you have to use getters and ...
6
votes
3answers
378 views

Best practices for using public, protected, private?

Is it fair to say that it is good practice to default everything to private up front when coding something? And then only upgrade it to protected if a subclass needs it, or public if another class ...
242
votes
17answers
18k views

Why have private fields, isn't protected enough?

Is the visibility private of class fields/properties/attributes useful? In OOP, sooner or later, you are going to make a subclass of a class and in that case, it is good to understand and being able ...
19
votes
5answers
4k views

Is it okay for a class to use its own public method?

Background I currently have a situation where I have an object that is both transmitted and received by a device. This message has several constructs, as follows: public void ReverseData() public ...
0
votes
1answer
103 views

Best practice for settings for PHP class for code generation [closed]

I would like to have a class creating any code from given options and output it in a chosen way. I have written class for generation of simple JavaScript code for such cases where is needed to ...
1
vote
0answers
99 views

Data Mapper pattern vs. model encapsulation

I am currently developing my first custom data mapper layer but have run into a conceptual problem that seems to be glossed over all books and guides I have read. My problem is as follows. My data ...
1
vote
0answers
106 views

Change object state in different point in program

This is not about whether or not getters/setters are wrong. I understand its impact to encapsulation and that question has been raised here and SO several times already. I also do not want to just ...
2
votes
1answer
154 views

Javascript Closure Style Similar to Java Class Structure

PROBLEM: There is a coding imperative (S. McConnel, Code Complete) that one shouldn't code on language, but by means of it, e.g. doing right style things even if language doesn't have some ...
6
votes
4answers
334 views

Class design dilemma: Encapsulation vs Single Responsibility/Separation of Concerns

I'm working on a class that represents an object with multiple representations - one is an XML type representation used by an automatic ordering system, the other is a POJO-based representation used ...
3
votes
1answer
180 views

Passing MVVM objects to other classes [closed]

So I'm using C# WPF and binding to a Model View to display states. The data that needs to be displayed is spread across a couple different classes. For instance I have a class that reads out digital ...
1
vote
1answer
93 views

Setting up my inheritance model?

I'm working on refactoring a synchronization web app that our company uses. Currently it's merely a set of controllers that fire up a set of helpers that go through a long chain of if-else type ...
1
vote
0answers
93 views

Having a globally static cached list without breaking encapsulation?

I have a couple wrappers around third party APIs that allow me to retrieve information from certain web services. Querying these services can be rather time intensive, so in my older version of my ...
0
votes
1answer
113 views

Should I hold an Id and the Item [duplicate]

Let's say I have a Customer which contains an Address class like: public class Customer { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; ...
1
vote
2answers
107 views

How to mitigate complexity of fallbacks introduced to automatically retry upon failure?

So this may come off a bit broad and generalized, but after spending some time working around under developers, I've come to notice many different methods and design patterns. However, one big thing ...
3
votes
1answer
113 views

A condition on an argument used in multiple calls of a method: enforced by caller or by the method?

Let's assume I have an object a of a class A. It has a method that needs an argument in form of another object of a particular type - but the argument should also be in particular state, because the ...
15
votes
4answers
63k views

What is the use of Association, Aggregation and Composition?

I have gone through lots of theories about what is encapsulation and the three techniques of implementing it, which are Association, Aggregation and Composition. What I found is: Encapsulation ...
0
votes
3answers
163 views

Difference of efficiency [duplicate]

I'm doing a compiler, and I'm using the System.out.println(); to print assembly; And the code get bigger, and more complicate to understand. I want to know whats is the difference of efficiency ...
0
votes
2answers
164 views

Meaning of using getters and setters and Uses of parameterized Constructor. [duplicate]

I am using getters and setters to for the purpose of encapsulation. public class Student { private String studentID; private String studentName; private String address; public ...
47
votes
7answers
8k views

Using third-party libraries - always use a wrapper?

Most projects I am involved with use several open-source components. As a general principle, is it a good idea always to avoid binding all components of the code to the third-party libraries and ...
4
votes
2answers
180 views

Does returning pointer to composed objects violate encapsulation

When I want to create an object which aggregates other objects, I find myself wanting to give access to the internal objects instead of revealing the interface to the internal objects with passthrough ...
6
votes
6answers
2k views

What is the usefulness of private variables? [duplicate]

While I've never used a language that had built-in variable privacy, a book I'm reading by Douglas Crockford explains a way to create privacy in JavaScript, however it doesn't make sense to me so. The ...
2
votes
2answers
412 views

How is encapsulation broken by getters/setters, even when using MVC model [duplicate]

I was thinking about encapsulation in Java and then I thought how getters/setters break encapsulation. After I went through this , I saw many recommendations like to avoid getters/setters on fields ...
15
votes
6answers
7k views

Why did Java make package access default?

I'm asking this question because I believe they did it for a very good reason and that most people do not use it properly, well from my experience in industry so far anyway. But if my theory is true ...
3
votes
3answers
320 views

'Encapsulation Vs Performance' trade-off in a lockable linked list

Main purpose of encapsulation is to protect in-variants of any class(in java). Here is the complete code for class DList/class lockDList/class DListNode/class LockDListNode. class DList and it's ...
3
votes
3answers
425 views

Abstraction concept in OOP

I hear that Abstraction is a technique that helps us identify which specific information should be visible, and which information should be hidden. Encapsulation is then the technique for packaging ...
3
votes
1answer
61 views

Abstract/encapsulating session superglobal usage inside common accessor class

I am in the process of removing direct dependencies from an existing project. I have now come to removing the direct use of $_SESSION inside scripts. This is to make the same scripts available for an ...
3
votes
2answers
2k views

What are the disadvantages of self-encapsulation?

Background Tony Hoare's billion dollar mistake was the invention of null. Subsequently, a lot of code has become riddled with null pointer exceptions (segfaults) when software developers try to use (...
4
votes
3answers
271 views

Nested POJOs, exposing collections, detecting changes

I have a couple plain object classes to represent data read from a file. They look something like this: public class Command { private String name; private List<Message> messages; } ...
5
votes
1answer
484 views

How to store satellite data in C data structures

I've been reading through Introduction To Algorithms 3rd Ed, and I am having difficulty in implementing some practical situations. It's not the theory, or implementing the internals of the data ...
0
votes
1answer
181 views

How much to encapsulate when objects being dealt with are pretty similar

As I have been answering questions related to object-oriented design and how to go about reducing the amount of classes to make sure that there is not a lot of "class clutter" and code repetition I ...
2
votes
1answer
351 views

Should we encapsulate everything in a try{} block in a Try object?

Why can't I make a class for a Try including what I try and then run that in the try {} block? Why is it impractical? class DBConnectTry extends Try { TryResponse response[] attempt(TryObject o[...
12
votes
4answers
1k views

Do I suffer from encapsulation overuse?

I have noticed something in my code in various projects that seems like code smell to me and something bad to do, but I can't deal with it. While trying to write "clean code" I tend to over-use ...
7
votes
5answers
414 views

Should private functions be held to the same standards as public functions?

If I'm building private utility functions, should they be held to the same rigorous standards in terms of handling invalid data as public functions? Example:If I'm writing code to calculate the ...
1
vote
1answer
114 views

If the representation part of type abstraction is declared private, then how does sub-classing violate encapsulation?

Here, I learned that sub-classing violates encapsulation: it violates encapsulation, since the implementations of the superclass and subclass become tightly coupled For instance, If we consider ...
4
votes
1answer
434 views

How can I use the “Non-Member Functions Improve Encapsulation” pattern from C#?

In 2000, Scott Meyers argued that non-member functions improve encapsulation. In C++, a non-member function is a C-style global function: http://www.drdobbs.com/cpp/how-non-member-functions-improve-...
1
vote
4answers
310 views

Why I need to make method of my class private, If I need to access it throughout my application?

I am going through the beginners tutorial for C# from a website http://www.homeandlearn.co.uk/csharp/csharp_s10p1.html On chapter of classes, it makes the method of class private and then use ...
14
votes
5answers
3k views

Can I use Dependency Injection without breaking Encapsulation?

Here is my Solution and projects: BookStore (solution) BookStore.Coupler (project) Bootstrapper.cs BookStore.Domain (project) CreateBookCommandValidator.cs CompositeValidator.cs IValidate.cs ...