Hot answers tagged class
54
He is too general about it. He is correct, it hinders testing. However, static classes and methods have their place and it really depends on the context. Without code examples you can't really tell.
I use static when I am not going to create an instance of a class because the class is a single global class used throughout the code.
This can be severe ...
46
The simplest answer is that if you put everything into one class, you have to worry about everything at once when you're writing new code. This may work for small projects, but for huge applications (we're talking hundreds of thousands of lines), this quickly becomes next to impossible.
To solve this issue, you break up pieces of functionality into their ...
35
The general rule to follow is that structs should be small, simple (one-level) collections of related properties, that are immutable once created; for anything else, use a class.
C# is nice in that structs and classes have no explicit differences in declaration other than the defining keyword; so, if you feel you need to "upgrade" a struct to a class, or ...
26
C++ can have non-method functions just fine, if they do not belong to a class don't put them in a class, just put them at global or other namespace scope
namespace special_math_functions //optional
{
int math_function1(int arg)
{
//definition
}
}
22
Well, the simplest response might be "It helps organize things." If nothing else, you might compare it to sections in a notebook -- it's just plain simpler if you have "all the stuff relating to the UI" over here and "all the stuff relating to the gameplay" over there.
The more sophisticated answer is that divvying up work is not just convenient, but very ...
21
Most of the time: An anti pattern.
Why? Because it faciliates procedural programming with "Operator" classes and data structures. You separate data and behaviour which isn't exactly good OOP.
Often times: A DTO (Data Transfer Object)
Read only datastructures meant to exchange data, derived from a business/domain object.
Sometimes: Just data structure.
...
21
I'm seeing a lot of instantiable classes in the C++ and Java world
that don't have any state.
Some possibile reasons to create classes without ivars of their own:
State is or could be contained in a superclass.
Class implements some interface and needs to be instantiable so that instances can be passed to other objects.
Class is intended to be ...
19
The case for any change of practice is made by identifying the pain points created by the existing design. Specifically, you need to identify what is harder than it should be because of the existing design, what is fragile, what is breaking now, what behaviors can't be implemented in a simple manner as a direct (or even somewhat indirect) result of the ...
18
I checked and fully 1/4 of my classes are marked "static". I use
static when I am not going to create an instance of a class because
the class is a single global class used throughout the code.
The best thing to do is to try and unit-test your code. Try designing tests that are repeatable, independent, simple and test only one method at a time. Try ...
16
1. Singleton
You restrict the number of instances because the constructor will be private meaning only static methods can create instances of that class (there are other dirty tricks actually to accomplish that but let's not get carried away).
Creating a class that will have only 2 or 3 instances is perfectly feasible. You should be using singleton ...
15
No, it is not right that an "object" is always an instance of a class. Just for example, the standard for C (which doesn't have classes at all) defines an object as (§3.14/1): "region of data storage in the execution environment, the contents of which can represent values."
Now, it is true that using "object" to refer to an instance of a class is quite ...
12
It sure looks like a Mock.
While often used for testing, it's also sensible in a Duck-typed language to mock other class definitions.
You've got two classes which are both implementations of a common interface. This is polymorphism in action. There's not much of "standard formal" name for it because it's just OO programming.
In Python, because there's ...
12
Well, I had a read through some of the code you linked to, and your post, and my honest summary is that the majority of it is basically completely worthless. Sorry. I mean, you have all of this code, but you haven't achieved anything. At all. I'm going to have to go into some depth here, so bear with me.
Let's start with ObjectFactory. Firstly, function ...
11
StackOverflow has a great discussion on this topic. For ease of reference, I'll copy and paste it here, on behalf of its author, Mark S. Rasmussen:
I wrote my thoughts of static classes in an earlier thread:
I used to love utility classes filled up with static methods. They
made a great consolidation of helper methods that would otherwise lie
...
11
The principle is that of Separation Of Concerns.
Bob Martin would say - a class should have only one reason to change. That would mean it has a single concern - a single thing it does.
This is a design principle that when followed means that code is easier to change and understand.
In your example of car and paint, it is difficult to say whether they ...
11
I believe the answer is, per Wikipedia, that Java was designed to be simple and object oriented. Functions are meant to operate on the classes they are defined in. With that line of thinking, having functions outside of a class doesn't make sense. I am going to leap to the conclusion that Java doesn't allow it because it didn't fit with pure OOP.
A quick ...
9
No, it's not enough to deal with the raw pointer in the destructor. You also have to deal with it in the copy constructor and assignment operator functions. It's usually not enough to simply copy the pointer (which the compiler will happily do for you); you probably really want to make a copy of the resource it points to. And you have to remember to ...
9
First, you need to present that any measurable organisation need to adopt industry best practices. Saying that "it just works for us!" cannot be measured, neither in time or in resource as it is simply unpredictable. Software engineering is a science as much as any other fields of science, and these concepts have been studied, researched, tested, and ...
9
The real question is what would be the merit of continuing to do things the C++ way and what was the original purpose of the header file? The short answer is that the header file style allowed for quicker compile times on large projects in which many classes could potentially reference the same type. This is not necessary in JAVA and .NET due to the nature ...
8
Simply put, that is the way it was designed. You mentioned Java as a counter example - the Java language designers wanted to make classloading somewhat implicit, so they check the same directory before throwing ClassNotFound. Outside of that you need to qualify, just like in Python.
As Tom Anderson said, C does the same thing as Python, and it's a compiled ...
8
Most people already explained what singletons/abstract classes are. Hopefully, I'll provide a little different perspective and give some practical examples.
Singletons - When you want all calling code to use a single instance of variables, for whatever reasons, you have the following options:
Global variables - obviously no encapsulation, most of the code ...
8
If your person is actually not doing anything, then there is no need to have any actions on it. It is possible to have classes which only collects data and then there will be classes which operates on this data. In your case, it may be possible that you do not have any action on single person, but there may be action on group of persons, like ...
7
I think you're approaching the class with the wrong attitude. You're looking at MIPS and thinking:
Why are we learning this? It's not the most popular processor
platform. Most of the world runs on Intel chips -- why aren't we
learning that? When will I use MIPS in the real world?
I doubt that the point of the class, however, is to learn the MIPS ...
7
Well, too put it simply: nested classes do not violate encapsulation and in general, language features don't violate programming principles. Programmers violate programming principles.
Funnily enough, it is claimed nested classes increase encapsulation:
Increased encapsulation—Consider two
top-level classes, A and B, where B
needs access to members ...
7
It makes it obvious to users how the class is used. For instance, it would be complete nonsense to write the following code:
Math m = new Math();
C# doesn’t have to forbid this but since it serves no purpose, might as well tell the user that. Certain people (including me) adhere to the philosophy that programming languages (and APIs …) should be as ...
7
This is a good question! Simple and well asked. Well... the answer is no so easy to understand for a student that is studying computer science and probably does not know in deep OO and probably does not have working on experience.
So, I can answer by describing a scenario and making you imagine how multi-classes software is better than monolithic software ...
7
The difference between C++ and Java is in what the languages consider their smallest unit of linkage.
Because C was designed to coexist with assembly, that unit is the subroutine called by an address. (This is true of other languages that compile to native object files, such as FORTRAN.) In other words, an object file containing a function foo() will have ...
7
For my UML class diagrams I have not included third party classes I use e.g. observer type interfaces. Should I include them?
If they are important for understanding your system, yes.
If so how can I differentiate between my entities (classes, interfaces, enums etc) and their entities?
Perhaps a package diagram to clearly show what belongs ...
6
In my opinion this is a question of design, not performance.
If you want to prevent the class from being inherited, mark it sealed - you should only do this if you have a good reason to do so, otherwise you are potentially placing artificial restrictions on the future design of the system.
Any question of performance issues around class modifiers seem ...
Only top voted, non community-wiki answers of a minimum length are eligible

