Skip to content

Instantly share code, notes, and snippets.

@sohel-rana
Last active March 14, 2024 08:24
Show Gist options
  • Save sohel-rana/9e53c803b567829f4adf to your computer and use it in GitHub Desktop.
Save sohel-rana/9e53c803b567829f4adf to your computer and use it in GitHub Desktop.
Naming Convention for JavaScript

#Coding Conventions

Variables

Use camelCase for variable naming. Also use full word where possible

Good variable names: user, userAddress, stockInfo, tempUserLocalAddress etc.

Bad variable names: user\_data, new_user etc.

##Function Names Always use camelCase for function names. Also please put the documentation before the function and inside the function where applicable

Good function names: getTotal(), getUserData() etc.

Bad variable names: gettotal, get_user_data etc.

##URL hash/api Always use small case in url and api. For multiple word, use underscore to join them.

Good url and api :

http://localhost/users
http://localhost/get_users
http://localhost/api/get_user_data
etc.

Invalid urls and api

http://localhost/Users
http://localhost/getUsers
http://localhost/api/getUser_data
etc.

##File Naming

1. Controllers: use camelCase for file names.
2. Models: use camelCase for file names
3. Views/templates : use lowercase for file names. Join words by underscore. For example, create_user_schedule.js or create_user_schedule.html for template. 4. Schema: use PascalCase for file names. For example, UserSchedule.js

##Spacing Please be careful about spacing while writing code. Always put space when we have an operand.

Good Practices:

var x = 10;

if(x === 5 && y < 12)

var x = checked ? 'Do something' : 'Please check it before go to next step'

Bad practices:

var x=10;

if(x===5&&y< 12)

var x =checked?'Do something':'Please check it before go to next step'

#####Also put spacing when you concat strings

Good Practices:

var errorMsg = 'There were some errors ' + errorObj.message + '. Also check the console for details.';

Bad practices:

var errorMsg = 'There were some errors '+ errorObj.message+'. Also check the console for details.';

##Class/module Naming Always use Pascal Case for class/module names. For example,

module.exports = mongoose.model('StockPrice', StockPriceSchema);
//Here StockPrice is a valid model name. 

Also use PascalCase when in require a module. For example,

var User = require('../models/user')();
\\Here User is a valid name for require.

Using Pascal Case for class/module will help you to differentiate between local variable(camel Case) and module/class(PascalCase).

Remember that these are classes :)

// A model
define([
], function () {
  'use strict';

  var UserModel = Backbone.Model.extend({
    url: '/users',
    
    initialize: function() {
    },

    defaults: {
    },

    validate: function(attrs, options) {
    },

    parse: function(response, options)  {
      return response;
    }
  });

  return UserModel;
});
\\ A collection
define([
  'models/user'
], function (UserModel) {
  'use strict';

  var UserCollection = Backbone.Collection.extend({
    url: '/users',
    model: UserModel
  });

  return UserCollection;
});
\\ A view
define([
  'text!template/home.html'
], function (HomeTemplate) {
  'use strict';

  var HomeView = Backbone.View.extend({

    el: '#page-content',

    template: _.template(HomeTemplate),

    tagName: 'div',

    id: '',

    className: '',

    events: {},

    initialize: function () {
    },

    render: function () {
      this.$el.html(this.template());
    }
  });
  return HomeView;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment