Questions tagged [functions]
Function is a block of code which performs a specific task.
326 questions
7
votes
8
answers
940
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:
...
2
votes
3
answers
2k
views
Is refactoring scattered functions into a class always a good idea?
Straight to the point, here's the original code:
integer = input(...)
size = len(integer)
# Created three dicts for matching numbers as keys to corresponding words
dic_ones = {...}
dic_tens = {...}
...
9
votes
7
answers
3k
views
If the sleep time of a function at first time differs from the second time, but the output is the same, is it still a idempotent function?
For example, if the sleep time of a function would change after first execution but the output remains unchanged, e.g.:
public static int myFunction(){
try {
Thread.sleep(MyClass....
7
votes
6
answers
1k
views
Is a callback function with `this` as an argument a bad practice?
I have a class that has a callback function that gets triggered on some event. The user of the class instance is expected to do stuff on the instance itself, inside this callback function. So I am ...
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
2
answers
349
views
Why do you need to use pass by reference in C++ to change the value of the arguments inside the function?
I'm new to coding and am currently trying to learn C++ myself. I just learned about function parameters and pass by value vs pass by reference. Everywhere I read, they say that one of the reasons pass ...
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 ...
-4
votes
1
answer
118
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:
function,...
0
votes
1
answer
594
views
How do function inlining and Tail Call Optimization affect call stack?
I've just accidentally came across this answer about inlined functions and I'd like to know how this affects call stack. But I can't add comments because I don't have enough rep so I decided to ask ...
-1
votes
7
answers
730
views
If you use Inversion of Control, what alternatives to obfuscated function calls exist?
Consider a class that follows the obfuscated function call anti-pattern. I've also seen these called "stupid classes". The definition of such a class is that it only has one public method ...
0
votes
0
answers
325
views
C# Azure Function Durable Function Vs Batch Timer Trigger Function
I have a question related to the best approach when resources are limited, such as on the Azure Function consumption plan.
I have an IoT device, and the number of IoT devices is likely to increase to ...
-1
votes
1
answer
137
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 ...
0
votes
0
answers
149
views
Python Typechecking versus TypedDicts?
From what I understand from this answer, it is not possible to use a typeddict and typechecking in a function. So for example, if one has a function:
def some_func(some_int: int, some_dict:...
8
votes
5
answers
10k
views
Is code written inline faster than using function calls?
I wrote some script in Python that creates a giant 2D matrix (1000x1000 or bigger) and fills it with random numbers. And after that, it goes through every element of the matrix and changes the number ...
2
votes
2
answers
262
views
Should I abstract function calls with duplicate arguments?
I have an intuition, that I'd like to read what others have talked about. To me, it seems fairly intuitive that when you have a function which is called multiple times with the same arguments, it ...
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
}
...
38
votes
10
answers
10k
views
Why do heavily object-oriented languages avoid having functions as a primitive type?
As has been covered to the point of parody, heavily object-oriented languages, such as C# or Java, tend to lack the feature of having functions as a primitive type. You can argue about whether or not ...
0
votes
2
answers
239
views
Why session should be a param when you write a query function?
I read a lot of examples where I read code like this:
def get_user_by_id(session, id)
...
and the function that calls that function needs to create or get the DB session a pass to the function.
the ...
10
votes
7
answers
2k
views
Is global state really always bad?
I have a question regarding my general understanding of global state in software engineering.
When I write an app I like to decompose it into little, manageable components and functions, that are ...
3
votes
3
answers
351
views
Referencing transient class attributes
I've just started dipping my feet into OOP.
Is it considered bad practice to have classes that reference attributes that depend on another function being called and thus may not exist (version 1)? I'...
21
votes
4
answers
6k
views
What does function composition being associative even mean?
When we have a math problem such as 3 + 5 + 2, we say that it is associative. We can choose which step to pick first: 3 + (5 + 2); we know that brackets affect the order in which the operations are ...
1
vote
1
answer
2k
views
How to avoid argument drilling when subdividing functions?
As suggested by Uncle Bob in his book "Clean Code", I am actively trying to keep my functions small and readable.
However, I often encounter arguments drilling when I try to refractor a big ...
-2
votes
2
answers
150
views
Is there a name for a function that outputs more data than was put into it? [closed]
Is there a name for a function that outputs more data than was put into it? For example: let's say I input a one byte value into the function and it returns a three byte value, is there a name for ...
2
votes
2
answers
628
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 ...
-1
votes
2
answers
88
views
How do I properly write these functions? [duplicate]
So far my weakest side has been proper code structuring and organization. I wanted to know how I can properly apply separation of concerns to the following code to make it more organized.
I have a ...
0
votes
2
answers
155
views
How to tell client if predicate function fails?
Just say I have list APIs, and provide find() to search node.
With this design if something went wrong during pre-conditions, client would have no idea about it because no status code provided.
bool ...
1
vote
2
answers
5k
views
Recommended way of hiding implementation details?
I have a single *.h file. This file contains a single (more to come) function declaration.
Now the implementation of that file is very complex.
the corresponding *.cpp contains several function ...
4
votes
2
answers
2k
views
Should similar standalone functions go in a class?
I'm working to create a library in python that myself and a few colleagues will use. I'm struggling to conceptually understand how to best organize some code that feels like it doesn't cleanly fit ...
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 ...
13
votes
6
answers
12k
views
Is it reasonable to use dictionaries instead of arguments?
In python I often see functions with a lot of arguments. For example:
def translate(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p):
// some code
return(x, y, z)
I like this pattern in some ...
-1
votes
1
answer
100
views
What do you call a function that properly remains undefined?
Backstory:
I have subclasses that are supposed to override and define various functions, but not neccessarily all of them. They can't remain purely virtual though for obvious reasons, and so I am ...
1
vote
1
answer
667
views
AWS Java Lambda Class Naming Convention?
I wonder if there is a naming convention or a best practice to name Java Classes when building AWS Lambda functions with Java for REST API?
For example, if I have a Java project that contains Lambda ...
0
votes
2
answers
121
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)
...
11
votes
5
answers
2k
views
What does "If a function does only those steps that are one level below the stated name of the function, then the function is doing one thing" means?
This doubt is about function doing one thing from Chapter 3: Functions of the book named Clean Code
Here Uncle Bob is talking about this function:
public static String renderPageWithSetupsAndTeardowns(...
2
votes
1
answer
310
views
How should a bytecode VM call external C functions?
I am trying to implement a basic bytecode VM, which I plan to target with a compiler. How can I implement the ability to call external C functions using the bytecode, i.e., call arbitrary functions in ...
-1
votes
1
answer
94
views
In a language interpreted line by line - is optimizing similar lines of code within a module into functions better in terms of efficiency? [duplicate]
While writing python code (I write python-selenium for GUI automation), I am facing situations wheer I have to deal with 5 widgets that do the same thing, just there xpath is differs by one term.
# ...
10
votes
3
answers
7k
views
Is it best practice to define a member function directly in a class?
I'm a beginner in C++, and I was wondering if it is best practice to define a member function directly in a class, such as:
// something.hpp
class C {
inline int func() { return ... ; }
}
rather ...
-2
votes
1
answer
189
views
How do you use ad hoc polymorphism/function overloading with functions in Python?
So, let's say you've got a function foobar() which can function with a variable number of parameters inputted into it, and has different behavior for each of them. How do you get this to function ...
0
votes
2
answers
8k
views
Defining default values for Boolean arguments in JavaScript
Is it usually recommended to define default values for Boolean arguments?
I mean, is it usually recommended to define a function like this
someFunction(a, b, x) {
// a and b are strings, x is true ...
0
votes
2
answers
1k
views
Loop outside method or method with internal loop?
If I have a list of objects that need to have an operation performed on each, is there a best practice in abstracting the loop or not?
Looping over list and call
def func(item):
some_op(item)
...
8
votes
4
answers
3k
views
Use local or nested function for readability?
I found in the catalog of Refactoring by Martin Fowler, with Kent Beck book that they mention Extract Function refactoring.
It is a good practice to wrap your related code into local functions to ...
-1
votes
3
answers
2k
views
Does wrapping functions/'things' in classes reduce efficiency?
I was reading some C++ object-oriented programming notes that mentioned that we should avoid wrapping functions in classes if it is not required, since wrapping 'things' in classes would reduce ...
0
votes
1
answer
566
views
Python: Function pipeline with multiple return/input values, or use OOP? Best Practices?
I have a 'processing' function and a 'serializing' function. Currently the processor returns 4 different types of data structures to be serialized in different ways.
Looking for the best practise on ...
0
votes
3
answers
390
views
Solutions for polyadic functions/methods
In the book Clean Code, Robert C. Martin says that we should avoid polyadic functions (functions that contain four or more arguments).
One of the solutions presented by him is the use of objects as ...
4
votes
3
answers
355
views
Naming function with "proceed"
I am wondering if it is good practice to name function which does the main logic "proceed" + "functionName".
I would use that name if there are some checks(if-s, try-catches, etc.) ...
0
votes
2
answers
819
views
Name for a "called function"
I am writing a documentation for a programm where a function is defined and it is called three times. I call the definition of the function "function definition". But what is the correct ...
1
vote
4
answers
1k
views
Using output arguments in C++ to avoid dynamic allocations
I have a function that repeatedly encodes Foos to string. I'm currently deciding between two ways to implement this:
Return by value:
std::string encode(const Foo& foo);
void important_function() ...
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
421
views
What is a "function returning type" in C?
C11 standard says
6.3.2.1 Lvalues, arrays, and function designators
A function designator is an expression that has function type.
Except when it is the operand of the sizeof operator, 65)...
1
vote
1
answer
471
views
Which statements can be considered as exit points?
There are many discussions related to whether it is better to have only one or multiple exit points per function, but it is not clear to me which statements can be considered as exit points: only ...