Skip to main content

Questions tagged [functions]

Function is a block of code which performs a specific task.

Filter by
Sorted by
Tagged with
8 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, eg: public static int myFunction(){ try { Thread.sleep(MyClass....
wcminipgasker2023's user avatar
6 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 ...
doca's user avatar
  • 245
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 ...
Nova's user avatar
  • 181
0 votes
2 answers
137 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 ...
Karanveer A's user avatar
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 ...
Christian's user avatar
  • 171
-4 votes
1 answer
83 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: ...
Arunabh's user avatar
  • 143
0 votes
1 answer
268 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 ...
b3rry's user avatar
  • 3
-1 votes
7 answers
640 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 ...
J. Mini's user avatar
  • 1,017
0 votes
0 answers
250 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 ...
user8400863's user avatar
-1 votes
1 answer
101 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 ...
MikeSchinkel's user avatar
0 votes
0 answers
119 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:...
a.t.'s user avatar
  • 225
8 votes
5 answers
9k 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 ...
devdevdove's user avatar
2 votes
2 answers
232 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 ...
Thiago Pereira Maia's user avatar
0 votes
2 answers
939 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 } ...
Barmar's user avatar
  • 346
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 ...
J. Mini's user avatar
  • 1,017
0 votes
2 answers
187 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 ...
Tlaloc-ES's user avatar
  • 387
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 ...
Angelica's user avatar
  • 109
3 votes
3 answers
278 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'...
Tim Kirkwood's user avatar
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 ...
pro100tom's user avatar
  • 449
1 vote
1 answer
1k 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 ...
CheeseS's user avatar
  • 19
-2 votes
2 answers
126 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 ...
SuperSim135's user avatar
2 votes
2 answers
480 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 ...
Mawg's user avatar
  • 4,226
-1 votes
2 answers
77 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 ...
Good guy's user avatar
0 votes
2 answers
114 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 ...
Andy Lin's user avatar
  • 185
1 vote
2 answers
4k 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 ...
user avatar
4 votes
2 answers
1k 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 ...
coney1's user avatar
  • 87
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 ...
Wballer3's user avatar
13 votes
6 answers
10k 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 ...
P. Hopkinson's user avatar
-1 votes
1 answer
79 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 ...
Anon's user avatar
  • 3,613
0 votes
1 answer
478 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 ...
InterestedDev's user avatar
0 votes
2 answers
111 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) ...
George Sovetov's user avatar
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(...
The Chinky Sight's user avatar
2 votes
1 answer
254 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 ...
Greatcode's user avatar
-1 votes
1 answer
77 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. # ...
Ulysses's user avatar
  • 101
9 votes
3 answers
6k 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 ...
Angel E.'s user avatar
  • 119
-2 votes
1 answer
154 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 ...
nick012000's user avatar
0 votes
2 answers
6k 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 ...
user90726's user avatar
  • 205
0 votes
2 answers
967 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) ...
pstatix's user avatar
  • 1,047
8 votes
4 answers
2k 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 ...
Raikish's user avatar
  • 189
-1 votes
3 answers
1k 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 ...
The Pointer's user avatar
0 votes
1 answer
460 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 ...
Jamal Rahman's user avatar
0 votes
3 answers
319 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 ...
sartoz's user avatar
  • 103
4 votes
3 answers
316 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.) ...
Jakov's user avatar
  • 151
0 votes
2 answers
573 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 ...
Harry Berry's user avatar
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() ...
cube's user avatar
  • 157
2 votes
1 answer
595 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 } } ...
becomeocean's user avatar
0 votes
2 answers
357 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)...
Tim's user avatar
  • 5,485
1 vote
1 answer
386 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 ...
user90726's user avatar
  • 205
1 vote
1 answer
1k 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? ...
Alisson Hayasi's user avatar
0 votes
5 answers
246 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 = "...
john c. j.'s user avatar

1
2 3 4 5
7