I'm now developing an instant messaging system but I'm a little confused: I can't decide which approach I have to choose!

I want you to tell me what is good/bad with this programming approach. One friend of mine said it's difficult to read, but it works.

/*! | (c) 2012, 2013 by Bellashh*/
 /// <reference path="jquery-2.0.0-vsdoc.js" />
$(document).ready(function () {
  var ChatProvider = {
    "Friends": [],
    "People": [],
    "Conferences": [],
    "UI": {},
    "Utils": {
      "functions": {
        "alert": function (str) {
        } /*ChatProvider.Utils.functions.alert*/ ,

        "addFriend": function (friend) {
        } /*ChatProvider.Utils.functions.addFriend*/ ,

      } /*ChatProvider.Utils.functions*/ ,

      "events": {} /*ChatProvider.Utils.events*/ ,

      "settings": {

      } /*ChatProvider.Utils.settings*/
  } //var ChatProvider

    "Names": "bellash"

}); //$(document).ready

P.S: for visibility sake, I added some space between line separating objects' properties.

  • \$\begingroup\$ Quick question, from a user's (not developer's) point of view: How do we access Friends, People and Conferences? Do we get them through a function or do we access the arrays directly? \$\endgroup\$ – Joseph Apr 25 '13 at 16:37
  • 2
    \$\begingroup\$ Is this the real code you want reviewed, or some kind of example? I ask because addFriend pushes something onto the Friends array and then immediately pops it back off, which makes no sense, and the alert function seems contrived; it just wraps window.alert. In other words, it wouldn't do much good to review this code, as the code doesn't do anything useful. \$\endgroup\$ – Dagg Apr 25 '13 at 18:34
  • \$\begingroup\$ Dagg It is only an example. But I wonder if this is the best approach of coding in javascript instead of creating(bubling) separated functions in a .js file, my approach is good because it avoids conflicts between js file: am I wrong ? @JosephtheDreamer for example, to access the Friends array, you just need to write ChatProvider.Friends[index] ... \$\endgroup\$ – Bellash Apr 29 '13 at 11:23

From a once over:

  • drop functions, it is too long and it's obvious that alert in ChatProvider.utils.alert() is a function. Furthermore, specifically for alert, that should be under UI in my mind
  • Your functions are all over the place, I would think that adding a friend would be ChatProvider.friends.add() but you put it in ChatProvider.Utils.functions.addFriend()
  • You treat your object as 1 singleton, what if you need more than 1 instance ?
  • Maybe you are from a Java background, but namespacing the way you approach it should be avoided
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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