Practicing Prototypal Code
I'm focusing on practicing a very prototypal structure with my code (non library oriented) to begin learning to reuse large portions of my application infrastructures where applicable in future projects. With this in mind, I don't have experience with prototypal programming before now, and would like to ensure that I'm not misusing the strategy.
Code Summary
The following server side JavaScript code has the purpose of handling a large number of Socket.io events in a modulated, prototypal way. My goal is code that is readable, but also reusable. I feel as if I may be sacrificing the clarity of my code in order to utilize a prototype for the handlers; it eliminates the inclusion of duplicate code in each handler, most of which helps with debug logging, but it may make the code less readable. I'm interested in learning whether or not this is an unavoidable aspect of using prototypes, or if there are ways that I may use prototypes without diminishing readability.
Review
Requesting general improvements, best practices, code readability, etc.
Most importantly, I'm interested in feedback on the comparison between the prototypal and non-prototypal event handling, and whether or not I'm using that strategy correctly.
Commenting
Another thing I wouldn't mind reviews to touch on is my style of commenting. I prefer to let the code speak for itself, but do like to keep each section and sub-section titled. I use all-caps on my in-line (sub-section) comments because I want them to stand out clearly when scrolling through.
Perhaps these practices should be changed. If so, please say so. Also, I notice that in reviews where large sections of code are involved, people will comment a paragraph of explanation / functionality description of that section. Is that ever a good practice?
Non-Prototypal Handlers
control_center.js
/* Server Control Center */
//REQUIRE CUSTOM MODULES
var debug = new(require("custom_modules/debug"))("Control Center");
var socketHandler = require("custom_modules/socket_handler");
//APP INITIALIZE
debug.log("Application initialized...");
settings.js
module.exports = {
"debug" : true
}
debug.js
module.exports = function debug(name) {
this.settings = require('custom_modules/settings');
this.name = name;
this.log = function (message) {
if (this.settings.debug === true) {
console.log("[Debug][" + name + "]: " + message);
}
};
};
socket_handler.js
/* Socket Handler */
module.exports = function () {
//REQUIRE NPM MODULES
var socket = require('socket.io')(4546);
//REQUIRE CUSTOM MODULES
var debug = new(require("custom_modules/debug"))("Sale Handler");
var login = require("custom_modules/login");
var register = require("custom_modules/register");
socket.on("connection", function (client) {
//HANDLE CONNECTION
debug.log("Client connected to socket!");
// HANDLE CUSTOM EVENTS
login.handle(client);
register.handle(client);
});
};
register.js
/* Register Handler */
//DEFINE MODULE EXPORTS
module.exports.handle = function (client) {
//REQUIRE CUSTOM MODULES
var debug = new(require("custom_modules/debug"))("Register Handler");
//HANDLE EVENTS
debug.log("Initialized");
client.on("register", function (data) {
debug.log("Handling registration...");
});
};
login.js
/* Login Handler */
//DEFINE MODULE EXPORTS
module.exports.handle = function (client) {
//REQUIRE CUSTOM MODULES
var debug = new(require("custom_modules/debug"))("Sale Handler");
//HANDLE EVENTS
debug.log("Initialized");
client.on("login", function (data) {
debug.log("Handling login...");
});
};
This is the code in a modulated fashion, without the utilization of prototypes for the Socket.io handlers. (I'm using a prototype for the debugging object only).
Prototypal Handlers
control_center.js
, settings.js
, debug.js
, socket_handler.js
=== above
handler.js
/* Handler */
module.exports = function (client, handlerName, handlerFunc) {
//DEFINE MODULE EXPORTS
this.handle = function (client) {
//REQUIRE CUSTOM MODULES
this.debug = new(require("custom_modules/debug"))(handlerName);
//HANDLE EVENTS
this.debug.log("Initialized");
handlerFunc();
};
};
login.js
/* Login Handler */
module.exports.handle = new(require("custom_modules/handler"))(client,
"Login Handler", function () {
client.on("login", function (data) {
debug.log("Handling login...");
});
});
register.js
/* Register Handler */
module.exports.handle = new(require("custom_modules/handler"))(client,
"Register Handler", function () {
client.on("register", function (data) {
debug.log("Handling registration...");
});
});
This is the code in a modulated fashion, with the utilization of prototypes for the Socket.io handlers and debugging. The handler prototype causes the code to have a slightly larger net length with only 3 events being handled, but realistically I'll be handling 20+ events. Ultimately, the prototype pays off in code size, and allows easy re-use of those debugging functions in later projects, but it does seem to damage readability of the code.