Hot answers tagged

245

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), ...


189

Whilst many, including "Uncle Bob", advise not to use I as a prefix for interfaces, doing so is a well-established tradition with C#. In general terms, it should be avoided. But if you are writing C#, you really should follow that language's conventions and use it. Not doing so will cause huge confusion with anyone else familiar with C# who tries to read ...


154

Yes, you understand Clean Code right, but your examples are quite a bit over the top. Here is what you start with: PageReloaderForPagesDisplayingVectorGraphicsThatAreUsedInTheEditorComments PageReloaderForPagesDisplayingVectorGraphicsThatAreUsedInTheEditorDescriptions A "Page Reloader" presumably reloads pages, so the "Pages" part is redundant. This ...


122

A toXYZ() function is expected to do a conversion, and to return a new independent object (though immutability allows for optimization, java.lang.String.toString() just returns the object). As an example, in C++ we have std::bitset::to_ulong() which can easily fail, and a whole plethora of to_string(), all doing a (more or less) complex conversion and ...


118

Is vs. Can According to the Microsoft naming convention recommendations, both "Is" and "Can" are OK (and so is "Has") as a prefix for a Boolean. In plain English, "Is" would be used to identify something about the type itself, not what it can do. For example, IsFixed, IsDerivedFrom, IsNullable can all be found in CLR types and methods. In all of these ...


84

I don't think anyone can explain it better than Martin Fowler does, further down the article you linked to. For this new breed of containers the inversion is about how they lookup a plugin implementation. In my naive example the lister looked up the finder implementation by directly instantiating it. This stops the finder from being a plugin. The approach ...


71

Let's say you have some sort of "repository" class, and that repository is responsible for handing data to you from a data source. The repository could establish a connection to the data source by itself. But what if it allowed you to pass in a connection to the data source through the repository's constructor? By allowing the caller to provide the ...


69

In .NET, you often have pairs of methods where one of them might throw an exception (DoStuff), and the other returns a Boolean status and, on successful execution, the actual result via an out parameter (TryDoStuff). (Microsoft calls this the "Try-Parse Pattern", since perhaps the most prominent example for it are the TryParse methods of various primitive ...


68

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 ...


64

For an enum option you should use title case like Default. Since C# is case-sensitive it will not collide with the reserved keyword. See .net Naming Guidelines. Since all public members should be title case in .net, and all reserved names are lower case, you shouldn't really encounter this except with local variables (including parameters). And locals would ...


63

Not really, as booleans are not always used to indicate that an object "is" something. "has" is an equally valid prefix "was", "can" are also valid in particular circumstances, also, I have seen the suffix "Able" used. So Object herring:- isFish = true isCat = false hasScales = true hasFur = false canSwim = true wasEgg = true eatAble = true Object ...


59

English is a lingua franca/lowest common denominator for a reason. Even if the reason is conceptually as weak as "Everybody does it", that's still a rather important reason. Going against common practice means that you have to understand Dutch to make sense of the data structures in yor software. There's nothing wrong with Dutch, but the probability that ...


56

A function name that contains and is at the wrong level of abstraction. I lean towards addResponseIdToDB() because otherwise the ‘side effect’ is a complete surprise. However: responseIds = addResponseIdToDB(); doesn’t leave anyone surprised. The command query responsibility segregation principle argues that this should not be the only way to get the ...


52

Preface Hopefully this is obvious, but... in the suggested namespaces below, you would replace MyCompany and MyProject with the actual names of your company and project. DTOs I would recommend using the same DTO classes across all layers. Fewer points of maintenance that way. I usually put them under a MyCompany.MyProject.Models namespace, in their own VS ...


50

AbstractFactory is indeed a poor choice for a name. There is no way to know what is created by this factory, and when you'll look for an entity which creates Animals, you'll never find the corresponding factory by name. AnimalAbstractFactory is not a wise choice neither, since in most languages, it would be redundant with the abstract keyword in the ...


49

Golden Hammer The golden hammer is a tool chosen only because it is fancy. It is neither cost-effective nor efficient at performing the intended task. source: xkcd 801 (Despite the down-votes, I stand by this answer. It might not exactly be the opposite of re-inventing the wheel semantically, but It fits every example mentioned in the question)


49

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, ...


47

But why do modern languages like python and go still use the less readable names from the C apis? Because everyone (in their domain) knows the short names. If you made different names, people would question "what makes this different from mkdir?" and generally get confused that stuff isn't where they expect it. Because without auto-complete in an IDE, long ...


45

Not only is this a reasonable practice, the language was specifically designed to permit this. Search the C# specification for "Color Color" for the rules and a justification, and see the Microsoft doc on Color Color for some interesting corner cases that arise from this decision. Under no circumstances should you name a property "DogObject&...


43

Why do we continue to use the old Germanic "Albert" as a boys name and not the modern English translation "Shining Hero"? Because a name is just a label. It's good to give new stuff a meaningful name, but, after long usage the name itself becomes the meaning. printf means something very specific to generations of programmers -- a print function with a ...


39

The interface is the important logical concept, hence, the interface should carry the generic name. So, I'd rather have interface Something class DefaultSomething : Something class MockSomething : Something than interface ISomething class Something : ISomething class MockSomething : ISomething The latter has several isues: Something is only one ...


39

Why prefixes in the first place? The prefix for function names is a C practice that intends to avoid naming conflicts. This is especially suitable in big projects, where different teams could easily come with do_this() and do_that() in different subcomponents of a large codebase. Since C lacks of a namespace or a package feature, the prefix is the most ...


38

This is not a C++ library issue but a question of mathematical terminology. In mathematics, a norm can mean different things: What you call norm is the Euclidian norm, which is the distance to the origin. In C++ it's abs(). This naming convention has the advantage of being consistent for complex and for real numbers (the origin in the latter case being ...


37

Consider the cost/benefit ratio of your two options: Would reusing the same name cause confusion or naming conflicts? Probably not, since they're in different folders. The name "player_stats/generator.js" is equivalent to "player_stats_generator.js". However, if you see, in the future, a reason to merge your js files into a single directory (deployment? I ...


35

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 ...


34

Robert Martin uses the term "Framework Bound" to refer to the most obvious negative consequence of this anti-pattern. As I don't think there's any common name for the pattern itself, a reference to this consequence could suffice for most purposes.


33

Here's the "regular" control flow programs usually followed: Run commands sequentially You maintain control over the control flow of the program Inversion of Control "inverts" that control flow, meaning it flips it on its head: Your program doesn't control the flow anymore. Rather than calling commands as you see fit, you wait for someone else to call you....


33

In this scenario, I would leave the enum values in Dutch: public enum Department { BOUW, ONDERHOUD } Because the logic using these constants will be matching against data that is also in Dutch. For example, if the input is "bouw", the comparison code might look like: if (Department.BOUW == input.toUpper()) I find it easier to debug when the values match (...


32

There is a nice document that contains a lot of rules that you should follow to be in line with Microsoft: Framework Design Guidelines. One thing that you should change: Do not name classes as their namespace. That will lead to compiler mixups. Just don't. Find a better name for either the class of the namespace.


30

the way git works is that a branch name is just a pointer to a specific commit. Once you merge a hotfix branch into master, your hotfix and master will point to exactly the same place in the commit tree. As you make more commits on master, the hotfix branch will continue pointing at the same place while master will get updated. Your history will always be ...


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