Skip to main content
98 votes
Accepted

Is it a security vulnerability to declare class members as public?

Access modifiers like public/private/protected are not intended as a security boundary. And since C++ is not a memory-safe language, this cannot be a security boundary. The laziest “attack” to access ...
amon's user avatar
  • 135k
47 votes

In OOP, isn't the 'protected' keyword required?

It depends on what you mean by "required". Access modifiers are not a necessity. You could replace every access modifier with public and most applications will work just like they did when you used ...
Flater's user avatar
  • 52.7k
38 votes

In OOP, isn't the 'protected' keyword required?

No, it's not required: Bjarne Stroustrup, explained how he naively added protected to C++ release 1.2, thinking to provide a useful feature to class developers, just to conclude only 5 years later ...
Christophe's user avatar
  • 78.7k
37 votes

How to analyze and understand the use/application of a "class" in a colossal million-line legacy code base?

One over riding pattern has emerged in every interaction I've ever had with technology: I learn more when it breaks. So break it. I mean, this is software. You can't hurt it. Sneak a copy off some ...
candied_orange's user avatar
20 votes
Accepted

Classes vs. modules in Python

A python module is nothing but a package to encapsulate reusable code. Modules usually, but not always, reside in a folder with a __init__.py file inside of it. Modules can contain functions but also ...
Eneko Alonso's user avatar
18 votes
Accepted

How to resolve class interdependence in my C++ code?

There are two parts in your question. The first part is the organization of C++ header files and source files. This is solved by using forward declaration and the separation of the class declaration (...
rwong's user avatar
  • 16.9k
18 votes
Accepted

Is it best practice to define a member function directly in a class?

When a function is defined as part of the class definition, it is implicitly inline (regardless of whether you use that keyword). This has a number of consequences. The function may be inlined by the ...
amon's user avatar
  • 135k
16 votes
Accepted

Is calling the superclass constructor in a subclass really important?

If you add variables to self in the constructor of Class and don't call Class.__init__() in the constructor of Subclass, then these variables will not be in your Subclass object. See that question ...
DRz's user avatar
  • 276
15 votes
Accepted

What is a good design practice to avoid asking a subclass type?

Polymorphism So long as you use getType() or anything like it, you're not using polymorphism. I understand feeling like you need to know what type you have. But any work you would want to do while ...
candied_orange's user avatar
14 votes

Is it a security vulnerability to declare class members as public?

Using public and private correctly (and following good practices in general) helps you write better code with fewer bugs, and code with fewer bugs is typically harder for an attacker to exploit. ...
forest's user avatar
  • 414
13 votes
Accepted

Proper way to name a class which has just methods and not fields

If a class is properly encapsulated it's hard to tell if it even has fields from outside. Some classes are immutable. You can't change their fields once their objects are constructed. Your class is ...
candied_orange's user avatar
13 votes
Accepted

How to analyze and understand the use/application of a "class" in a colossal million-line legacy code base?

First, you need a goal when analyzing a class. If you don't have this, you have no idea when to stop. And with a million-line codebase, you could go on forever. Since we don't often read code for the ...
Greg Burghardt's user avatar
12 votes

Is there a better way of using getters and setters on private members of a class?

Using properties A proper getter and setter would look like this: public class Carrots { private string name; public string Name { get { return name; } set { name = ...
John Wu's user avatar
  • 26.6k
11 votes
Accepted

Define C++ class in one or more files

It depends, as usual. Generally, it's best practice to split each class into a header (.h or .hpp) and source (.cpp) file, where you put everything you can in the source file*, because it ...
Peter's user avatar
  • 3,748
10 votes
Accepted

How should an `Employee` class be designed?

This is a more well-formed transcription of my initial comment under your question. The answers to questions addressed by the OP may be found at the bottom of this answer. Also please check the ...
Andy's user avatar
  • 10.4k
10 votes
Accepted

What's the best way to handle a large multi-file class in Javascript?

In situations like this, when I have one part of my program that needs to stand alone as a single, self-contained module but contains many thousands of lines of code, I prefer to take a compositional ...
Ken Halbert's user avatar
10 votes
Accepted

Does wrapping functions/'things' in classes reduce efficiency?

This statement is either wrong or incomplete: Calling a non-virtual member function requires exactly the same code as calling a free function with one additional pointer argument. That's peanuts. ...
Christophe's user avatar
  • 78.7k
10 votes

How to analyze and understand the use/application of a "class" in a colossal million-line legacy code base?

If it is available, look at the history of commits related to the class in your version control system (CVS, SVN, git, mercurial...). Commit messages might help. Context of commits (ie other commits ...
Pablo H's user avatar
  • 665
9 votes

Why instantiate and initialize an object without saving it into a variable?

If this is the case then would it be right to say that it is not stored in memory? Not really. The Scene object is being instantiated, which by definition means memory is allocated. Why would we ...
Dan Pichelman's user avatar
9 votes

In OOP, isn't the 'protected' keyword required?

Python is also a language that strongly adheres to the object oriented programming approach. It uses the classical approach of classes and objects. The thing to remember however is that any "word" is ...
paul23's user avatar
  • 1,091
9 votes

Terminology: Classes that contain only methods

Question #1: what is the name of a class that contains only methods? Stateless Question #2: what is the name of a class that contains both fields and methods? Stateful These "names" (...
candied_orange's user avatar
8 votes
Accepted

why can't class directly take arguments instead of using a constructor?

Your example class Car(String theCountry) { String country; country = theCountry; String display() { return country; } } has several flaws that "real" constructors dont. You ...
F.P's user avatar
  • 609
8 votes

How to best to organize class and interface files?

Since an interface is abstractly similar to a base class, use the same logic you would use for a base class. Classes implementing an interface are closely related to the interface. I doubt you would ...
Frank Hileman's user avatar
8 votes

In OOP, isn't the 'protected' keyword required?

Before we decide that the protected access modifier must be removed from all popular OO languages I would like to point out that it would be pretty inconvenient to lose it. In abstract base classes ...
Martin Maat's user avatar
  • 18.5k
8 votes

Is judicious & plausible selection of class fields a valid refactoring step?

Your question seems to be based on a misunderstanding. Fowler does not "dislike temps" in general. Quite the opposite, he provided a specific refactoring for introducing such variables, he called it ...
Doc Brown's user avatar
  • 210k
8 votes
Accepted

Why would you make a struct without typedef?

C doesn't offer custom namespaces as C++ does, but it's untrue that C doesn't have namespaces at all. Functions and structures are in different namespaces: #include <stdio.h> void Test ( ) { ...
Mecki's user avatar
  • 2,350
8 votes
Accepted

Why List<T>.Enumerator is struct?

According to a video from a conference session called "Writing Allocation Free Code In C#", the Net Core team is taking efforts to reduce unnecessary object allocations. Since struct is a "Value" ...
Berin Loritsch's user avatar
8 votes

Is it a security vulnerability to declare class members as public?

Private and protected do not provide any security at all. Why? Because they are in your source code. They only affect code written by someone with access to your source code. If I have access to your ...
gnasher729's user avatar
7 votes

What is a good design practice to avoid asking a subclass type?

The description of the problem sounds like you should use Multimethods (aka Multiple dispatch), in this particular case - Double dispatch. The first answer went at length on how to deal generically ...
Roman Susi's user avatar
  • 1,773
7 votes

Association + multiplicity vs aggregation and composition?

UML is not one of these mathematical computation models that try to achieve completeness with a minimal set of rules(like Turning machines or lambda calculus). UML is a visualization of software ...
Idan Arye's user avatar
  • 12.1k

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