Questions tagged [methods]
A method is a procedure that is associated with a particular object. The purpose of the method is to guide the behavior of the object, and the tag should be used when this is the case.
206 questions
-3
votes
2
answers
289
views
What to name a method which reads and sets value without return value?
I have a method like this
private void foo() {
this.myValue = readSomeValueFromFileSystem()
}
What is the good name for that ? Is there any convention about it ?
I feel that it is kinda set but ...
17
votes
8
answers
5k
views
When should a function be given an argument vs getting the data itself?
When is it better to pass data to a function in a parameter, and when is it better for the function to just fetch the data itself?
Here's some simplified examples in PowerShell:
Option 1 (give the ...
6
votes
4
answers
3k
views
How do you honour the principle to only do "one thing" in a method in reactive streams?
Uncle Bob mentions in his book "Clean Code" that "[f]unctions should do one thing [...] only" and advocates that a method should stick to one abstraction level (I'd call it flight ...
1
vote
2
answers
226
views
What is an appropriate length for function names? [closed]
I'm writing a C++ class, CBookSpellDef which has the following methods:
int GetIndexOf(const char *psz);
char* GetNameAtIndex(int index) { return m_spellTypes[index].szName; }
char* ...
-4
votes
1
answer
93
views
Is it possible a class method have the same name as an existing function? [closed]
Courtesy link:
What Are The Specific Meanings Of The Terms: Functions, Methods, Procedures, and Subroutines?
What's the difference between a function and a method?
In the linked question:
...
13
votes
4
answers
5k
views
Is there a clean way to model methods that only make sense depending on the current state of the object?
Let's say that I have a class called Mission. This class is intended to represent a mission requested by a professor working in a faculty. Mission has a private enum field representing whether the ...
1
vote
2
answers
547
views
For more than one parameter, when NOT to introduce parameter object?
I know there are some questions about "Introduce parameter object", eg: Is "Introduce Parameter Object" actually a good pattern?, Should we avoid custom objects as parameters?, ...
1
vote
1
answer
826
views
Should I use private field or static variable in my method?
There is a variable that must persist between calls but otherwise only one method uses it.
As it is a method of the object, the value can be persisted as a private field of the object, or otherwise it ...
-1
votes
1
answer
107
views
Most relevant objectively-quantifiable reason to choose to use an object method vs. a function that just accepts the object as a parameter? [closed]
When writing code in a programming language that has the option of creating standalone functions vs. methods of a class or struct, what is the most relevant objectively-quantifiable reason to choose ...
10
votes
8
answers
4k
views
What is the purpose of enclosing all return values and arguments of a method in separate classes?
I've seen such a convention. Whenever a public method is declared, two classes are also defined that enclose its return value and parameters like this:
public MethodNameReturnDTO MethodName(...
1
vote
2
answers
457
views
In what language does a method not return a value and a function does?
I don't know from where I got this but in my head a function returns a value and a method does not.
I know that with OOP a method is a function related to a class. But I am trying to remember where I ...
-2
votes
1
answer
301
views
Is it recommended practice to put a "slash" after the self parameter in Python methods?
With the introduction of the slash character to specify position-only arguments, is it recommended to put a slash in every method after the self parameter?
For example:
class A:
def something(self,...
0
votes
1
answer
153
views
Different gateway method implementations
Sometimes I need to use the same use case with different details implementation. It is easy and straightforward for presenters as each use case has its own output port and therefore different ...
-1
votes
1
answer
1k
views
Are there different ways in Python to decorate class methods that dynamically assign themselves to a dict within the class?
Here's what I'd like to do in the form of working code, since it's difficult for me to explain otherwise:
from typing import Callable, Generic, TypeVar
from typing_extensions import Self
# The type ...
-1
votes
1
answer
387
views
What should be a method prefix that makes calculation based on the parameter [closed]
I have a method, that takes a list of students, and returns the student with the best score.
Part of me wants to call it Student getBestStudent(List<Student> students), but I think that get ...
2
votes
5
answers
1k
views
Class method Vs. Interface?
I am relatively new to C# and OOP concepts in general, but am building a standalone application and have run into a question and want to make sure I'm doing it the "right way".
I have a few ...
0
votes
2
answers
112
views
Calling general-purpose methods from the code that clearly needs only specific behavior
Here are a couple of examples in Python:
clearly_even = 2 * get_integer()
print(solve_for_any_integer(clearly_even))
def solve_for_any_integer(x):
while x % 2 == 1:
x = make_even_from_odd(x)
...
-2
votes
1
answer
141
views
How are methods called that prepare/alter some data and then pass it on
I am currently trying to name a method that prepares/alters some input data, then passes it on, and I'm wondering if there is a naming scheme for such methods.
Concretely, I have a reusable UI ...
4
votes
1
answer
83
views
UML v2.5.1 correct notation of blanks in operations (methods)?
In the UML specification 2.5.1 (Link) on page 117 it is specified that the notation of operations (methods) should look like the following:
[<visibility>] <name> ‘(‘ [<parameter-list>...
1
vote
5
answers
403
views
Is this a proper use of overriding according to LSP?
I have a abstract class named MotorizedVehicle that contains an implemented gas- and brake-function. I want to make a Truck class that extends this class and uses gas exactly in the same way as ...
18
votes
9
answers
5k
views
Which object should have the method?
I am trying to create an object model for a user and a chatroom. I'm stuck on where to place certain functionality when the objects collaborate.
At the moment all the functionality for the User is ...
0
votes
2
answers
151
views
Where to place methods that consume multiple object collections
We've been battling over where certain methods should live within our domain model, so looking for some adice and reasoning as to where they should go.
Say we have a Project object now that Project ...
1
vote
2
answers
155
views
Going from Javascript to C++. I have teacher and student objects, and I want to display if they are teachers or students. How should I do this?
This may be too general of a question, but basically whenever I try to find an answer for something that would work from a Javascript approach, the answer heavily implies that you wouldn't do it that ...
2
votes
2
answers
766
views
Are multiple dynamic dispatch methods possible?
C++ only supports single dynamic dispatch methods. Indeed, the following program:
#include <iostream>
struct Shape {
virtual void overlap(Shape* y) { std::cout << "Shape, Shape\n&...
0
votes
2
answers
4k
views
Terminology: Classes that contain only methods [closed]
I am studying the programming language Kotlin, and I just came across the idea of a Data Class. I have a background in Java programming where classes can have fields and methods. I have heard ...
6
votes
4
answers
2k
views
What does "side-effect" mean in the context of "Idempotent HTTP methods may not have side-effects when repeated multiple times"?
HTTP has safe and idempotent HTTP-methods.
Idempotence in HTTP is not exactly the same as idempotence in mathematics, the definition states:
Methods can also have the property of "idempotence&...
-3
votes
2
answers
71
views
What is the common design to return processed value and message to the user? [duplicate]
When a method is called and must return a value for further processing, but for instance a message has also to be provided to the UI : what is the best or most common way to deal with those 2 outputs ...
1
vote
2
answers
195
views
What does this statement about implementing forwarding methods of components mean?
I can't wrap my head around this statement here:
One common drawback of using composition instead of inheritance is
that methods being provided by individual components may have to be
...
9
votes
6
answers
2k
views
How do you decide if a parameter should go to the constructor of the method it relates to?
I use Python but I guess my question applies to OOP in general. Whenever I create a class I am never sure whether I should put a parameter/attribute in the constructor or in the method the parameter ...
1
vote
1
answer
1k
views
Why does php not allow to decrease visibility of class properties and methods in the inheriting class?
Please consider the following code:
class baseclass {
public $hideme;
public function getit() { return $this->hideme; }
public function setit($value) { $this->hideme = $value; }
}
...
4
votes
4
answers
366
views
Grouping of methods
We have multiple pages searching for users, each site having different search parameters. Sometimes, we have 2 parameters, sometimes 4 and most of these parameters overlap. So we have kind of (...
9
votes
3
answers
8k
views
What does it mean for a method or a function to do one thing? [duplicate]
What does it mean when you say that a method or a function should do only one thing?
Can it do a few things as long as it has a cohesive name?
Should we avoid routines with the word "and" in them?
...
4
votes
2
answers
3k
views
Is there anything wrong with writing getter/setter methods in C#? [duplicate]
I am a Java dev for almost all of my programming (at least in the workplace) but I do some Unity for fun on the side. I have used C# properties many times and they are convenient to still provide ...
1
vote
1
answer
1k
views
Should List(Get) and Search be the same endpoint API (method)?
Here are two endpoints (or methods):
ListResources(user_id) - Fetch the resources based on the given user id.
SearchResources(query_options) - Fetch the resources with specified conditions.
The ...
3
votes
5
answers
10k
views
Java: Why not allow nulls in methods to represent optional parameters?
I wanted to follow up on this previous question I asked related to @Laive comment, but I couldn't think of an excellent way to do so without asking another question, so here we go.
With the previous ...
2
votes
2
answers
216
views
Is there a guideline as to when I should pass a collection as an argument or return a new collection?
Suppose I have the following methods:
def read(file: str) -> List[str]:
temp = []
with open(file) as f_obj:
for line in f_obj:
temp.append(line)
return temp
def ...
7
votes
2
answers
2k
views
Why is it necessary to "import" a library first before usage?
My question regards the use of import statements.
In most programming languages I've come across (e.g. Python, Scala, Go), it is necessary to first import a library before you can use its functions.
...
6
votes
3
answers
12k
views
Write a method with same logic but deals with different objects
Assume I have the following method that does the same logic but deals with different objects (It's more a pseudocode):
private <E> List<E> updateOrInsert(List<E> list) {
if ...
2
votes
1
answer
2k
views
UML - Changing the visibility of operations when overriding them
I am trying to find out if it is allowed in the UML to change the visibility (access modifier) of an operation when overriding it. For example, in Java it is possible to increase the visibility of an ...
12
votes
3
answers
3k
views
Working through the single responsibility principle (SRP) in Python when calls are expensive
Some base points:
Python method calls are "expensive" due to its interpreted nature. In theory, if your code is simple enough, breaking down Python code has negative impact besides readability and ...
4
votes
2
answers
216
views
Should I define more methods to update necessary UI only, or less methods but update some UI unnecessarily?
For example, suppose my input data and UI is not in 1 to 1 relationship:
html:
<script>
aChanged=function(){
};
bChanged=function(){
};
cChanged=function(){
};
</script>
a:<input id=...
15
votes
5
answers
5k
views
Pass object twice to same method or consolidate with combined interface?
I have a method that creates a data file after talking to a digital board:
CreateDataFile(IFileAccess boardFileAccess, IMeasurer boardMeasurer)
Here boardFileAccess and boardMeasurer are the same ...
1
vote
4
answers
834
views
Can method names give any implementation details and break encapsulation?
I was reading here about OOP and methods, and the accepted answer states that method names should be verbs. However, that doesn't really answer my question.
Suppose if I had a Character class with a ...
2
votes
1
answer
993
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 ...
13
votes
7
answers
9k
views
Is it a bad idea have make a class method that is passed class variables?
Here's what I mean:
class MyClass {
int arr1[100];
int arr2[100];
int len = 100;
void add(int* x1, int* x2, int size) {
for (int i = 0; i < size; i++) {
x1[i] +...
0
votes
1
answer
2k
views
C# Unit Testing: Mock a Return Type
What is the best way to Mock a return type if we don't care about the actual state of the object.
For example, I am Mocking a method whose return type is: IEnumerable<Document>
Now, I can add ...
0
votes
1
answer
313
views
Parameters vs Specialized functions
Trying to figure out which structure is cleaner between using a specialized function or using parameters to accomplish the same thing (see code example below).
Essentially, I can reduce the number of ...
0
votes
4
answers
836
views
Preferred method of accessing an external class of methods through the original class
I want to move a bunch of similar methods to an external class. The class is initialized with the original class instance. From there I can access it either by property (persistent instance) or by ...
1
vote
1
answer
2k
views
Accepted term for method that simply calls another method
Is there a generally accepted term for a method that does nothing more than calling another method (and returning its result)?
40
votes
6
answers
53k
views
Why is unit testing private methods considered as bad practice?
Context:
I am currently working on a small project in Python. I commonly structure my classes with some public methods that are documented but mainly deal with the high level concepts (what a user of ...