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.

140 questions
234
votes
10 answers

Is there a name for the (anti- ) pattern of passing parameters that will only be used several levels deep in the call chain?

I was trying to find alternatives to the use of global variable in some legacy code. But this question is not about the technical alternatives, I'm mainly concerned about the terminology. The obvious solution is to pass a parameter into the…
RubenLaguna
  • 1,842
  • 2
  • 12
  • 11
214
votes
5 answers

What are good habits for designing command line arguments?

While developing the application I started to wonder - How should I design command line arguments? A lot of programs are using formula like this -argument value or /argument value. Solution which came to my mind was argument:value. I thought it is…
Filip Hazubski
  • 2,013
  • 3
  • 11
  • 11
169
votes
11 answers

Are there guidelines on how many parameters a function should accept?

I've noticed a few functions I work with have 6 or more parameters, whereas in most libraries I use it is rare to find a function that takes more than 3. Often a lot of these extra parameters are binary options to alter the function behaviour. I…
Darth Egregious
  • 1,803
  • 2
  • 13
  • 9
65
votes
14 answers

What is best practice on ordering parameters in a function?

Sometimes (rarely), it seems that creating a function that takes a decent amount of parameters is the best route. However, when I do, I feel like I'm often choosing the ordering of the parameters at random. I usually go by "order of importance",…
Casey Patton
  • 5,211
  • 7
  • 33
  • 41
53
votes
10 answers

Should we avoid custom objects as parameters?

Suppose I have a custom object, Student: public class Student{ public int _id; public String name; public int age; public float score; } And a class, Window, that is used to show information of a Student: public class Window{ …
ggrr
  • 5,725
  • 11
  • 35
  • 37
43
votes
7 answers

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 name except for casing in C#. It doesn't seem to be…
Jason Tyler
  • 653
  • 5
  • 7
40
votes
9 answers

Should I accept empty collections in my methods that iterate over them?

I have a method where all logic is performed inside a foreach loop that iterates over the method's parameter: public IEnumerable TransformNodes(IEnumerable nodes) { foreach(var node in nodes) { // yadda yadda…
Nick Udell
  • 1,214
  • 2
  • 11
  • 17
39
votes
13 answers

Is it wrong to use a boolean parameter to determine values?

According to Is it wrong to use a boolean parameter to determine behavior?, I know the importance of avoid using boolean parameters to determine a behaviour, eg: original version public void setState(boolean flag){ if(flag){ a(); …
ocomfd
  • 5,652
  • 8
  • 29
  • 37
36
votes
2 answers

Why do methods that take an unlimited amount of parameters often define overloads with fewer parameters?

For instance, the System.IO.Path.Combine method in .NET has the following overloads: Combine(params String[]) Combine(String, String) Combine(String, String, String) Combine(String, String, String, String) What is the point of the last three? The…
Alex
  • 470
  • 3
  • 7
36
votes
9 answers

How to name a method that both performs a task and returns a boolean as a status?

If there is a method bool DoStuff() { try { // doing stuff... return true; } catch (SomeSpecificException ex) { return false; } } should it rather be called IsStuffDone()? Both names could be misinterpreted…
Limbo Exile
  • 705
  • 2
  • 7
  • 11
33
votes
3 answers

Optional parameters or overloaded constructors

I am implementing a DelegateCommand, and when I was about to implement the constructor(s), I came up with the following two design choices: 1: Having multiple overloaded constructors public DelegateCommand(Action execute) : this(execute, null) {…
user277671
29
votes
3 answers

Why not annotate function parameters?

To make this question answerable, let's assume that the cost of ambiguity in the mind of a programmer is much more expensive then a few extra keystrokes. Given that, why would I allow my teammates to get away with not annotating their function…
JDB
  • 628
  • 8
  • 15
27
votes
7 answers

Specify optional parameter names even though not required?

Consider the following method: public List ReturnEmployeeIds(bool includeManagement = false) { } And the following call: var ids = ReturnEmployeeIds(true); For a developer new to the system, it'd be pretty difficult guessing what true did.…
JᴀʏMᴇᴇ
  • 2,361
  • 4
  • 22
  • 25
25
votes
5 answers

Multiple arguments in function call vs single array

I have a function that takes in a set of parameters, then applies to them as conditions to an SQL query. However, while I favored a single argument array containing the conditions themselves: function searchQuery($params = array()) { …
xiankai
  • 373
  • 1
  • 3
  • 6
21
votes
6 answers

Should a method be forgiving with the arguments that are passed in?

Suppose we have a method foo(String bar) that only operates on strings that meet certain criteria; for example, it must be lowercase, must not be empty or have only whitespace, and must match the pattern [a-z0-9-_./@]+. The documentation for the…
Zymus
  • 2,403
  • 3
  • 19
  • 35
1
2 3
9 10