fnbind

1.0.1 • Public • Published

fnbind

A module to construct prototype objects as functions to provide more code readability.

Summary


Motivations

The Javascript beauty relies on its flexibility and how easy great things can come with a bunch of simple code. But as the time passes, this "bunch of simple code" can turn into a huge mess because it's now a file of 5000 lines of code and you have no idea of how much methods do you have, which of them are used or not between those functions, methods are now more complex and needs bigger names, and so on.

The way I found to solve this is to doing three things:

  1. Declare all my public methods in alphabetical order on the first place a developer looks when opening a class file;
  2. Use the Javascript flexibility to create objects as functions to have dots separating the words described on a method name;
  3. Declare all my private methods after all public methods implementations.

Well then.. Why?

Code doesn't need to be complicated and neither hard to understand. You can make it way more readable by change this:

ExampleClass.prototype.getListOfFatCatThatSleepsAllDay = function() {
  // ...
};

Into this:

ExampleClass.prototype['get.listOf.fatCats.thatSleepsAllDay'] = function() {
  // ...
};

Because when you need to create a very similar function, but with another purpose, you can instead of doing this:

ExampleClass.prototype.getListOfFatCatThatSleepsAllNight = function() {
  // ...
};

Into this:

ExampleClass.prototype['get.listOf.fatCats.thatSleepsAllNight'] = function() {
  // ...
};

The dots between the words make it way more cleaner for human eyes to read it, and having a big(verbose) function name declaration can make your code way more easier to understand for those who doesn't wrote that code.

Well then, having that in mind, let's see how to use it in an actual Javascript class.

How to use it

Suposing that you are developing a 'nodejs-like' code...

NOTE: There is a javascript class at 'lib/example-class.js' to serve as a full example of use of fnbind, and a test file at 'test/index.test.js' to demonstrate its usage.

  1. Install fnbind with npm and create a javascript file:

    npm install --save fnbind
    touch example-class.js
  2. Declare a common javascript class and exports it:

    const fnbind = require('fnbind');
    
    const ExampleClass = (function() {
      /**
       * @constructor
       */
      function ExampleClass() {
    
      }
    
      return ExampleClass;
    })();
    
    module.exports = ExampleClass;
  3. Inside the constructor, declare all the public methods you want to expose following a javascript plain object structure:

    const fnbind = require('fnbind');
    
    const ExampleClass = (function() {
      /**
       * @constructor
       */
      function ExampleClass() {
        this.get = {
          fatCat: {
            duquesa: fnbind(this['get.fatCat.duquesa'], this),
            solares: fnbind(this['get.fatCat.solares'], this)
          },
          listOf: {
            fatCats: {
              thatSleepsAllDay: fnbind(this['get.listOf.fatCats.thatSleepsAllDay'], this)
            }
          }
        };
      }
    
      return ExampleClass;
    })();
    
    module.exports = ExampleClass;

    NOTE: As a personal preference, I like to declare all public methods on alphabetical order.

  4. Implement your public methods using the Object.prototype:

    const fnbind = require('fnbind');
    
    const ExampleClass = (function() {
      /**
       * @constructor
       */
      function ExampleClass() {
        this.get = {
          fatCat: {
            duquesa: fnbind(this['get.fatCat.duquesa'], this),
            solares: fnbind(this['get.fatCat.solares'], this)
          },
          listOf: {
            fatCats: {
              thatSleepsAllDay: fnbind(this['get.listOf.fatCats.thatSleepsAllDay'], this)
            }
          }
        };
      }
    
      /**
       * @return {fatCat} - A single fatCat instance
       */
      ExampleClass.prototype['get.fatCat.duquesa'] = function() {
        const fatCat = {
          name: 'Duquesa',
          photo: ``,
          reasonWhyImFat: `Bitch please, I'm a duchess. I can be as fat as I want, and I will still be fabulous.`,
          sleepTime: 'day',
          weightInformation: {
            quantity: 17,
            unitOfMeasurement: 'lbs'
          }
        };
    
        return fatCat;
      };
    
      /**
       * @return {fatCat} - A single fatCat instance
       */
      ExampleClass.prototype['get.fatCat.solares'] = function() {
        const fatCat = {
          name: 'Solares',
          photo: '',
          reasonWhyImFat: `I have a very hard routine to maintain my orange and sexy belly:
          I wake up in the morning, eat like a starving beggar, drink tons of water and
          lay on the yard floor pretending that i'm dead.
          When the sun goes down, I go back to my house, eats until I see the food pot bottom, leave it empty so
          my homies stay fit, and go back to my pasteboard castle where I can watch my human slave play video games.`,
          sleepTime: 'day',
          weightInformation: {
            quantity: 22,
            unitOfMeasurement: 'lbs'
          }
        };
    
        return fatCat;
      };
    
      /**
       * @return {Array<fatCat>} - A list of fat cats
       */
      ExampleClass.prototype['get.listOf.fatCats.thatSleepsAllDay'] = function() {
        const fatCat1 = this.get.fatCat.duquesa();
        const fatCat2 = this.get.fatCat.solares();
    
        return [fatCat1, fatCat2];
      };
    
      return ExampleClass;
    })();
    
    module.exports = ExampleClass;
  5. Now you can create a new instance of your class and use it like a normal javascript class:

    touch index.js
    /**
     * index.js
     */
    const ExampleClass = require('./example-class');
    const exampleClass = new ExampleClass();
    
    let amountOfFatCats = exampleClass.get.listOf.fatCats.thatSleepsAllDay().length;
    console.log(amountOfFatCats); // 2

    Done. :)

Running the module tests

npm test

Package Sidebar

Install

npm i fnbind

Weekly Downloads

1

Version

1.0.1

License

ISC

Last publish

Collaborators

  • leonardosarmentocastro