Questions tagged [static-methods]
The static-methods tag has no usage guidance.
98
questions
0
votes
6
answers
462
views
Is a static class full of static methods a code smell?
I have to create custom buttons but in order to encapsulate the logic of button creation I've this class (VB.net):
Public Class ButtonCreator
Public Shared Function CreateBaseButton(Optional ...
0
votes
1
answer
246
views
Why exactly was the C++ convenience of not requiring a second declaration, removed?
See "Note" on second page:
This convenience gave rise to serious inconsistencies and was removed several years ago
The book was authored in 2003.
What exactly does it mean by "...
0
votes
1
answer
565
views
Android + Kotlin + Hilt: Dependency Injection vs Static Methods
I've already read this carefully, but still need more clarification.
I'm not new to dependency injection, but new to Hilt, and trying to implement Hilt in my multi-module app.
The reason?
I currently ...
2
votes
1
answer
631
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 ...
1
vote
4
answers
951
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 ...
1
vote
2
answers
312
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....
4
votes
1
answer
2k
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; ...
-5
votes
1
answer
84
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 ...
0
votes
3
answers
165
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 ...
8
votes
4
answers
5k
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)
...
2
votes
1
answer
165
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, ...
-2
votes
1
answer
651
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 <...
2
votes
1
answer
779
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 ...
6
votes
4
answers
6k
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 ...
2
votes
3
answers
539
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 ...
2
votes
1
answer
4k
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 ...
4
votes
4
answers
358
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'...
2
votes
1
answer
8k
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 ...
3
votes
1
answer
108
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 ...
1
vote
2
answers
4k
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 ...
11
votes
7
answers
3k
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(){
...
3
votes
4
answers
4k
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 ...
5
votes
2
answers
383
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 ...
8
votes
5
answers
464
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 ...
0
votes
1
answer
3k
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....
0
votes
2
answers
101
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-...
0
votes
0
answers
53
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 ...
1
vote
2
answers
6k
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 ...
6
votes
2
answers
310
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;
//...
7
votes
3
answers
17k
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;...
49
votes
2
answers
52k
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 ...
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 ...
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 ...
0
votes
1
answer
114
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 ...
1
vote
3
answers
6k
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:
...
-3
votes
2
answers
136
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 ...
0
votes
2
answers
3k
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;
}
...
4
votes
2
answers
745
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)
...
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 ...
17
votes
2
answers
32k
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.
...
6
votes
2
answers
203
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 ...
16
votes
5
answers
8k
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 ...
4
votes
3
answers
439
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 ...
0
votes
1
answer
296
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 ...
9
votes
4
answers
16k
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 ...
16
votes
1
answer
9k
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&...
-2
votes
4
answers
416
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 ...
9
votes
5
answers
16k
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 ...
8
votes
1
answer
721
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 ...
2
votes
2
answers
564
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 ...