Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

re-edited by author: no this is not 2 questions. This is one question about code review questions containing two separate points. Please do not edit my question.

For naming variables, the two sides are regarding using articles (a, an, the) in the variable name (e.g. theBook vs. book -or- parseTheString vs. parseString).

For hard-coding strings, the three sides are whether to put all strings into constants (public static final) in a separate file (class or interface), put them all in the same file (private static final), or don't use constants for strings at all. (e.g. new Button("Click Me") vs. new Button(BUTTON_LABEL_CLICK)). We also discuss the Hungarian notation issue quite a bit but I've seen that discussed here in other questions.

For a very long time it has been the defacto standard to not use articles in variable names (adds visual clutter without adding value) and to not have hard-coded strings sprinkled through the source code (a potential maintenance nightmare and very poor for internationalization). Now we are starting to see violations of these and the developers seem truly surprised when someone marks these as defects or at least needing attention. The manager has said they don't care one way or the other and these points are not in our company coding standards.

share|improve this question
6  
why not ask two separate questions instead of bundling two for one? –  HorusKol Jun 12 '13 at 22:09
add comment

3 Answers

up vote 10 down vote accepted

Articles in Variable Names

They add little to no value in most cases, but make the code longer to read.

Usually, I'd rarely have a the need for the distinction between a "a" or "the" in the flow of a function, so it doesn't make sense to use articles.

It's a formal programming language and I am fine with it not reading exactly like a natural language. In fact, it's probably better this way, not everything's necessarily so great about natural languages.

There are a few cases where readability can benefit from using articles though, for instance if you want to implement a checker for a "is a" relationship with a method signature like boolean isA(Object o, Class<?> cl) (I'm not advocating this to be a good idea).

Hard-Coded Strings

It depends on whether you plan to reuse them or not, and whether that reuse is driven by semantics (and not just the fact that the String has the same content in different places) and spread across multiples classes or sub-systems.

To that extent, I usually use these rules:

  1. If you have the same string multiple times and it has the same meaning acros multiple classes, then extract to constants in an interface.

  2. If you have the same string multiple times within a single class and it has the same meaning within that class, then extract to constants within the class.

  3. If you have only a few number of repetitions of as tring, or they just happen to be identical but aren't guaranteed to always be in the future, then I'd leave them inlined.

  4. If any of the above would render maintenance difficult, screw rules 1 to 3 and do whatever works best for you.

Of course, make sure that the constant's name makes the code as semantically valid as the string it holds. That also applies for other things that aren't strings (like Predicates, for instance).

share|improve this answer
4  
hard coded strings makes for very difficult internationalisation - but if this isn't even a long term goal (ie, you know that your market will always be limited to one language) then it isn't much of a problem. –  HorusKol Jun 12 '13 at 22:12
    
@HorusKol: you mean when they're extracted to an interface? I don't find that either approach makes it easier or harder for I18N in general. The only annoyance with I18N is that usually you'll end up with the "key" Strings that aren't necessarily aptly named, and that's when the semantics get lost and you don't know what you're looking at anymore. Also, you then need to convey to your analysis tools that these strings are meant to be here and shouldn't be extracted to constants or other things because they're just keys anyways. –  haylem Jun 12 '13 at 22:15
    
@haylem - yes I agree that "isa" and "hasa" are special cases for the "articles" rule. –  Iceberg Jun 13 '13 at 0:54
    
@haylem - re hard-coded strings: you make reasonable points but we must also consider that we cannot predict the future. We may know what is intended now re how it is to fit into the world. The future is unknowable with certainty so for long-living code, it is most flexible and easiest to maintain/re-fit/upgrade by making as many things as possible (key phrase there) as flexible as we can. Having to edit one or a few fairly small classes as opposed to picking through tens of thousands of lines of code (grep notwithstanding) and hope you got them all - the difference is clear to me at least. –  Iceberg Jun 13 '13 at 1:03
1  
@Iceberg: It's true that you can't predict the future, so be careful not to plan unnecessarily for it. Quite likely, YAGNI. Sometimes, it's more cost-effective, even on the long term, to focus on what you need for now rather than what you may need later. All depends on your project of course, but we don't know what you aim for here. –  haylem Jun 13 '13 at 7:59
show 3 more comments

For hard-coding strings, the three sides are whether to put all strings into constants (public static final) in a separate file (class or interface), put them all in the same file (private static final), or don't use constants for strings at all. (e.g. new Button("Click Me") vs. new Button(BUTTON_LABEL_CLICK)). We also discuss the Hungarian notation issue quite a bit but I've seen that discussed here in other questions.

If you want a high-quality user experience, anything that is presented to the user (button labels, error messages, etc.) must be reviewed by the UI designer. In my experience, when developers are hard-coding text, that doesn't happen. The developers do the best they can, but the result is a set of messages and labels that are little better than random.

share|improve this answer
    
another good point. And when the text is mandated to change? much harder and more time consuming to make it so. Thanks everyone. It's good to hear the different viewpoints. –  Iceberg Jun 13 '13 at 15:47
add comment

Apple use articles - inconsistently - in their ObjC code. It adds nothing to the legibility of their code or the descriptiveness of their variable names:

- (id)viewWithTag:(NSInteger)aTag;
- (id)initWithFrame:(NSRect)frameRect mode:(int)aMode cellClass:(Class)factoryId numberOfRows:(int)rowsHigh numberOfColumns:(int)colsWide;

Note aTag and aMode vs frameRect.

I hate the semi-standard and don't use it. It makes the code sound moronic.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.