Hot answers tagged

242

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 can be applied multiple times without changing the result beyond the initial application. In mathematics, this means that if f is idempotent, f(f(x)) = f(x), ...


69

Sure there is a good reason to name it more explicitly. It's not primarily be the method definition that should be self-explanatory, but the method use. And while findById(string id) and find(string id) are both self-explanatory, there is a huge difference between findById("BOB") and find("BOB"). In the former case you know that the random literal is, in ...


51

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 function are two: it must be idempotent and it must not have side effects, which is to say, no mutation of local static variables, non-local variables, ...


37

Advantages of FindById(). Future-proofing: If you start with Find(int), and later have to add other methods (FindByName(string), FindByLegacyId(int), FindByCustomerId(int), FindByOrderId(int), etc), people like me tend to spend ages looking for FindById(int). Not really a problem if you can and will change Find(int) to FindById(int) once it becomes ...


10

Another way of thinking about this is to use the type safety of the language. You can implement a method such as: Find(FirstName name); Where FirstName is a simple object that wraps a string which contains the first name and means there can be no confusion as to what the method is doing, nor in the arguments with which it is called.


8

This is an event handler, in addition to being a method. The convention for naming event handlers is EventOrigin_EventName(...). Event origin can be a name of an instance (camel case), or a name of a class (Pascal case), or some other meaningful name (Pascal case). Event name is Pascal case. Underscore between. Visual Studio uses this naming convention ...


8

The convention is that On____ methods are ones triggered by some other occurrence (described by the blank). Sometimes that is a literal C# event, sometimes not. How it is triggered is an implementation detail. The name is meant to convey the design of “this triggers after _______”, not an implementation detail.


8

Not to get all philosophical, but a branch in git is an alternate reality of your code base — a parallel universe — in which a new feature or defect fix is developed in isolation from all other code changes. A branch in git should not be viewed as a "folder" containing files. It should be viewed as a timeline recording independent and isolated ...


6

If possible, use a different naming scheme that allows for more meaningful and less ambiguous names. Then choose names so that they reflect what the message is (or what kind of data it contains), rather then who the sender/recipient is. You could then document the kinds of messages each party can send. Depending on the domain, the names themselves, if ...


6

The Term is Idempotence. Note below that there is a distinct difference between an Idempotent function (Called recursively on itself; Second code block and the Mathematical definition), and functional idempotence (Called repeatedly with same input sequentially; First code block and often the term meant in Programming). A function f with side effects is ...


6

It entirely depends on whether it is a constant and won't likely change or if it is a constant which is symbolic or representative of something else and therefore may change. An example of a constant which won't likely change is: const PI=3.1415926 const RED=[255,0,0] In this case, call it as it is. It represents the value of PI, not some arbitrary float....


5

I cannot figure out a good way to explain why words should be in a specific order, except for "It how it's written in English" That's because it is the only reason for writing it this way. There is a naive view that there is a consensus that all programming should be done in american English. But this isn't really the case, let the developers choose ...


5

"File Path" is a noun phrase. You want to find a way to explain the syntax of noun phrases in english, in particular where the head noun appears (at the end in this and similar phrases), and that the the value returned should be an instance of the thing named by the head noun. Your function presumably returns a path, in particular the path of a file. If you ...


5

Generally there are two schools of thought on this: Multiple types with the same name are fine if the name makes sense for them all. Differentiate them via a package/namespace/module etc name. Avoid multiple types with the same name as one then has to reply on the package/namespace/module etc name to differentiate them, which can be confusing. As a general ...


5

The idea behind the Ubiquitous Language of DDD is that no translations take place between business-related terms. This means that if you need to present your design to the business side of the company (or they happen to see your source code), you don't need to explain to them why you have chosen those names for the domain concepts, because you are using the ...


4

A literal solution to this is to name your font sizes like their size parameter: { size8: '8px', size11: '11px', size14: '14px', } This gives you exactly what you asked for - an option for trivial insertions at any point of the scale. One can ask if introducing such symbolic names, where the pixel size is just repeated in the name, is really ...


4

If the variables $flow_curve, $curves, and $graphs belong to a bigger "something", give that "something" a name and create a class from it (maybe it is the Graph class you mentioned? Or maybe some GraphCollection?). Find an abstraction which suits your needs best. Then the code could be naturally placed in the constructor of that class - and that is the name ...


4

The closest term I know is SUT (System Under Test). I want to note that I have seen it used to refer to a whole module (like a dll), however you seem to call module to a class. Regardless, I found the following: The "system under test". It is short for "whatever thing we are testing" and is always defined from the perspective of the test. When we are ...


4

Have a look at Data Driven Tests Essentially you can refactor your tests to something like : ["sales", 34] ["finance",12] GetEmployeeTest(string dept, int expectedCount) { ... } The test framework will pass in the parameters from the data block and name the tests accordingly so you can see them in the results.


4

A good explanation for why it was named that comes from looking at motivating examples of what UNPIVOT does, and implementing it in terms of older SQL expressions. Consider that you have an intermediate resultset with multiple possible sources for a given value, e.g. from a bunch of LEFT JOINs. You wish to "combine or merge" all these mostly null sources ...


4

While its nice to be able to make the code write sentences, outside of limited situations it becomes impractical. For a start English is too vague to separate clearly things like character.Has(sword) and character.Has(measles) with single words. Secondly if(character.Has(sword)) reads nicely but, say we want to check the character the monster is attacking ...


3

Without knowing how your API looks, it's kind of a shot in the dark, but the term that came into my mind was facade a facade is an object that serves as a front-facing interface masking more complex underlying or structural code. (Wikipedia) I know that the definition refers to an object, not a whole API, but it may be suitable anyway.


3

Vague apologies if this comes across as a bit harsh. One thing to remember here: MATLAB is a tool developed by engineers for engineers; it has never really had a focus on top-quality software engineering so definitely encourages what would be considered as bad software engineering practices. My work benefits from names that contain information about the ...


3

In physics I've heard this referred to as a projection: a projection is a linear transformation P from a vector space to itself such that P2 = P. That is, whenever P is applied twice to any value, it gives the same result as if it were applied once (idempotent). Graphically, this makes sense if you look at a cartoon of a vector projection: In the ...


3

It is a Deterministic algorithm because given the same input (in this case no input), it will always produce the same output. In computer science, a deterministic algorithm is an algorithm which, given a particular input, will always produce the same output, with the underlying machine always passing through the same sequence of states. Deterministic ...


3

In addition to the other answers, if there is a specific input to the functon that has this property, it is a fixed point, invariant point or fixpoint of the function. For example, 1 to any power is equal to 1, so (1ⁿ)ⁿ = 1ⁿ = 1. The special case of a program that produces itself as output is a quine.


3

COALESCE isn't supposed to be used on a data series. It's supposed to receive several explicit expressions as arguments, one of which you hope will be non-NULL. Think of it as SQL's version of nested X ? Y : B operators, used to express several levels of fallback values when the normal value is NULL.


3

First, in the shown case, it makes no sense to introduce an explaining variable since using the function directly is equally readable, so you could write: function hello(suffix) { if (isSuffixString(suffix)) { return `Hello ${suffix}` } else { return 'Invalid entry' } } However, if you need the result of a function call more than once, it ...


2

How about the opposite question? Some argue that spaces in filenames are good because they look nice. Do they? Most of the time, you cannot tell if that's a single space, a tab, or a couple of spaces. Actually, there is a suite of unprintable characters which might be used in filenames. Nowadays, most systems support UTF-8, so it is possible to compose ...


2

Unless the purpose of the class is to provide the date and time to other classes, the class should not depend on the time, at all. It should depend on data. And the time should be provided to it as data. So for example the class could accept an injection of a service that exposes a method or property that returns the date and time. The class should not ...


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