253 votes
Accepted

What is a term for a function that when called repeatedly, has the same effect as calling once?

This type of function / operation is called Idempotent Idempotence (UK: /ˌɪdɛmˈpoʊtəns/,[1] US: /ˌaɪdəm-/)[2] is the property of certain operations in mathematics and computer science whereby they ...
Rufus's user avatar
  • 1,437
232 votes

My boss asks me to stop writing small functions and do everything in the same loop

There are other problems Neither code is good, because both basically bloat the code with a debug test case. What if you want to test more things for whatever reason? phoneNumber = ...
null's user avatar
  • 3,546
227 votes
Accepted

My boss asks me to stop writing small functions and do everything in the same loop

Taking the code examples first. You favour: if (isApplicationInProduction(headers)) { phoneNumber = headers.resourceId; } else { phoneNumber = DEV_PHONE_NUMBER; } function ...
David Arno's user avatar
152 votes
Accepted

When do function call costs still matter in modern compilers?

It depends on your domain. If you are writing code for low-power microcontroller, then method call cost might be significant. But if you are creating normal website or application, then method call ...
Euphoric's user avatar
  • 36.9k
146 votes
Accepted

When to optimize for memory vs performance speed for a method?

Instead of speculating about what may or may not happen, let's just look, shall we? I'll have to use C++ since I don't have a C# compiler handy (though see the C# example from VisualMelon), but I'm ...
Phil Frost's user avatar
  • 1,569
120 votes
Accepted

Is it inadvisable to make a function that essentially renames a built-in function?

As others have already mentioned: don't create a function with a name that is similar to that of a builtin, standard-library or generally widely used function but change its behavior. It is possible ...
5gon12eder's user avatar
  • 6,976
114 votes

Is it inadvisable to make a function that essentially renames a built-in function?

If you make a function like that where minimize(4, 10) returns 10, then I'd say that is inadvisable because your fellow programmers may strangle you. (Okay, maybe they will not literally strangle you ...
Telastyn's user avatar
  • 109k
104 votes
Accepted

Should you guard against unexpected values from external APIs?

You should never trust the inputs to your software, regardless of source. Not only validating the types is important, but also ranges of input and the business logic as well. Per a comment, this is ...
Paul's user avatar
  • 3,277
79 votes
Accepted

Is it bad practice to use a C++ compiler just for function overloading?

I wouldn't go so far as to call it "bad practice" per se, but neither am I convinced it's really the right solution to your problem. If all you want is four separate functions to do your four data ...
Philip Kendall's user avatar
67 votes

Why do many functions that return structures in C, actually return pointers to structures?

There are several practical reasons why functions like fopen return pointers to instead of instances of struct types: You want to hide the representation of the struct type from the user; You're ...
John Bode's user avatar
  • 10.8k
66 votes

When to optimize for memory vs performance speed for a method?

To answer the stated question: When to optimize for memory vs performance speed for a method? There are two things you have to establish: What is limiting your application? Where can I reclaim the ...
Berin Loritsch's user avatar
60 votes

When do function call costs still matter in modern compilers?

Function call overhead depends entirely on the language, and at what level you are optimizing. On an ultra low level, function calls and even more so virtual method calls may be costly if they lead ...
amon's user avatar
  • 133k
59 votes

My boss asks me to stop writing small functions and do everything in the same loop

There is no "right" or "wrong" answer to this. However, I will offer my opinion based on 36 years of professional experience designing and developing software systems ... There is no such thing as "...
LiamF's user avatar
  • 759
56 votes

Is it bad practice to use a C++ compiler just for function overloading?

Using only some features of C++ while otherwise treating it as C is not exactly common, but also not exactly unheard of either. In fact, some people even use no features at all of C++, except the ...
Jörg W Mittag's user avatar
51 votes
Accepted

Why use `const foo = () => {}` instead of `function foo() {}`

Function statements (named functions, 2nd syntax shown) are hoisted to the top of the full lexical scope, even those behind arbitrary and control blocks, like if statements. Using const (like let) to ...
dandavis's user avatar
  • 640
50 votes

My boss asks me to stop writing small functions and do everything in the same loop

“Entities must not be multiplied beyond necessity.” — Occam's Razor Code has to be as simple as possible. Bugs like to hide in between complexity, because they are difficult to spot there. So what ...
amon's user avatar
  • 133k
50 votes

What is a term for a function that when called repeatedly, has the same effect as calling once?

The precise term for this (as Woofas mentions) is idempotence. I wanted to add that while you could call your func1 method idempotent, you could not call it a pure function. The properties of a pure ...
Neil's user avatar
  • 22.7k
48 votes

A language based on limiting amount of arguments passed to functions

There are lots of languages which already work this way, e.g. Haskell. In Haskell, every function takes exactly one argument and returns exactly one value. It is always possible to replace a function ...
Jörg W Mittag's user avatar
47 votes
Accepted

Is converting a C++ method to a C function with a pointer argument an acceptable pattern?

A reinterpret_cast is always fishy unless you know exactly what you are doing. Here, your code happens to work only due to GCC's calling convention for C++ methods, but this smells heavily like ...
amon's user avatar
  • 133k
45 votes

When to optimize for memory vs performance speed for a method?

"this would reduce memory" - em, no. Even if this would be true (which, for any decent compiler is not), the difference would most probably be negligible for any real world situation. However, I ...
Doc Brown's user avatar
  • 201k
44 votes

Why use `const foo = () => {}` instead of `function foo() {}`

Here are some reasons you might want to use function: The signaling is clear and concise. This is far more beneficial than any of the edge-case hoisting concerns that are listed in the other answer. ...
Wayne Bloss's user avatar
42 votes
Accepted

Function returning true/false vs. void when succeeding and throwing an exception when failing

Throwing an exception is simply an additional way of making a method return a value. The caller can check for a return value just as easily as catch an exception and check that. Therefore, deciding ...
Kilian Foth's user avatar
41 votes
Accepted

A language based on limiting amount of arguments passed to functions

Robert C. Martin in his book "Clean Code" recommends heavily the use of functions with 0, 1 or 2 parameters at maximum, so at least there is one experienced book author who thinks code becomes cleaner ...
Doc Brown's user avatar
  • 201k
40 votes

Why do many functions that return structures in C, actually return pointers to structures?

There are two ways of "returning a structure." You can return a copy of the data, or you can return a reference (pointer) to it. It's generally preferred to return (and pass around in general) a ...
Mason Wheeler's user avatar
38 votes

Function returning true/false vs. void when succeeding and throwing an exception when failing

There is absolutely no reason for returning true on success if you don't return false on failure. What should the client code look like? if (result = tryMyAPICall()) { // business logic } else { ...
Polygnome's user avatar
  • 2,038
36 votes

How small should functions be?

Make them as small as possible, but no smaller. The limiting factor here has nothing to do with the work being done. It has to do with if you can think of a good name. A decent vocabulary is a ...
candied_orange's user avatar
34 votes

Should you guard against unexpected values from external APIs?

Yes, of course. But what makes you think the answer could be different? You surely don't want to let your program behave in some unpredictable manner in case the API does not return what the ...
Doc Brown's user avatar
  • 201k
33 votes
Accepted

Unable to solve mystery of functions in Javascript

I am trying to understand behind the curtain scenes of Javascript and kind of stuck in understanding the creation of built in objects, specially Object and Function and the relation between them. It ...
Eric Lippert's user avatar
  • 45.9k
33 votes

When to optimize for memory vs performance speed for a method?

You can do better than both of those with return (abs(a + b) > 1000); Most processors (and hence compilers) can do abs() in a single operation. You not only have fewer sums, but also fewer ...
Graham's user avatar
  • 1,996
30 votes
Accepted

Why do heavily object-oriented languages avoid having functions as a primitive type?

IMO... Because Java and C# are not true OO languages. Functional programming was not in vogue when they were designed. I agree with Jörg W Mittag, neither C# nor Java are true object-oriented ...
Schwern's user avatar
  • 1,058

Only top scored, non community-wiki answers of a minimum length are eligible