Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Can we discuss Variable Naming Conventions for a minute?

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #165194
    mmcgu1966
    Participant

    I know, just like bracket-style, there are about as many variable-name conventions as there are programmers. I wanted to throw-out what I do and get feedback on it to see if it’s crazy, or brilliant.

    I don’t use a single naming convention, but I do have a carefully thought-out style.

    for coding (asp, php, jQuery, whatever) I use camel-case variable names (thisForExample), and
    for styling with CSS I like to use underscore variables (like_this_name).

    The reason I use two radically different styles of variable names is to make then easy to keep apart (especially in javascript where you can have both together). Other programmers I work with grump about this but I insist that it’s important to tell them apart at a glance.

    #165205
    __
    Participant

    Other programmers I work with grump about this but I insist that it’s important to tell them apart at a glance.

    Well, it is certainly important that you can read your code.

    Consider, though, that if you’re working with other programmers, it is equally important that they can also read your code.

    for styling with CSS I like to use underscore variables

    CSS doesn’t have variables. SASS does, of course, and CSS will have variables eventually; but neither of these will ever exist “in javascript.” If you mean “class names” or “selectors” or something that you might use in a javascript variable, you might use a naming convention like

    var cssSelector_id = "#";
    var cssClassname_box = "box-class-name";
    

    (or some much shorter prefix, or whatever you desire).

    As for variables themselves, I think it is much more beneficial (and less confusing) to have conventions that represent the use/content of the variables themselves, rather than representing which language they are written in.

    Taking JS as an example, there are already many well-established (and also useful) naming conventions. For example:

    • variable names generally use camelCase.
    • variables that begin with _ are meant to be treated as “private” (though they may or may not actually be private).
    • variables that contain constructor functions use PascalCase.
    • global variables sometimes use ALLCAPS (which I think of as a subtle reminder that it is best to NOT USE THEM AT ALL).
    • browser-defined “system” variables often start and end with double-underscores (e.g., __proto__).
    • it is not necessarily “best practice,” but it is becoming more common for variables that contain jQuery objects to start with $.

    I find all of these to be Good Ideas. As personal practice, I also use underscores as a separator character for variables or method names that are logically related.

    For example, I might have a function that gets an item from an array; I’ll name it (obviously) getItem.

    If I had another function that does the same thing, but looks for a specific item (say, by its index), I might name it getItem_byIndex.

    Guess what the function getItem_byValue does?

    #165219
    __
    Participant

    Yeah, that’s what I figured: so, in CSS context, what sorts of class names do you need to distinguish between? smacss might be a good read for you.

    In JS context, it’s all “just strings” – so it’s more-or-less “up to you” (and your coworkers) if you want/need to distinguish what sort of CSS the string contains.

    (To add to my earlier post, another convention I use is the plural form of a variable name for a collection (list) of like objects: e.g., a list of car objects might be named cars.)

    #165229
    __
    Participant

    I like making my css class names “sdrawkcab”

    Don’t you mean “ƨbɿɒwʞɔɒd”?

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Other’ is closed to new topics and replies.