Questions tagged [functions]
Function is a block of code which performs a specific task.
313
questions
-1
votes
1
answer
43
views
Rules of Thumb for when to create a function that accepts an object parameter vs. creating a method of the object instead? [closed]
My question is this:
What are some rules of thumb for whether to make a function standalone or a method?
Background
Our team was having a debate during a GoLang code review — though the question can ...
1
vote
0
answers
89
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
6k
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
178
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
175
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
9k
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
161
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 ...
3
votes
3
answers
170
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
5k
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
427
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
113
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
317
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
71
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
81
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
1k
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
391
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
635
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 ...
11
votes
6
answers
6k
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 ...
-2
votes
1
answer
72
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 ...
0
votes
2
answers
101
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
174
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
75
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.
# ...
9
votes
3
answers
4k
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
88
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
2k
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
442
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)
...
7
votes
4
answers
1k
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
684
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
253
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
202
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
258
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
181
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
679
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
279
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
271
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
280
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 ...
1
vote
1
answer
290
views
When should an argument be set to None in Python?
The focus of my question is on design.
If I have an argument that can be None and it is passed between several functions until finally being used, which function should treat it as a default argument?
...
0
votes
5
answers
235
views
The cases where returning Bools as literal expressions is a better option
Most people would return Bools as follows:
bananas(color) {
return (color = "yellow")
}
and not as literal expressions; that is, not this way:
bananas(color) {
if (color = "...
3
votes
1
answer
175
views
How choose between functions or objects
I've seen different approaches and it's not clear to me which one is the correct one.
Some dev use lots of small, very specific objects and use to compone them is some way to achieve their goal.
...
2
votes
1
answer
3k
views
how to represent functions and global variables in UML?
I'm going to translate into UML a C++ project I have been doing for a few days and which I estimate convenient to handle as a model for a way better management. The problem is the following: I tried ...
0
votes
1
answer
163
views
How to maintain SRP with a chain of async functions?
For code maintainability I understand that it is generally better to have code structured like this:
void abc() {
a();
b();
c();
d();
}
Instead of like:
void abcd() {
a();
bcd(...
0
votes
2
answers
236
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 ...
1
vote
2
answers
405
views
Are multiple dynamic dispatch methods possible?
C++ only supports single dynamic dispatch methods. Indeed, the following program:
#include <iostream>
struct Shape {
virtual void overlap(Shape* y) { std::cout << "Shape, Shape\n&...
10
votes
5
answers
5k
views
How to name a function that returns a function?
Let's say, for example, that I have a function makeFoo that makes another function foo:
function makeFoo(string) {
return () => string
}
const foo = makeFoo('bar');
I know that makeFoo is a ...
8
votes
9
answers
8k
views
Returning true/nothing instead of true/false?
Is it OK to have a function that returns true/nothing instead of true/false?
My examples use JavaScript, but I'm wondering about the general case, not attached to one specific language.
The subjects ...
1
vote
3
answers
129
views
Good etiquette for 2 optional arguments that can't both be used
I'll demonstrate with an example of the normal distribution in Python.
def norm_pdf(x, mu=0, v=1, p=1):
"""Returns un-normalized probability density of normal distribution at x.
mu: mean
v: ...
-3
votes
1
answer
47
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
155
views
Should a function that returns something be named as an accessor (getSomething)?
I understand accessors have to do with OOP. For other languages such as C, imagine you have a function that given 2 numbers returns their sum. Should you name it getSum() or sum()?
3
votes
3
answers
764
views
Can a function returning void be called a procedure? [duplicate]
I have read about function vs procedure
function and procedure both are subroutines but function returns a value and procedure doesn't.
Can a function returning void be called a procedure?
Give ...