Questions tagged [methods]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
2
votes
1answer
97 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
2k 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
200 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
391 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 ...
1
vote
1answer
83 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 ...
14
votes
7answers
6k 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
338 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
128 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
205 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
143 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)?
8
votes
5answers
3k 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
642 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
407 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
475 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
116 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
236 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(){ ... }...
3
votes
3answers
651 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
475 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
114 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
653 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
976 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
185 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 ...
8
votes
5answers
1k 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
233 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 ...
2
votes
5answers
1k 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
293 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
389 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)...
3
votes
5answers
427 views

Does my code violate DRY principle?

Suppose that I have two services Person Service and Company Service and I want to maintain links between them for example a Person is linked to Company because he works there or he owns the company ...
0
votes
2answers
142 views

Java - Access to methods of objects fields

I am designing a role playing game. In such games there is a character that has a level. I found out that the level itself is an object. It has values like experience, the knowledge of how moch ...
7
votes
6answers
819 views

How to name a method which may or may not perform an action depending on a condition?

I stumble across this case somewhat often, and I'm surprised about finding so few similar discussions around the web. This question is very related, but my problem is that I want a method that does ...
4
votes
2answers
371 views

Identify methods on OOP

I am struggling to define methods in OOP. Currently I am practicing with this scenario: "A hospital has started the development of a new system to keep records of analysis done by patients and the ...
2
votes
4answers
10k views

When to call the constructor and when to call the method in Java?

I am struggling to fully understand the usage of constructors in Java. What I have learned so far about constructors is the following: same name as class abbreviation ctor overloading no return type ...
22
votes
6answers
5k views

Is it considered bad software engineering practice to always use “public” methods? [duplicate]

I've always used public methods and recently been advised by one of my friends to always avoid defining methods as public, as far as possible, though I have worked in any commercial company I have ...
0
votes
3answers
727 views

Why are no parameters better than few parameter methods?

In Clean Code, Robert C. Martin states that no parameter methods are better than those with very few parameters. I'm kind of confused as no parameter methods are usually harder to unit test and tend ...
0
votes
0answers
55 views

Is making method parameters final a worthy endeavour? [duplicate]

It is generally a good idea to restrict access to your variables as much as reasonably possible. For example make things private instead of publicand make variables final if they aren't supposed to ...
0
votes
1answer
3k views

Object constructors with dynamic parameter lists

I had a quick question and I was hoping someone could help me figure this out. I'm new to Java and I'm trying to learn about classes and objects and I see you can call parameters in the constructor of ...
-4
votes
1answer
1k views

How to create a method that takes array list as argument?

To create a method that takes an array as an argument, you would do this: public static void printAges(int[] age) { // prints ages to screen for (int x = 0; x < age.length; x++) { ...
1
vote
1answer
92 views

Should I extract method to reduce duplication if the resulting method's functionality will vary based on type of collection given to it?

I am designing a treeNode class. While implementing the getPathToParentNode() and getPathFromParentNode() methods, I noticed that their functionality is essentially the same - they just operate on ...
1
vote
5answers
243 views

Dedicated method vs method with parameter

Suppose we've a permissionManager which tell us if user has some privileges to some action or not. We've quite a lot of them(dozen, maybe more). I see two ways to implement checks for a user, like a: ...
2
votes
2answers
203 views

Strategy for implementing Multiple Dispatch

This is a question regarding how Multiple Dispatch works. Suppose that we have a type hierarchy like this: Drawable -> Shape -> Polygon -> Rectangle And there are three functions (This is ...
2
votes
2answers
451 views

Is a procedure's complexity a function of how many nested loops it has?

Take this example public static boolean uniqueNumbers(int[] x){ for(int i = 0; i <x.length; i++){ for(int j = 0; j <x.length; j++){ if(i != j && x[i] == ...
-1
votes
1answer
273 views

GO - How to define methods of named type?

In GO, rule is, methods can be defined only on named type and pointer to named type. In C, below code, operations are defined on type(say List), typedef struct List List; //list.h typedef struct { ...
6
votes
3answers
202 views

Are we “allowed” to use methods on JavaScript maps (the data type) and if so are there any new rules?

JavaScript maps (the data type, not the array method) seem set up to accept data (key/value pairs) but not necessarily methods. At least they're not advertised that way. However, we can put methods ...
10
votes
7answers
4k views

Why is cyclomatic complexity that important for a single method?

I am using SonarLint for Eclipse since recently, and it helped me a lot. However, it raised to me a question about cyclomatic complexity. SonarLint considers as acceptable a C.C of 10, and there are ...
1
vote
1answer
270 views

What native JavaScript functions/methods/properties/operators can return null? undefined? [closed]

I'm writing some code that needs to deal with many potential JavaScript values including so-called non-values . I'm realizing it would be very helpful to know if there are some general rules about ...
10
votes
3answers
2k views

In Java 8, is it stylistically better to use method reference expressions or methods returning an implementation of the functional interface?

Java 8 added the concept of functional interfaces, as well as numerous new methods that are designed to take functional interfaces. Instances of these interfaces can be succinctly created using ...
8
votes
2answers
4k views

Why does Java use :: for method references instead of .?

I often wonder why Java uses :: for method references instead of ., e.g. why write System.out::println instead of System.out.println Of course, one might simply answer: "because the designers ...
2
votes
1answer
226 views

In a JavaScript method signature what is meant by a return type of `typeof blahBlahBlah`?

I've seen an API list methods with both of the following signatures: methodA(...) : ReturnType methodB(...) : typeof ReturnType I understand the first but not the second. My question comes ...