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.

If constants are structured hierarchically, do the "folders" also need to be capitalized? Take for example a Javascript object that needs to store constants for a video player. Is the convention to capitalize the "folders"?

PLAYER = {
 VIDEO: {
  WIDTH: 640,
  HEIGHT: 480
  ENCODING: 'h264'
 },
 AUDIO: {
  BITRATE: 128
  ENCODING: 'mp3'
 } 
};

// usage:
alert('The player's video width is: ' + PLAYER.VIDEO.WIDTH);

Or only capitalize the leaf nodes?

player = {
 video: {
  WIDTH: 640,  
  HEIGHT: 480,
  ENCODING: 'h264'
 },
 audio: {
  BITRATE: 128,
  ENCODING: 'mp3'
 }
};

// usage:
alert('The player's video width is: ' + player.video.WIDTH);
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

It doesn't matter. What matters is that you're consistent.

share|improve this answer
add comment

Neither. I have not seen ALL-CAPS identifiers in any popular Javascript library. The use of ALL-CAPS for constants is a leftover from C programming, when constants were defined using C macros. Macro names were in a completely separate global namespace, so we used all caps to prevent collisions. It was necessary in C, but it's not necessary in other languages, and only minimally helpful. I'm happy that it has not been popular for Javascript.

share|improve this answer
    
+1 I've seen it in some node.js libraries, but it's not exactly common. –  Ed Woodcock Jun 4 '11 at 20:45
    
Why is it not useful in Javascript to capitalize constants? Javascript has no modifiers like Java's final or C++'s const/define to make a variable constant. The only way to signify the constant nature of a variable in Javascript is make it look different from other variables. –  JoJo Jun 5 '11 at 20:23
    
@Jojo - Since there's no protection, Javascript developers rely on documentation to tell them which fields are mutable and which should not be changed. Constants may also be wrapped in functions: player.video.height() or player.video().height(). For an example, see the documentation for jQuery or ExtJs or YUI. –  kevin cline Jun 6 '11 at 2:48
    
Why should one rely on documentation to express the intent of their code? If poor code could be remedied by massive amounts of comments, then there would be no need for programmers.stackexchange.com. I believe that writing good code that "reads like a book" is better than cryptic code that needs to be explained by documentation. –  JoJo Jun 6 '11 at 3:37
    
@Jojo: It's not cryptic at all. It's just a different way of doing things. There's no protection. Library builders support the documented usage. But you are free to replace their functions or modify their internal data structures. If you do it wrong, things don't work. –  kevin cline Jun 6 '11 at 3:52
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.