Skip to main content

New answers tagged

7 votes

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?

The function you reference is idempotent. The relevant definition of idempotent is (per Wikipedia): a subroutine with side effects is idempotent if multiple calls to the subroutine have the same ...
JimmyJames's user avatar
  • 27.7k
-1 votes

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?

Timing and system clock state are important in context of this question. I therefore reject hypotheticals about mathematical purity. If our only concern is inexact system clock which can't measure 1ms ...
Basilevs's user avatar
  • 2,263
2 votes

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?

In short Depending on your definition, either your function is idempotent or no function at all is idempotent. Some more arguments Idempotence is about producing the same result even when applied ...
Christophe's user avatar
  • 78.7k
3 votes

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?

This is really a matter of definition, but to me if the execution time matters at all for idempotence then to be most strictly idempotent the second call to the function should take zero or negligible ...
bdsl's user avatar
  • 3,726
3 votes

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?

Clearly this is depends on magnitude. For some tasks, a speed difference of one second might be a difference like night and day (or even between life and death). For others it could be totally ...
Kilian Foth's user avatar
1 vote

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?

Yes, the function itself is idempotent because there are seemingly no additional unintended side-effects when the function is called repeatedly - i.e. final state of the program is the same after ...
Ben Cottrell's user avatar
1 vote

When should a function be given an argument vs getting the data itself?

It is a good practice to separate your core/business logic from IO, parsing, serializing, deserializing and other technical staffs. Then you can write unit tests against the core logic only and verify ...
Gulshan's user avatar
  • 9,462
5 votes

Is a callback function with `this` as an argument a bad practice?

It isn't that passing this is problematic. Variation number 1 is making some assumptions that are not immediately apparent — assumptions that variation number 2 does not make. Variation 1: Publisher ...
Greg Burghardt's user avatar
-1 votes

Is a callback function with `this` as an argument a bad practice?

You do what you need to do, and preferably you use a pattern that others use successfully. The pattern depends on your environment. An example: On macOS and iOS you have fields in the UI that allow ...
gnasher729's user avatar
-2 votes

Is a callback function with `this` as an argument a bad practice?

The key difference between the two can be summarized as the "this" being injected versus captured. Typically, there are two points to be considered: The first point is the benefit of ...
rwong's user avatar
  • 16.9k
3 votes

Is a callback function with `this` as an argument a bad practice?

Passing an object as an callback argument is a common practice in languages without closures/lambdas/context capture. For example, in C there is no way to make a callback enhanced with any kind of ...
Basilevs's user avatar
  • 2,263
2 votes

Is a callback function with `this` as an argument a bad practice?

The example is artificial enough to make the answer clearly "it depends". Ask yourself: will code like this.foo2 = new Foo(); this.callback(foo2, this.n); result in correctly working ...
Doc Brown's user avatar
  • 210k
0 votes

When should a function be given an argument vs getting the data itself?

From "The Clean Code": Statements in a function should be the same level of abstraction If you follow this rules, your functions will either be functions that do one low-level thing or ...
Falco's user avatar
  • 1,301
7 votes
Accepted

Is a callback function with `this` as an argument a bad practice?

No I think it's fairly common for events to pass the object raising the event and the event arguments to a handler. I've seen it said that this is so events can be bubbled up through several handlers ...
Ewan's user avatar
  • 77.4k
0 votes

When should a function be given an argument vs getting the data itself?

If performance is not a concern, which sure is not in 99 percent of cases, then you should just strive for writing maintainable code. There is, I dare to say, only one rule to follow when writing ...
Atif's user avatar
  • 163
2 votes

When should a function be given an argument vs getting the data itself?

The problem with simplified examples is that, being simple and straightforward, they don't press against the relevant sensory and cognitive limitations that we're trying to deal with by folding things ...
Steve's user avatar
  • 9,232
-1 votes

When should a function be given an argument vs getting the data itself?

There are plenty of situations where the called function cannot know certain data. Let’s say I have a function returning my income tax. The tax depends on my circumstances and my income. So you think ...
gnasher729's user avatar
2 votes

When should a function be given an argument vs getting the data itself?

There can be a lot of reasons to chose the one or the other. The main drivers are: Separation of concerns: When the function gets the data itself, it constraints itself to one single way of getting ...
Christophe's user avatar
  • 78.7k
42 votes
Accepted

When should a function be given an argument vs getting the data itself?

When should a function be given an argument vs getting the data itself? When it has a job besides getting the data. I can see why you'd be confused about this. None of your functions are named after ...
candied_orange's user avatar
10 votes

When should a function be given an argument vs getting the data itself?

As usual, there are various influences on whether to do one or the other. Is it likely that you will at some point have to do the same processing on different data (or some other processing on the ...
Kilian Foth's user avatar
0 votes

In C++, why shouldn't all function parameters be references?

Pass by value gives you a copy of the value, pass by reference gives you a reference to the value. If you have a value on the register, you can use that directly, if it is a reference to a value, you ...
dtech's user avatar
  • 723
0 votes

Is there a reason to not modify values of parameters passed by value?

Because of pointers. A pointer maybe passed by value but the object can be mutable. To stay in the contrived example: public double GetArea(Circle C) { C.radius = C.radius * C.radius; C.radius ...
Pieter B's user avatar
  • 13.2k
1 vote

Is there a reason to not modify values of parameters passed by value?

I very rarely find myself re-assigning parameters in the method body. That's not because I have an objection to that per-se, but because of naming considerations. In a well-designed method, parameter ...
PMah's user avatar
  • 159
1 vote

Is there a reason to not modify values of parameters passed by value?

In C++, a parameter can have type (for example) int i, const int i, int& i, and const int& i. References obviously work different, but if you want to prevent assignment to i, you just make it ...
gnasher729's user avatar
3 votes

Why do you need to use pass by reference in C++ to change the value of the arguments inside the function?

Going back to the good old analogy of notes written on bits of paper and put in pockets. Call by value int i = 42; This means "find a new bit of paper, write "42" on it, and put it in ...
Philip Kendall's user avatar
3 votes
Accepted

Why do you need to use pass by reference in C++ to change the value of the arguments inside the function?

Let’s say you have a variable int x in your function. You call a function with a parameter int i, like f(x); You passed a value. Inside the function f the parameter is turned into a variable, as if ...
gnasher729's user avatar

Top 50 recent answers are included