Questions tagged [static-methods]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
2 votes
1 answer
166 views

Static validator in DDD value objects

I have a value object to hold a user id number as a string. This number has a unique format throughout my domain. So, it's being validated inside the object during instantiation and an exception is ...
user avatar
1 vote
4 answers
197 views

Are static classes/methods good for pure business logic?

I have a service class that performs some operations. One of the operations is a piece of code long enough to warrant extracting to a new class and unit test it in isolation: @Service public class ...
user avatar
1 vote
2 answers
91 views

Mock part of a static method

I am designing a test for an existing static method: public static boolean hasPermissions(String username, int pageid) { PermissionsService s = new PermissionsService(); int[] pages = s....
user avatar
  • 31
3 votes
1 answer
521 views

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?

A very straightforward question. When I think about many of the member functions I create for my classes, many of them can be made static without affecting any functionality whatsoever. If I do so; ...
user avatar
  • 3,319
-5 votes
1 answer
56 views

Should code be refactored to be more unit testable or should a framework like PowerMock be used?

Assume there's some code that's already in production that needs some unit testing. Generally speaking, would you want to refactor this code that's already in production by adding things like adding ...
user avatar
0 votes
3 answers
155 views

Should I make the class static or non-static for the following case?

I have a class which has the purpose of providing file operations i.e. providing functionality to create the file, read, write and rewrite to the file. So, the main constituent of the class is ...
user avatar
8 votes
4 answers
3k views

How can I unit test methods which are using static methods?

Let's assume I wrote an extension method in C# for byte arrays which encodes them into hex strings, as follows: public static class Extensions { public static string ToHex(this byte[] binary) ...
user avatar
  • 231
2 votes
1 answer
133 views

Changing static method that now requires a runtime dependency

I had a simple static method which would take a value and a unit type (pounds, gallons, dollars) and would just format a string. A simplified example: public static string Format(decimal value, ...
user avatar
-2 votes
1 answer
469 views

Using static member methods to check for object being NULL

Is it a good practice to use the static member methods to check if an object of a class is NULL or not. The object would be sent through the parameters offcourse. Something like, #include <...
user avatar
  • 105
2 votes
1 answer
479 views

How to efficiently access public static variables/methods of the correct derived class?

I have an arbitrary number of derived classes all inheriting from the same base class. These derived classes all have the same static variables and static methods, although the implementations may ...
user avatar
5 votes
4 answers
4k views

Extension methods vs. Static Class Methods

I've taken on a Visual Studio C# project where my previous colleague used a lot of Extension Methods in static classes over multiple files depending on the uses. In my previous experience I would be ...
user avatar
2 votes
3 answers
331 views

Workaround for no methods in namespace in C#

I am new to C# and have a difficulty with the code structure. My application has: GUI with button Show my Ethernet IP. When user clicks this button PC's IP address is displayed. Class ...
user avatar
  • 131
1 vote
1 answer
3k views

c# What problem will happen when i use static class for access database?

Many place that i have read about static method they said please shouldn't use static method for access to database and should use dependency injection for inject other service and use that service to ...
user avatar
  • 131
4 votes
4 answers
343 views

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?

The language is PHP. I have several functions that relate to each other (and sometimes use each other) that I've decided to couple under the same class. Here are my reasonings, do keep in mind that I'...
user avatar
  • 641
2 votes
1 answer
6k views

Why isn't there a static initializer in Python?

The equivalent of static { // Code here } does not exist in Python by default. The closest thing, while being "Pythonic", seems to be to create a decorator and do the initialization there such ...
user avatar
  • 1,902
3 votes
1 answer
103 views

Should I replace a constant with static methods, if that constant usually 'cooperate' with a specific operator?

For example, to convert between g and kg, I have a constant 1000: public static final float G_TO_KG=1000; . . . this.result = someResult*1000; I found G_TO_KG always bind to operator '*'. So my ...
user avatar
  • 5,572
1 vote
2 answers
3k views

Calling a static method from constructor's member initializer list

I'm implementing a simple rendering system for a game engine. In my engine I have renderable entities that have a Model component (I'm using inheritance as opposed to a ECS for my engine for now, but ...
user avatar
  • 181
11 votes
7 answers
2k views

Is separating most classes into data field only class and method only classes (if possible) a good or an anti-pattern?

For example, a class usually have class members and methods, eg: public class Cat{ private String name; private int weight; private Image image; public void printInfo(){ ...
user avatar
  • 5,572
3 votes
4 answers
3k views

Public static method calls private constructor

I'm working in a C# codebase that was largely written by a former developer and this pattern is used extensively... public class AuditInserter { public static void Insert( DataContext ...
user avatar
  • 147
5 votes
2 answers
318 views

Using static factory methods to prevent unwanted input

Many years ago, I posted a problem I had with some code and received a well written detailed answer that suggested the use of factory methods. I liked this approach, because I can provide a method ...
user avatar
8 votes
5 answers
422 views

Should methods that are not "pure functions" and that interact with external APIs or hardware be static?

When reading about when to make a method static or not, I've seen a general principle, as summarized by this post, that a method should only be static if it does not modify a state and its result ...
user avatar
0 votes
1 answer
2k views

Should I avoid long method chaining simply to get a variable? [duplicate]

I am working on a game, and I have many instances where I have to do something like this: Game.getInstance().getEnergyDropManager().getDrops() Or: this.dimensionSelector.selectDimension(Game....
user avatar
  • 163
0 votes
2 answers
98 views

Inside a non-static non-virtual method, if it just use a few non-static members, should I turn it into static methods with parameters?

According to Are there guidelines on how many parameters a function should accept?, it is ok for a method to have few parameters. According to https://stackoverflow.com/questions/11240178/what-is-...
user avatar
  • 5,572
0 votes
0 answers
52 views

What's the value in marking static methods in a non-static class [duplicate]

I had a recent code review that gave me some slightly surprising feedback. It was in an instantiable service class that had a number of fully encapsulated private methods. In other words, methods that ...
user avatar
  • 3,516
1 vote
2 answers
5k views

Singleton pattern without static

I heard that static (in the Java sense, basically a static method is called on the class itself and not on an instance) is not True OOP. However, how would the Singleton pattern be implemented in such ...
user avatar
6 votes
2 answers
301 views

If a method in class A needs extra dependency but not every client uses it, should I move it out of A?

For example, suppose I have a class like that: public class PlayerInfo{ public loadDataFromDB(){ //some code about DB framework } public string name; public int age; //...
user avatar
  • 5,572
7 votes
3 answers
15k views

Is it safe to return a new object from static method?

Is it OK to have a class something like this public class Weapon { private string name; private int might; // etc... private Weapon(String name, int might) { this.name = name;...
user avatar
  • 271
37 votes
2 answers
41k views

Dependency Injection vs Static Methods

I had an interesting discussion today with another developer about how to approach a class with a method that accepts a string and outputs string. Imagine something like the following which is ...
user avatar
  • 1,639
4 votes
3 answers
3k views

Using static to create an immutable object

I was reading on this software engineering page about the use of static methods. However, I'm confused, and maybe it's about the context in which it talks about static. The idea is, that static is ...
user avatar
5 votes
3 answers
2k views

Why Named Constructors are getting popular shouldn't be an antipattern?

According to following article Named Constructors The Author suggests using static factory pattern to construct objects is way better than instantinate with new keyword. At the begining the idea is ...
user avatar
  • 469
0 votes
1 answer
98 views

Is there a better way than StringHelper?

I want to have functions that convert numeric values into strings in English words, to pluralize nouns, to inflect, break by words and so on. I will use these throughout a big project. Normally I ...
user avatar
  • 922
1 vote
3 answers
4k views

In Go when to write a function with or without a receiver?

We're trying to write Go in the most possibile idiomatic way, but sometimes we struggle to find which is the best way. For example in our service we're creating/converting a struct from another one: ...
user avatar
  • 197
-3 votes
2 answers
130 views

Implementing functional/reusable code [closed]

Writing code, in my opinion, usually involves 2 kinds of code: logical and functional code. While the logical part of the code always differs between every app and its goals, often the functional ...
user avatar
0 votes
2 answers
2k views

Static methods and testability

I have an application service which returns entity by its registration number public Entity FindByRegNumber(string number) { if (!RegNumber.IsValid(number)) { return null; } ...
user avatar
4 votes
2 answers
362 views

When to use a static method instead of a constructor?

I have a short question for you: let's imagine that we have a class similar to this one. public class StreamTradeDataProvider : ITradeDataProvider { public StreamTradeDataProvider(Stream stream) ...
user avatar
  • 349
3 votes
1 answer
1k views

Handle Named constructors with factory pattern

In my current project I'm refactoring the code to get a DBAL. I have a class Entity that is the base class for all classes that model a database table. So there are several classes that inherit from ...
user avatar
  • 225
15 votes
2 answers
23k views

Unit testing utility classes

All of us have some utility classes, which contain only static methods, for usage from different sources. Now, there can be two approaches which can be taken towards testing this piece of code. ...
user avatar
6 votes
2 answers
168 views

Deterministic statics vs inject-able classes

I have in mind several deterministic functions that I'd like to put together, but I'm struggling with the full impllications of doing: Static class with static methods Instance of a class that can be ...
user avatar
  • 394
15 votes
5 answers
7k views

Why shouldn't static methods be able to be overrideable?

In answers to this question, the general consensus was that static methods are not meant to be overridden (and thus static functions in C# cannot be virtual or abstract). This is not only the case in ...
user avatar
4 votes
3 answers
435 views

What problems might arise if I didn't make a method static when I could?

I have a stateless method that takes an input, and based on that input returns an output. This method has no state so in theory it could be made static. But let's say I don't do this. What problems ...
user avatar
0 votes
1 answer
240 views

Using static methods in layered architecture

In layered architecture does it matter where static methods go? Or is it the architect deciding this? For example can the DAL be static to cache records and perform smart data retrieval at the ...
user avatar
  • 101
9 votes
4 answers
14k views

Why would passing objects through static methods be advantageous?

Why would there be an advantage to use a static method and pass the reference to an object as a parameter rather than calling the method on an object? To clarify what I mean, consider the following ...
user avatar
15 votes
1 answer
8k views

Are we abusing static methods?

A couple of months ago I started working in a new project, and when going through the code it stroke me the amount of static methods used. Not only utility methods as collectionToCsvString(Collection&...
user avatar
  • 1,527
-2 votes
4 answers
346 views

Why do we add instance methods to classes? [closed]

Rewrite of the Question: Is there a technical reason why we are not using static methods instead of instance methods. Technical reasons are for example: Performance or added type-safety. I reason ...
user avatar
9 votes
5 answers
15k views

Does making a method static save memory on a class you'll have many instances of?

In response to Aaronaught's response to the question at: Can't I just use all static methods? Isn't less memory used for a static method? I am under the impression that each object instance carries ...
user avatar
  • 229
8 votes
1 answer
639 views

Is a large static initializer a code smell?

I am extending SimpleExpandableListAdapter in Android. I don't think Android's adapter is implemented very well, in that its constructors have a large number of rather complicated arguments and it has ...
user avatar
2 votes
2 answers
560 views

Create a globally visible method for an API

I'm trying to create a Java API that I will use in other projects. I understand that if I create new classes I can make objects in the other projects that have those classes. But what I want right ...
user avatar
  • 29
1 vote
1 answer
187 views

When should I use static functions and when should I use non-static ones? [closed]

Sometimes, when I'm programming, I have to decide between adding a static method to a class that accepts an instance (or more) of that class or adding a non-static method. Here's an example: Static ...
user avatar
1 vote
3 answers
807 views

Dependency inversion without static methods?

It's clear that the dependency inversion principle and the use of interfaces makes software components less coupled and promotes maintainability. On the other hand, these principles make using static ...
user avatar
1 vote
2 answers
1k views

Using static classes to define methods for handling POCO class objects

I am pretty new to the OOP paradigm and for this current project of mine, I need to design and develop a web application backend in .NET MVC using C#. A friend of mine told me that the class objects ...
user avatar
  • 141