Questions tagged [parameters]
Parameters are important for any non trivial program, to help make it generic and data driven. Parameters are usually function arguments but can also be part of the configuration.
153 questions
-3
votes
1
answer
185
views
Is there a name for this anti-pattern? (reference to a class member passed to another class method) [closed]
Is there a name for this anti-pattern?
A reference to a class member is being passed to another class method, rather than having the class method set the class member directly.
public class ...
0
votes
2
answers
160
views
Is it better to pass a specific “context” object to handlers rather than the entire domain object?
I’m designing a system where various “handlers” apply business rules to an object. Initially I had each handler receive the full domain object:
// A large domain object with many properties and ...
10
votes
7
answers
3k
views
Is it an anti-pattern to support different parameter types when using a dynamically-typed language?
In the Python code base we inherited there are several functions that check parameter types and try to "accommodate" different types. Example:
def process_data(arg):
json_ = {}
if ...
7
votes
8
answers
946
views
Why is "one function do more than one thing" a disadvantage of "boolean parameter", but not in "polymorphism"?
According to Is it wrong to use a boolean parameter to determine behavior?, I know using boolean parameters to decide the behaviour is bad, for example, when using boolean parameters as the following:
...
3
votes
5
answers
815
views
Why is "dependency injection" ok, but not "the opposite of preserve whole object (pass required parameters only)"?
According to Why should I use dependency injection?, "dependency injection" has some advantages, for example:
"Non dependency injection" version:
public class Client{
private ...
12
votes
5
answers
4k
views
Why is "hidden dependency" (required things not in parameter list directly) a disadvantage of "global variables", but not in "preserve whole object"?
According to https://softwareengineering.stackexchange.com/a/200092, as I know, "preserve whole object" is a refactor method that passes the whole object instead of required parameters only, ...
7
votes
8
answers
5k
views
Is "avoid extra null pointer risk" a reason to avoid "introduce parameter objects"?
According to Should we avoid custom objects as parameters?, I know I can group related parameters to improve readability of the function, eg:
Original version:
public void showSchedule(long startDate,...
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 ...
0
votes
1
answer
245
views
Elegant way in C to store many parameters with default value and current value in embedded flash
I'm programming an embedded system that has a number of user configurable parameters, which are stored in flash memory. I have to store a default value for each parameter as well as the user settings. ...
0
votes
1
answer
180
views
Should I "introduce parameter object" for the case that the parameter is originally already a whole object?
According to Should we avoid custom objects as parameters?, for example, if I have an object to show:
public class Student{
public int _id;
public String name;
public int age;
public ...
0
votes
3
answers
254
views
Is "the boolean value is from dynamic loaded data (eg:user input, database)" a reason to use boolean parameter?
I know there are some questions about boolean flags: Is it wrong to use a boolean parameter to determine behavior?, Multiple boolean arguments - why is it bad? which indicates the following code is ...
1
vote
2
answers
804
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?, ...
0
votes
1
answer
121
views
Why aren't mandatory options requested in the signature of ASP.NET Core "AddXXX" methods?
I've developing a .NET Core library meant to simplify the configuration of the authentication within our SSO system. It will expose two methods to be called in the Program.cs (or Startup.cs) of ASP....
4
votes
2
answers
3k
views
Should a method modifying object passed as a parameter return the modified object? [duplicate]
I have some incoming request - it's an instance of class generated from api specification - POJO with public getters/setters.
I would like to normalize some values. For example dimensions (to use ...
1
vote
2
answers
1k
views
Ordering keyword arguments in a function call
In some languages such as Python, the order of keyword arguments in function calls does not matter. But is there a best practice for it?
For instance, suppose that a function's signature is def foo(...
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(...
5
votes
2
answers
735
views
How to propagate parameters through several architectural layers?
Overview
I am tasked with designing a system that serves as an Interface between a User and one or more microcontrollers in different Variants.
As an example, our Microcontrollers Type 1 are milk ...
0
votes
1
answer
2k
views
Dynamic Variables from JSON Parameter File
I want to assign Python variables imported from a JSON file. This question had an interesting answer using a classmethod, but I couldn't get it to work and I'm not allowed to comment...
So, let's ...
0
votes
2
answers
1k
views
Reassign parameter to local variable
On Stack Overflow I frequently see questions with code in the following style:
function funcName(parameter) {
let variable = parameter;
// rest of function uses variable rather than parameter
}
...
0
votes
5
answers
341
views
Should I "modularize" my configuration file into different files?
I have a simulation in Python which reads its configuration from a toml file. Since I have tons of parameters, the toml file can grow quite large.
This is an example file, similar in structure to my ...
2
votes
2
answers
647
views
When, and why, to pass parameters by value?
For anything larger than a 64 bit integer, why would I want to pass by value?
[Update] The question was closed, because it was not specific enough. One comment suggested that I specify a language. I ...
14
votes
14
answers
8k
views
Should I raise an exception/error when an optional argument is used but is not necessary?
Take this constructed example:
def fetch_person(person_id, country_code=None):
if is_fully_qualified(person_id):
return person_source_1.fetch_person(person_id)
else:
return ...
2
votes
2
answers
1k
views
Unit testing of classes with functions as parameters in C++
Let's say I have a function in a class with the following signature:
int fun(int x, int y,std::function<int(int, int)> funArg)
The output depends on the operations done in funArg.
My question ...
1
vote
0
answers
132
views
Is using C++ Classes to handle commonly used parameters a misuse of classes?
My team works on an HTTP web server in C++. The codebase has aged over time, and has a widespread problem of 12+ parameters being passed to every function.
A fake example: We need to build a Car, but ...
3
votes
3
answers
2k
views
Using python context managers instead of passing arguments: Is it an anti pattern?
We have an input flag in our API which indicates whether we should keep the mid level resources involved in fulfilling current request or not. I've decided to use some context managers at interface ...
2
votes
1
answer
668
views
Is it an antipattern to pass an object that stores the application state from one function to another?
The program is written in JavaScript. To give you a rough idea what I am thinking of:
function State() {
return {
color: 'green',
size: 100,
// ... there are other properties here
}
}
...
0
votes
2
answers
279
views
Anti-pattern in which code blocks are indirectly used as parameters
I was recently trying to explain a particular anti-pattern to some novice programmers and found that it was hard to express without an overly-detailed example. I'm sure it has a name and that someone ...
2
votes
2
answers
500
views
Better way than repeatedly passing the same parameters?
I have some methods in my code that essentially hide/show some Views, like showTitleHideBody(), showBodyHideTitle(), etc. They just change their Views' (tvTitle, tvBody) visibility.
Initially those ...
-3
votes
1
answer
269
views
Best way to pass an optional parameter to a program
In a couple of my programs, the program needs to know an IP address and a port to which it should connect or send data to.
The solution I have right is to ask for user input via the console - but ...
-3
votes
2
answers
64
views
Passing different sized parameters cost savings
I was wondering if there is some cost saving, either in time or space by passing and/or returning smaller arguments? char vs int.
I have heard the compiler will optimize the code based on the type of ...
-3
votes
1
answer
56
views
Is there a good set of heuristics around how/when to use optional parameters
using a python function signature as an example:
def this_function_has_an_optional_parameter(x, y = 42): ...
I'm wondering if there is an existing set of guidelines specifically for this
0
votes
3
answers
326
views
Should the type of a parameter that holds the arguments for a function be named "argument" or "parameter"
For context, I'm writing TypeScript, but I believe the concept works for many languages.
I have a function getFooParams(): FoodParams, that gets data that gets passed to foo later on. (It's a React-...
1
vote
1
answer
2k
views
Design Patterns for Passing Large Quantity of Parameters in Machine Learning
I am looking to better understand best practices for handling large quantity of parameters. I am particularly interested in the types of parameters involved in machine learning code bases and ...
1
vote
2
answers
459
views
const function parameters and default behavior
Say I have a C++ function
/**
* @param path If empty, the system default is used
*/
void foo(const std::string& path);
And in my implementation I have a default handling for empty paths
void ...
7
votes
4
answers
2k
views
Are "in" parameter modifiers considered a code smell?
As of C# 7.2+, you can add a parameter modifier in before the parameter type to define a parameter as const, essentially.
It is like the ref or out keywords, except that in arguments cannot be ...
1
vote
2
answers
3k
views
How should a streamwriter be passed to an object considering OO and DI
I have an class named FileCreator which is used to write many strings to a stream. Basically, to achieve its job, the FileCreator needs two objects: a StreamWriter and the actual strings that will be ...
3
votes
5
answers
12k
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 ...
1
vote
8
answers
4k
views
Is there a use case for boolean parameters? [closed]
Most programmers (including me) believe that methods with a boolean flag parameter should be refactored into two methods without the flag parameter. Are there any use cases for a boolean parameter ...
2
votes
2
answers
232
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 ...
3
votes
2
answers
659
views
Best practices for testing settings file with many parameters in simulation code
I'm conflicted as to what is the best way to approach this problem.
I am writing a simulation in Python, which is parametrized by ~ 50 parameters. I have a JSON file where these parameters are set, ...
2
votes
2
answers
227
views
Is inline still necessary when using the Named Parameter Idiom?
The Named Parameter Idiom as described here mentions that there will be a performance impact when not using inline.
Since each member function in the chain returns a reference, there is no copying ...
1
vote
3
answers
151
views
What considerations should I mind when designing methods or functions that take in a lot of parameters?
What considerations should I mind when designing methods or functions that take in a lot of parameters? A lot meaning over 4 but less than 10.
Example, I am debating whether to pass in an array like ...
-2
votes
1
answer
110
views
method taking a class parameter
I have recently begun studying UML. All is going fine so far until I saw the following:
This is a class Called Point2D
It has 2 attributes which are x, type float and y, type float.
It has 3 methods
...
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 ...
5
votes
3
answers
3k
views
Best way to provide configuration parameters for objects far away from the starting point
We are developing complex application on the top of the ROS framework in C++ and recently ran into discussion how to provide parameters to the parts of code far away from the starting main().
The ...
2
votes
0
answers
506
views
How to handle a large number of optional parameters
I am currently developing on a small library allowing to read and write Java .properties files while retaining all the formatting (comments, whitespace, etc.): https://github.com/hupfdule/apron
This ...
1
vote
2
answers
3k
views
Pass-Through parameters in recursive code
When writing large amounts of recursive code (for valid reasons), I have come across many parameters that are not used in specific functions, but are still needed for a subset of all of the functions.
...
3
votes
3
answers
2k
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 ...
1
vote
3
answers
511
views
When to use Parameters and When Not to C#
I am have an application using MVVM pattern. It takes a user ID and returns a table with the user's bookmarks. I am trying to decided if it is better practice to include parameters in my model's ...
1
vote
3
answers
4k
views
Setter with one parameter and null default value
Which solution is most logical? The value can be null, but when not null it must be a string.
This (First):
function setValue(string $value = null);
To me this is bad; since we can now call the ...