Skip to main content
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 ...
Jon Raynor's user avatar
  • 11.8k
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 ...
David Arno's user avatar
  • 39.6k
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....
candied_orange's user avatar
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 ...
Telastyn's user avatar
  • 110k
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&...
Arseni Mourzenko's user avatar
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 ...
Jon Raynor's user avatar
  • 11.8k
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 ...
Zymus's user avatar
  • 2,553
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 =>...
Becuzz's user avatar
  • 4,865
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 ...
Doc Brown's user avatar
  • 221k
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 ...
candied_orange's user avatar
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 ...
Caleth's user avatar
  • 12.4k
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. ...
Philip Kendall's user avatar
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# ...
Greg Burghardt's user avatar
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 ...
esoterik's user avatar
  • 3,937
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 ...
kqr's user avatar
  • 379
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. ...
DavidT's user avatar
  • 4,647
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 ...
sunrize920's user avatar
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 ...
Pete's user avatar
  • 3,231
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 ...
David Arno's user avatar
  • 39.6k
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 ...
amon's user avatar
  • 136k
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 ...
Peregrine's user avatar
  • 1,246
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 ...
Doc Brown's user avatar
  • 221k
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 ...
amon's user avatar
  • 136k
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 ...
Avner Shahar-Kashtan's user avatar
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 ...
candied_orange's user avatar
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 ...
candied_orange's user avatar
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 ...
Lewis Pringle's user avatar
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 ...
CharonX's user avatar
  • 1,740
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 ...
Flater's user avatar
  • 59.5k
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
max630's user avatar
  • 2,605

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