Hot answers tagged

243

I'm going to come out and say it: It's not really a bad practice (and even if it is, its not that bad). You could make the argument (as Chad pointed out) that it can mask errors like in the following query: SELECT * FROM cars car JOIN manufacturer mfg ON mfg.Id = car.ManufacturerId JOIN models mod ON mod.Id = car.ModelId ...


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


234

It appears that these variable names are based on the abbreviations you'd expect to find in a physics textbook working various optics problems. This is one of the situations where short variable names are often preferable to longer variable names. If you have physicists (or people that are accustomed to working the equations out by hand) that are ...


203

I had the same question about a year ago so I looked at some code myself. Here is what I found (constants were ALL_CAPS in every project, by the way): ╔═══════════════════════╦═════════════╦════════════╦══════════════╦════════════╦════════════╗ ║ PHP Project ║ Classes ║ Methods ║ Properties ║ Functions ║ Variables ║ ╠════════════════════...


191

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


146

Because its orginal intention (see http://www.joelonsoftware.com/articles/Wrong.html and http://fplanque.net/Blog/devblog/2005/05/11/hungarian_notation_on_steroids) has been misunderstood and it has been (ab)used to help people remember what type a variable is when the language they use is not statically typed. In any statically typed language you do not ...


122

Because when you have a table with a foreign key you can't name that foreign key "Id". You have table name it TableId And then your join looks like SELECT * FROM cars c JOIN manufacturer m ON m.Id = c.ManufacturerId And ideally, your condition should have the same field name on each sides SELECT * FROM cars c JOIN manufacturer m ON m.ManufacturerId = c....


120

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


110

Consistency is king; pick one or the other, but do it consistently everywhere. That said, I prefer the first variation, because it doesn't violate camelCase (doing so means you have two style rules to remember, not just one). Two capital letters is sometimes used because of this, but an ID is really just a form of Id-entification.


106

The reason the software uses those names is because the datasheets use those names. Since code at that level is very difficult to understand without the datasheet anyway, making variable names you can't search is extremely unhelpful. That brings up the question of why datasheets use short names. That's probably because you often need to present the names ...


104

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


90

Semantic Versioning (http://semver.org/) deserves a mention here. It is a public specification for a versioning scheme, in the form of [Major].[Minor].[Patch]. The motivation for this scheme is to communicate meaning with the version number.


89

Variables with short lifetimes should be named shortly. As an example, you don't write for(int arrayCounter = 0; arrayCounter < 10; arrayCounter++) { .... Instead, you use for(int i .... In general rule of thumb it could be said that the shorter the variable scope the shorter the name should be. Loop counters are often only single letters, say i, j and k....


82

I use Get when I know the retrieval time will be very short (as in a lookup from a hash table or btree). Find implies a search process or computational algorithm that requires a "longer" period of time to execute (for some arbitrary value of longer).


79

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


75

TL;DR: In the context of .NET class libraries, Microsoft recommends that you use Id. This is slightly counter-intuitive, since it's a rare example of an abbreviation that is allowed / recommended (abbreviations are generally frowned upon). If we're talking about C# or .NET class library conventions, Microsoft has some fairly well defined naming guidelines ...


74

Hungarian notation is a naming anti-pattern in modern day programming environments and form of Tautology. It uselessly repeats information with no benefit and additional maintenance overhead. What happens when you change your int to a different type like long, now you have to search and replace your entire code base to rename all the variables or they are ...


71

The only thing I pluralize is collections. foreach (var customer in customers) { // do something with customer } All of your examples are individual objects, so they are not pluralized. Yes, the names refer to objects that might have multiple instances, but all you need to know in the name is the object entity (i.e. customer). So in all of your ...


68

I took a very pleasant tour through W3C, Google, and Wikipedia and finally found the answer: an annotated XML spec where we find an excerpt of an email from the inventor of the name, James Clark, an email from chairman Jon Bosak who suggested to use the X letter, and some other ideas for names and the final votes: Votes | Acronym | Full Name ------+---------...


68

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


67

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


66

Ruby's ActiveRecord library and Groovy's GORM use "id" for the surrogate key by default. I like this practice. Duplicating the table name in each column name is redundant, tedious to write, and more tedious to read.


65

If you stop to think about it, you'll see that an interface really isn't semantically much different from an abstract class: Both have methods and/or properties (behaviour); Neither should have non-private fields (data); Neither can be instantiated directly; Deriving from one means implementing any abstract methods it has, unless the derived type is also ...


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


62

I used to use it (many years ago) and I don't anymore. The main reason is it's superfluous in the OO languages with strong typing (C++, Java) which I happen to have used most of my career. In these languages, if I define my types well, the compiler can and will enforce type safety for me. So any naming prefixes are just clutter which make the names longer, ...


61

Use singular. The tool to turn screws with is called "screw driver" not "screws driver". However, pluralize your method and property names accordingly, to indicate whether one value or a collection of them will be returned.


61

Should boolean methods always take the affirmative form, even when they will only ever be used in the negative form? Making rules about such things seems a little much -- I wouldn't want to see a guideline in a coding standards document that says thou shalt not use negative names for boolean properties. But as a matter of personal style, I think trying to ...


61

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


60

All names should be meaningful. If _ was a well known standard at your company or in the wider community, then it would be meaningful as a "name that does not matter". If it's not, I would say it's bad practice. Use a descriptive name for what you refer to, especially since the name might matter in the future.


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