Tag Info

Hot answers tagged

73

It depends on whether the static classes maintain state or not. Personally, I have no problem with stateless functions thrown together in a static class.


63

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 ...


57

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 ...


54

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 ...


41

The characteristic of being static is independent of the visibility. The reasons that you will want to have a static method (some code that does not depend on non-static members) will still be useful. But maybe you don't want anyone/anything else to use it, just your class.


36

CRUD is usually the technical term that is used to describe create/read/update/delete functionality. ...create, read, update and delete (CRUD) (Sometimes called SCRUD with an "S" for Search) are the four basic functions of persistent storage. Sometimes CRUD is expanded with the words retrieve instead of read, modify instead of update, or destroy instead ...


32

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 } }


26

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 ...


25

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 ...


25

The answer implied by the concept of classes is "no". Either whatever action, data or relation you're handling is part of all subclasses - then it should be handled in the superclass without checking the actual type. Or it applies only to some subclasses - then you'd have to perform run-time type checks to do the right thing, the superclass would have to ...


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 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 ...


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 ...


18

In Item 5, of Effective Java, Joshua Bloch says The lesson is clear: prefer primitives to boxed primitives, and watch out for unintentional autoboxing. One good use for classes is when using them as generic types (including Collection classes, such as lists and maps) or when you want to transform them to other type without implicit casting (for ...


17

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 ...


17

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 ...


17

A fairly common reason (in Java) would be for initializing immutable field variables in a constructor by using a simple private static method to reduce constructor clutter. It is private: external classes should not see it. It is static: it can perform some operation, independent1 of the state of the host class. A somewhat contrived example follows... ...


16

Many functional languages have a module system. (Many object-oriented languages do too, by the way.) But even in the absence of one, you can use functions as modules. JavaScript is a good example. In JavaScript, functions are used both to implement modules and even object-oriented encapsulation. In Scheme, which was the major inspiration for JavaScript, ...


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 ...


15

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 ...


15

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 ...


15

There's nothing wrong with using a non-owning pointer to someone who points back to you. Just make sure it's not owning.


14

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 ...


14

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 ...


13

Short version: C was before OOP's time, so it wasn't a design choice. Object oriented programming, OOP, is an old idea. But as a cultural phenomenon it only took off in the 80's and 90's. As someone born in the 80's I'd say it was C++ that launched the hype, set the trend, started the buzzword, or otherwise got OOP off the ground. That turned it from an ...


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

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 ...


12

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 ...


12

How many dimensions are needed? C++ Template programming may require some code duplication for each level of higher dimension. Address calculation is the easy part. A simple approach can be used for dimensions up to a dozen. For a 3-dimensional example: Let the size of the array be [m, n, p] For each dimension, we calculate a "weight vector" by ...


12

Not only should it not know, it simply can't! Usually, a class can be extended anytime, anywhere. It can be extended by classes that didn't even exist when it was written. Some languages allow extending classes to be controlled by the superclass. In Scala, a class can be marked as sealed, which means that it can only by extended by other classes within the ...



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