Function is a block of code which performs a specific task.
Questions tagged [functions]
318 questions
219
votes
16 answers
My boss asks me to stop writing small functions and do everything in the same loop
I have read a book called Clean Code by Robert C. Martin. In this book I've seen many methods to clean up code like writing small functions, choosing names carefully, etc. It seems by far the most interesting book about clean code I've read.…

GitCommit Victor B.
- 2,031
- 2
- 9
- 9
179
votes
13 answers
Is it OK to split long functions and methods into smaller ones even though they won't be called by anything else?
Lately I've been trying to split long methods into several short ones.
For example: I have a process_url() function which splits URLs into components and then assigns them to some objects via their methods. Instead of implementing all this in one…

Stas Bichenko
- 3,689
- 4
- 24
- 32
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
129
votes
12 answers
One-line functions that are called only once
Consider a parameterless (edit: not necessarily) function that performs a single line of code, and is called only once in the program (though it is not impossible that it'll be needed again in the future).
It could perform a query, check some…

deprecated
- 3,297
- 3
- 20
- 26
110
votes
15 answers
When to optimize for memory vs performance speed for a method?
I recently interviewed at Amazon. During a coding session, the interviewer asked why I declared a variable in a method. I explained my process and he challenged me to solve the same problem with fewer variables. For example (this wasn't from the…

Corey P
- 1,224
- 2
- 7
- 14
106
votes
6 answers
What is a term for a function that when called repeatedly, has the same effect as calling once?
(Assuming a single-threaded environment)
A function that fulfills this criterion is:
bool MyClass::is_initialized = false;
void MyClass::lazy_initialize()
{
if (!is_initialized)
{
initialize(); //Should not be called multiple times
…

Rufus
- 1,437
- 3
- 9
- 11
105
votes
12 answers
When do function call costs still matter in modern compilers?
I am a religious person and make efforts not to commit sins. That is why I tend to write small (smaller than that, to reword Robert C. Martin) functions to comply with the several commandments ordered by the Clean Code bible. But while checking…

Billal Begueradj
- 1,305
- 5
- 16
- 31
96
votes
5 answers
Why store a function inside a python dictionary?
I'm a python beginner, and I just learned a technique involving dictionaries and functions. The syntax is easy and it seems like a trivial thing, but my python senses are tingling. Something tells me this is a deep and very pythonic concept and I'm…

mdeutschmtl
- 1,079
- 1
- 8
- 6
73
votes
4 answers
How do functional languages handle random numbers?
What I mean about that is that in nearly every tutorial I've read about functional languages, is that one of the great things about functions, is that if you call a function with the same parameters twice, you'll always end up with the same…

Electric Coffee
- 1,465
- 1
- 14
- 22
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
63
votes
2 answers
Why use `const foo = () => {}` instead of `function foo() {}`
Edit added 2+ years later
I "checked" the @dandavis answer because it answers my original question, giving reasons to prefer const foo. However, I am completely convinced by the @Wayne Bloss answer that function foo() is generally…

user949300
- 8,679
- 2
- 26
- 35
62
votes
10 answers
Is it bad practice to use a C++ compiler just for function overloading?
So I am working on a software design using C for a certain processor. The tool-kit includes the ability to compile C as well as C++. For what I am doing, there is no dynamic memory allocation available in this environment and the program is overall…

Snoop
- 2,718
- 5
- 24
- 52
58
votes
6 answers
Why do many functions that return structures in C, actually return pointers to structures?
What is the advantage of returning a pointer to a structure as opposed to returning the whole structure in the return statement of the function?
I am talking about functions like fopen and other low level functions but probably there are higher…

yoyo_fun
- 2,267
- 3
- 17
- 22
57
votes
5 answers
Where should I put functions that are not related to a class?
I am working on a C++ project where I have a bunch of math functions that I initially wrote to use as part of a class. As I've been writing more code, though, I've realized I need these math functions everywhere.
Where is the best place to put them?…

coconut
- 673
- 1
- 5
- 5
56
votes
4 answers
Boolean Method Naming Affirmative vs Negative
Should boolean methods always take the affirmative form, even when they will only ever be used in the negative form?
Say I wanted to check whether an entity exists before creating one, my argument is that the first form below is better than the…

lynks
- 663
- 1
- 5
- 6