53
votes
Accepted
Dependency Injection vs Static Methods
There is no reason why this needs to be injected. This is just a function, it has no dependencies, so just call it. It can even be static if you want as it looks to be pure. One can write unit ...
40
votes
Accepted
How can I unit test methods which are using static methods?
Then all tests of Foo.Do will depend on the success of TestToHex_Works.
Yes. That's why you have tests for TextToHex. If those tests pass, the function meets the spec defined in those tests. So Foo.Do ...
15
votes
Dependency Injection vs Static Methods
Here's why:
class DOSClient {
OrderParser orderParser;
string orderCode;
DOSClient(OrderParser orderParser, string ordercode) {
this.orderParser = orderParser;
this....
15
votes
Accepted
Should methods that are not "pure functions" and that interact with external APIs or hardware be static?
So, should such methods (and, in this case, the entire class) be static?
No. Or at least, you shouldn't use them directly as statics.
If you're working with various hardware components, you're very ...
15
votes
Accepted
CA-1822: Mark members as static Roslyn recommendation will not unnecessarily increase memory consumption?
Garbage collection deals with objects, or, in other words, instances of specific types. GC doesn't care nor deal with methods, unless you store a method as an object (such as an instance of Func<T&...
14
votes
Extension methods vs. Static Class Methods
I think extension methods are more "discoverable". If I have a certain type, Intelli-sense will automatically give me the extension methods.
When a static class and method, I have to know the name ...
12
votes
Accepted
Is it safe to return a new object from static method?
No, your first code snippet will not create a new weapon every time you reference it.
The second example you posted is an example of the singleton pattern.
Based on your comment at the bottom of ...
12
votes
Extension methods vs. Static Class Methods
The biggest advantage for me is readability.
Consider for a moment a typical LINQ statement that uses method chaining:
var total = myList.Where(x => x.num > 5)
.Select(x =>...
11
votes
Accepted
Is separating most classes into data field only class and method only classes (if possible) a good or an anti-pattern?
You have shown two extremes ("everything private and all (maybe unrelated) methods in one object" vs. "everything public and no method inside the object"). IMHO good OO modeling is ...
10
votes
Is separating most classes into data field only class and method only classes (if possible) a good or an anti-pattern?
Late answer but I can't resist.
Is X most classes into Y good or an anti-pattern?
In most cases, most rules, applied without thinking, will mostly go horribly wrong (including this one).
Let me tell ...
10
votes
Accepted
Why Named Constructors are getting popular shouldn't be an antipattern?
Named Constructors in PHP
Don't limit yourself by PHP's single constructor. Use static factory methods.
The article is not about coupling. There is zero difference in coupling between using new ...
10
votes
Accepted
In C++, If a member function can be made static with no change to functionality, are there any performance or memory benefits from doing so?
Practically in 99.9% of cases: no.
Theoretically: maybe. You won't be passing the implicit this parameter to every function, and not passing that could save you bytes and the time passing those bytes. ...
9
votes
Extension methods vs. Static Class Methods
There is no industry standard here about whether to use extension methods or static classes. Honestly, "extension methods" are in static classes. This is a case where the language designers of C# ...
8
votes
Why isn't there a static initializer in Python?
... why isn't there a built-in solution for static initialization in Python?
There is, you just put static initialization stuff in the class definition, i.e.
>>> class myclass:
... print ...
8
votes
Is it a good idea to put in a bunch of static functions that are related to each other in terms of scope inside a class?
This is exactly what namespaces are for, and PHP has got them! The procedures can be freestanding and still not global as long as you give them their own namespace.
You're now using static methods on ...
8
votes
Accepted
What's the alternative to trying to inherit static methods?
The core issue you are facing is object creation/initialization. There are several standard patterns for dealing with this:
Inversion of control - use a framework to construct the objects for you.
...
7
votes
Accepted
If a method in class A needs extra dependency but not every client uses it, should I move it out of A?
The answer here is dependent on what the methods themselves are doing. With a name like loadDataFromDB() it seems that your are actually loading the object from the database. If that's the case, you ...
6
votes
Accepted
Using static factory methods to prevent unwanted input
This seems like a perfectly good use of static factory methods.
The requirement that a international student should have documents, but a domestic student shouldn't, does make me question why you ...
6
votes
Accepted
Public static method calls private constructor
(Answer rewritten in light of the comment, "Sorry, I should've mentioned that I made a very simple example to help show the main design element that confused me. The InsertAudit method would call ...
6
votes
Accepted
Workaround for no methods in namespace in C#
Just using a static method is the way to go here. If your function “belongs” to some existing class you can declare it as a public static method there. If it should be separate, you can put the method ...
6
votes
Extension methods vs. Static Class Methods
The big gain for extension methods is that they allow you to add extra functionality to .Net Framework or 3rd party classes that you don't have the source code for or can't otherwise modify.
For ...
6
votes
Why exactly was the C++ convenience of not requiring a second declaration, removed?
What exactly does it mean by "Serious inconsistencies"?
I don't know what the author meant, but static variables (in classes) need to be assigned to one and only one compilation unit. One ...
5
votes
Accepted
How to efficiently access public static variables/methods of the correct derived class?
Trying to access the correct static members of derived classes indicates that your current model is not a good fit for the C++ language. Consider more carefully why you need this approach, and whether ...
5
votes
Accepted
Is it ok to extend utilities?
I don't think there's any justification for extending a util class like that - or even if extending has any technical meaning for a static class with static utils.
Let's say you do create your own ...
4
votes
Is it safe to return a new object from static method?
public static final Weapon Alondite = new Weapon("Alondite", 16);
Then calling the weapon anywhere in the project, like this Weapon.Alondite, will this create new object every time the static method ...
4
votes
Accepted
Singleton pattern without static
A singleton pattern implementation such as:
public final class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton ...
4
votes
Calling a static method from constructor's member initializer list
You said in your text that CreateSkyBox () was private, but its not declared private.
There is absolutely nothing wrong with having static 'helper' functions in a class, which are used to help with ...
4
votes
Public static method calls private constructor
This may be an example of the factory pattern.
You can see small, but significant differences between your implementation and the current implementation: In your implementation it is entirely ...
4
votes
Should methods that are not "pure functions" and that interact with external APIs or hardware be static?
The conflicting pieces of advice you've read all make sense in a way, but they all make (different) assumptions or ambiguously phrase things. It's one of those "saying the same thing with different ...
4
votes
Should I replace a constant with static methods, if that constant usually 'cooperate' with a specific operator?
If you mean to define a transformation, you should definitely make a function rather than a number.
I would even say your original name is wrong, it should rather be G_IN_KG
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
static-methods × 101object-oriented × 30
java × 21
c# × 20
php × 9
c++ × 7
methods × 7
design-patterns × 6
unit-testing × 6
dependency-injection × 6
singleton × 6
static-access × 6
design × 5
class × 5
constructors × 5
object-oriented-design × 4
tdd × 4
code-quality × 4
static × 4
domain-driven-design × 3
testing × 3
interfaces × 3
inheritance × 3
functions × 3
code-smell × 3