Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [static-methods]

The tag has no usage guidance.

-2
votes
1answer
52 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 <...
1
vote
1answer
78 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 ...
2
votes
4answers
308 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
3answers
290 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 ...
1
vote
1answer
146 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
4answers
307 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'...
0
votes
1answer
288 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
1answer
94 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
2answers
968 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 ...
10
votes
6answers
896 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
4answers
1k 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
2answers
250 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
5answers
382 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
0answers
32 views

How to unit test an SDL based frontend? Is it worth it? [duplicate]

I am currently beginning to create a small program/game in my free time and decided to try out SDL2 for my frontend/graphics code. I use the bindings from SDL2-CS, which is API-wise very close to the ...
0
votes
1answer
699 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
2answers
85 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
0answers
49 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
1answer
878 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
2answers
291 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; //...
4
votes
3answers
5k 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;...
12
votes
2answers
11k 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 ...
2
votes
3answers
1k 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 ...
1
vote
2answers
638 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
1answer
92 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
votes
2answers
771 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
2answers
122 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
2answers
1k 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
2answers
237 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
1answer
742 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 ...
7
votes
2answers
6k 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
2answers
144 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 ...
13
votes
5answers
3k 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
3answers
422 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
1answer
133 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 ...
10
votes
4answers
8k 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 ...
12
votes
1answer
4k 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
4answers
235 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 ...
8
votes
5answers
8k 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
1answer
413 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 ...
3
votes
2answers
425 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 ...
1
vote
1answer
115 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 ...
1
vote
3answers
452 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 ...
1
vote
2answers
880 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 ...
3
votes
1answer
659 views

Static properties and implicit “self” property in structures and enumerations vs classes in Swift

I am currently reading the Swift language documentation and came across these sentences in the chapter about methods: Similarly, type methods on structures and enumerations can access static ...
-1
votes
4answers
320 views

Is static going to make any difference in this code snippet

I have been to an interview and was asked this question - is there any difference adding or removing the static keyword in these classes? I know what static means but my understanding of this point ...
5
votes
1answer
1k views

How can I use the “Non-Member Functions Improve Encapsulation” pattern from C#?

In 2000, Scott Meyers argued that non-member functions improve encapsulation. In C++, a non-member function is a C-style global function: http://www.drdobbs.com/cpp/how-non-member-functions-improve-...
3
votes
2answers
173 views

Can a class method be accessed both in an instance and statically?

I am relatively new to class design and I have a task that I'm not sure how best to complete, or whether my idea in general is a code smell. I'm developing a RPG where people can own monsters, so ...
1
vote
2answers
2k views

Is using multiple static classes with maximum one public method is a good idea

I'm writing a structural detailing (CAD) software for concrete buildings in C#. I have defined like hundreds of static classes each with one public method and if needed some private methods. Each one ...
1
vote
2answers
941 views

Programming against interfaces in Java

Supposing I have an interface Foo and a given implementation FooImpl. public class FooImpl implements Foo If I want to define a new operation on this class that depends on the particular ...
0
votes
1answer
208 views

Static and not-static: programmer quantum theory

Let me illustrate using the PHP language. The discussion here is, how should I do exactly to solve this problem in a clear and unambiguous mode. Imagine that I have a class called Path. This class is ...