Although grammatically incorrect, when writing identifiers for functions, variables etc. does it make sense to simply append an "s" to plurals of words ending in Y? My reason for this would be that if you need to find-and-replace, for example, replacing "company" with "vendor", "company" would match both singular and plural forms ("company and "companys"), whereas if the plural was spelled correctly, you would have to do two separate searches.

  • 8
    What about childs, mouses, knifes, wolfes, mans, womans, wifes and tooths? Everything is valid in order to avoid the dreaded "two separate searches". – Tulains Córdova Jul 27 '15 at 12:44
  • 3
    I'm partial to wolfys over wolfies myself... – Jimmy Hoffa Jul 27 '15 at 12:50
  • 2
    Do you really plan to be renaming identifiers very often in your workflow? It seems a bit overkill to take on all the cognitive wackiness of mispelled names in the code just to support a function that might not ever be used. – Kent A. Jul 27 '15 at 13:11
  • 10
    Believe me, you do not want to apply a find-and-replace operation over a large codebase for a word like "company" without a "full-words-only" constraint. Thus you will have to use two separate searches, either. – Doc Brown Jul 27 '15 at 13:14
  • 2
    Two. Separate. Searches. – Tulains Córdova Jul 27 '15 at 13:20

Any such search and replace should be performed with care and each change manually checked to eg avoid "accompany" in a comment becoming "acvendor" with your company/vendor change. As such, two separate searches for "company" and "companies" should not create a significant overhead compared with the time spent inspecting and approving each change.

So misspelling words to achieve only one search offers the negatives of looking bad and being more difficult to read than it need be, without offering any obvious benefit.

  • 11
    And, with refactoring tools becoming better, global search and replace based only on the text is becoming the least reliable way to rename an identifier. – Kent A. Jul 27 '15 at 13:07
  • This would not be an issue using a "whole words only" search, as mentioned by Doc Brown. – SHNC Jul 27 '15 at 14:04
  • 6
    @user3047082, whole word search wouldn't match "companys" either, making the whole question rather pointless... – David Arno Jul 27 '15 at 14:42

I'm assuming you're talking about renaming in source code files. With today's IDEs this should always be done with the IDE's refactoring tools. If your IDE doesn't have this, consider switching to another IDE. Most IDE refactoring tools also keep a history of refactoring, giving you the ability to quickly "undo" if you don't like the results of the refactor. Using search/replace, you may not have the ability to undo the whole set of changes (unless you maybe use your revision control tools and revert to previously committed version). Also, using refactoring tools you're safer from inadvertently changing something you didn't intend to change.

  • 3
    To whomever flagged this answer as low quality: while it does not strictly answer the question as asked, it addresses the bigger picture of why would one want to use such a naming scheme and a better way of accomplishing the same task. I voted "looks OK" to the flag review and added an upvote. – user22815 Jul 28 '15 at 0:13
  • Thanks, @Snowman. It's human nature, when in an unknown area, to frame a question based on your own (inexperienced) mindset. Yes, while I didn't answer the actual question, my assumption of what was really being asked was based on other clues in the question. The plurals I've encountered most are from code generation tools, e.g., XSD-to-POJO, Hibernate/JPA reverse engineering from database, etc. Essentially, if these plurals are in the code and the author didn't like the choice of plurals, then they most likely weren't written by the author and were more likely auto-generated. – javabeano Aug 6 '15 at 16:48

Yes! Yes! Yes! It makes perfect sense to do that. And I have been doing it for years.

Disclosure 1: English is not my native language.

Disclosure 2: My knowledge of English grammar is considerably better than that of the average native speaker.

Disclosure 3: When it comes to communicating with humans, I am a vehement grammar Nazi.

And now that these disclosures are out of the way, let me state that English grammar has no place in code. You see, that's why it is called code and not prose. It is supposed to have some resemblance to a language understood by humans, for the purpose of readability, but other than that, what we mostly need from code is not the qualities of prose; it is other, more technical qualities, like precision, unambiguity, and terseness. That's why the C syntax of if( x != y ) y++; is much preferable to the IF X IS NOT EQUAL TO Y THEN ADD 1 TO Y END-IF. syntax of Cobol. The alleged desirability of compilers that understand natural language is a fallacy, and don't take my word for it, see what ol'Edsger has to say about it: Edsger W. Dijkstra, On the foolishness of "natural language programming".

Another quality which is of importance is computability of identifiers. The fact that a property called Color can always be read via a method called getColor() and written via a method called setColor() is of paramount importance. These identifiers are computable from the name of the property, so you do not have to know them by heart. If a programmer was to choose a pair of methods called getColor() on one hand, but colorize() on the other hand, their colleagues would rightfully consider this sabotage. That's how important identifier computability is.

Also, programming tools can be written (and plenty of them have in fact been written, for example, Hibernate) which can compute these names. Without identifier name computability you would have to use additional syntax (e.g. in Hibernate, extra annotations) to specify to each tool precisely how to create every single identifier name, or precisely which ad hoc name you have given to each entity.

So, identifier computability is important, while at the same time English grammar is irrelevant, (since we are not doing natural language programming,) so to be able to compute the name of a collection of entities by always appending "s" to the name of a single instance makes perfect sense, never mind the fact that it violates most people's (mine included) English language sensitivities.

And whether we like it or not, this is the trend of the future. The native language of the majority of programmers on the planet is not English anymore, and the trend is to continue very strong in this direction. (Also, I would not even be willing to bet money on the suggestion that English is the native language of the majority of programmers working in the USA right now.) These are people who, to a large extent, when trying to calculate the name of a collection from the name of a single instance of "company", will simply append an "s", and the form "companies" will not even cross their mind. To a great and ever increasing percentage of programmers in the world, knowledge of the peculiarities of the English language does not add any value to their work, it only makes it slightly harder.

  • 7
    1. Indexes and Indices are both valid plurals of index. You are mistaken in your belief that only one is correct, eg please see oxforddictionaries.com/definition/english/index. 2. A private X, that can be read via getX and written to via setX isn't private; it's a public value. 3. Code is a design document. It informs a compiler on how to generate "machine code", but way more importantly, it describes that design in a way that other people can easily read. Readability is one of the two most important aspects to code (testability being the other). – David Arno Jul 27 '15 at 15:31
  • 3
    Code is written for two reasons: for humans to read and for computers to execute. Some would say source code is written primarily for humans to read, since most of it gets immediately compiled into byte code before the computer executes it. So, while the computers may not care about proper grammar, the humans certainly do. – Eric King Jul 27 '15 at 20:33
  • 3
    While English may not be your native language, it might be the next person that reads your code. It will probably also confuse others of your native tongue that have learned English. – Andy Jul 27 '15 at 21:11
  • 2
    or you're only going to hurt those that double take at Companys when it should be Companies. After all, the entire point of the code we usually write is in fact to make it closer to natural language. – Andy Jul 28 '15 at 12:32
  • 2
    Whatever, I find that paper full of shit. Code is between natural language and bytecode. If not for easing human understanding of code, there wouldn't be any reason not just to enter bytecode directly. – Andy Jul 28 '15 at 14:53
