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.

Filter by
Sorted by
Tagged with
-2
votes
1answer
50 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 ...
3
votes
1answer
40 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>...
0
votes
5answers
130 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 ...
19
votes
9answers
4k 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
2answers
105 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
2answers
140 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 ...
1
vote
2answers
174 views

Could multiple dynamic dispatch methods be bound to classes like traditional single dynamic dispatch methods?

C++ only supports single dynamic dispatch methods. Indeed, the following C++ program: #include <iostream> struct Shape { virtual void overlap(Shape* y) { std::cout << "Shape, Shape&...
0
votes
2answers
424 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 ...
2
votes
4answers
382 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
2answers
69 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
2answers
47 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
6answers
529 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 ...
0
votes
1answer
442 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
4answers
361 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 (...
5
votes
3answers
2k 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
2answers
1k 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 ...
0
votes
1answer
75 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
3answers
927 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
2answers
177 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
2answers
401 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
3answers
3k 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
1answer
1k 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
3answers
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
2answers
209 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
5answers
4k 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 ...
2
votes
4answers
488 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
1answer
295 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
7answers
7k 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
1answer
677 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
1answer
254 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
4answers
242 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
1answer
559 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)?
22
votes
5answers
20k 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 ...
7
votes
4answers
2k views

Is it a good practice to end a function's name with a preposition?

I have seen examples of methods whose name ended with a preposition applying to the first (and most often only) parameters. But other examples don't use this kind of naming convention. Which do you ...
2
votes
3answers
712 views

Java String substring() and StringBuilder delete() methods

I've noticed that some methods like the String's substring(int beginIndex, int endIndex) and StringBuilder's delete(int beginIndex, int endIndex), use the second parameter to signify that the ...
3
votes
2answers
1k views

Are there any problems with using class variables in Java that can be accessed by any method?

So I've been coding in Java for a decent amount of time, but recently, I've started a class that cares about my coding design. In the past, if I had two methods inside a class that needed to edit the ...
0
votes
1answer
122 views

Java's equivalent of C++'s double colon when writing a text about a code [closed]

What is the Java way to say in a text that a function belongs to a specific class? For example, when writing a C++ commit message, I say TimeInterval::toString() now uses a UTC timezone I.e., I ...
1
vote
2answers
247 views

If a instance method is used in one place only, should I move that method to that place?

For example, suppose I have a ZipCode class: public class ZipCode{ public value String value; public boolean validateFormat(){ ... } public otherMethod1(){ ... }...
4
votes
3answers
1k views

Is 2 methods calling each other code smell?

For example, if 2 classes depend on each other, it is a kind of circular dependency and should be avoided. How about methods? for example, if I have 2 methods which call each other: public void ...
6
votes
1answer
630 views

Is method with “ByXXX” suffix (eg:findUserById) redundant?

Sometimes I can see method definition like: public User findUserById(int id){ } But my justification is, isn't "(int id)" already include the information of "ById"? is it better to have naming just ...
1
vote
2answers
120 views

My implementation accepts only URLClassLoader, may I be less restrictive in my method signature?

I wrote a Java API that accepts ClassLoaders as parameter, even though it won't accept any ClassLoader that isn't a URLClassLoader (because I'm only interested in the URLClassLoader::getURLs() method):...
3
votes
4answers
1k views

Method naming with an out parameter

What naming convention should use when I have a method with an out parameter. I want to do this using clean code principles. So is it a good practice to include the name of the out parameter in the ...
0
votes
1answer
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....
43
votes
7answers
6k views

Is using parameter names that differ from type names only by casing considered a bad practice in C#?

I see questions similar to this with regards to parameter names that match properties on the class, but I can't find anything regarding using a parameter name that is the same as the parameter type ...
1
vote
3answers
194 views

object1.action(object2) vs object2.action(object1)

I apologize for the potentially misleading and ambiguous title; I've tried to generalize it as best as I can. I am currently working on an OpenGL project in C++. I wanted to wrap a couple of OpenGL ...
9
votes
5answers
2k views

Long method refactoring: leaving as is vs separating into methods vs using local functions

Suppose I have long method like this: public void SomeLongMethod() { // Some task #1 ... // Some task #2 ... } This method doesn't have any repetitive parts that should be moved to ...
1
vote
3answers
284 views

How to define sound playing function? one single function for all sounds? or each sound has its own function?

After many years, I still unable to determine the correct way to define a class/function to play sound. Suppose my app may need to play some sounds, for example: button click, error popup,... How ...
3
votes
5answers
2k views

API “initialize” method: POST or GET?

We faced an issue with an API "init" method, and trying to understand if we must use GET or POST http method in context of REST. Preconditions: SPA on the frontend which communicates with our backend ...
6
votes
2answers
295 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; //...
3
votes
5answers
3k views

What's the general option on passing a boolean parameter in a method or constructor in OOP languages? [closed]

I came across this issue at my second job interview. The technical interviewer said multiple times that booleans are not ok to be passed as parameters in methods, rather find another constructs (Enums)...