Skip to content

Instantly share code, notes, and snippets.

@tmbtech
Forked from leonyu/coding-styles.md
Created May 16, 2016 20:02
Show Gist options
  • Select an option

  • Save tmbtech/694bdfdc0eede7a6bbcc5c0a9d65df77 to your computer and use it in GitHub Desktop.

Select an option

Save tmbtech/694bdfdc0eede7a6bbcc5c0a9d65df77 to your computer and use it in GitHub Desktop.

Revisions

  1. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -36,11 +36,11 @@ printHelloWorld();
    This is a technique often used to emulate OOP when OOP is not available such as in languages C and Bash.
    It is also often used when OOP is not practical, for example when recreating the object is an overhead.
    ```js
    function hellWorld(ctx) {
    function helloWorld(ctx) {
    ctx.log(HELLO_WORLD);
    }
    // usage
    hellWorld(conole);
    helloWorld(console);
    ```

    ### Static-typed object-oriented ###
  2. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@ function hellWorld(ctx) {
    ctx.log(HELLO_WORLD);
    }
    // usage
    hellWorld(ctx);
    hellWorld(conole);
    ```

    ### Static-typed object-oriented ###
  3. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -103,8 +103,8 @@ HelloWorld.prototype.print = function () {
    new HelloWorld().log();
    ```

    #### Injection-based ####
    ##### constructor injection #####
    ### Injection-based ###
    #### constructor injection ####
    ```js
    class HelloWorld {
    constructor(console) {
    @@ -118,7 +118,7 @@ class HelloWorld {
    new HelloWorld(console).print();
    ```

    ##### Parameter Injection #####
    #### Parameter Injection ####
    ```js
    class HelloWorld {
    print(console) {
    @@ -129,7 +129,7 @@ class HelloWorld {
    new HelloWorld().print(console);
    ```

    ##### Autowired Injection #####
    #### Autowired Injection ####
    ```js
    class HelloWorld {
    $console;
    @@ -140,8 +140,8 @@ class HelloWorld {
    // usage
    Singleton.get(HelloWorld).print(console);
    ```
    #### Factory ####
    ##### Constructor Injection #####
    ### Factory ###
    #### Constructor Injection ####
    ```js
    function createHelloWorld(f) {
    return {
    @@ -154,7 +154,7 @@ function createHelloWorld(f) {
    createHelloWorld(console).print();
    ```

    ##### Parameter Injection #####
    #### Parameter Injection ####
    ```js
    function createHelloWorld() {
    return {
    @@ -167,7 +167,7 @@ function createHelloWorld() {
    createHelloWorld().print(console);
    ```

    ##### IIFE / old-school module pattern #####
    #### IIFE / old-school module pattern ####
    ```js
    const HelloWorld = (function createHelloWorld() {
    return {
  4. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ These examples are presented in an attempt to show how each coding styles attemp

    Since every coding style can abstract away data into a parameter or variable, there is no point for us to show that. All implementations assume `HELLO_WORLD` is a constant that is always inlined. This way it reduces the variations we need to present. (To make an anology, if we were to implement `incrementByOne`, would we need to inline the number `1` or pass it in as parameter?)

    All implementations also assume `console` is static. In case of OOP, `Console` is assumed to be extendable. In case of functional programming, `console.log` is asumed to be a function that can be passed around without further modification.
    CAVEAT/LIMITATION: All implementations also assume `console` is static. In case of OOP inheritance, `Console` is assumed to be extendable. In case of functional programming, `console.log` is asumed to be a function that can be passed around without further modification.

    ## Declarative ##
    ```jsx
  5. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ These examples are presented in an attempt to show how each coding styles attemp
    * Invocation of `console.log`
    * Declaration of `HELLO_WORLD`

    Since every coding style can abstract away data into a parameter or variable, there is no point for us to show that. All implementations assume `HELLO_WORLD` is a constant that is always inlined. This way it reduces the variations we need to present. (To make an anology, if you were to implement `incrementByOne`, would `1` be inlined or passed in as variable?)
    Since every coding style can abstract away data into a parameter or variable, there is no point for us to show that. All implementations assume `HELLO_WORLD` is a constant that is always inlined. This way it reduces the variations we need to present. (To make an anology, if we were to implement `incrementByOne`, would we need to inline the number `1` or pass it in as parameter?)

    All implementations also assume `console` is static. In case of OOP, `Console` is assumed to be extendable. In case of functional programming, `console.log` is asumed to be a function that can be passed around without further modification.

  6. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ These examples are presented in an attempt to show how each coding styles attemp
    * Invocation of `console.log`
    * Declaration of `HELLO_WORLD`

    Since every coding style can abstract away data into a parameter or variable, there is no point for us to show that. All implementations assume `HELLO_WORLD` is a constant that is always inlined. This way it reduces the variations we need to present.
    Since every coding style can abstract away data into a parameter or variable, there is no point for us to show that. All implementations assume `HELLO_WORLD` is a constant that is always inlined. This way it reduces the variations we need to present. (To make an anology, if you were to implement `incrementByOne`, would `1` be inlined or passed in as variable?)

    All implementations also assume `console` is static. In case of OOP, `Console` is assumed to be extendable. In case of functional programming, `console.log` is asumed to be a function that can be passed around without further modification.

  7. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ These examples are presented in an attempt to show how each coding styles attemp
    * Invocation of `console.log`
    * Declaration of `HELLO_WORLD`

    Since every coding style can abstract away data into a parameter or variable, there is no point for us to show that. All implementations assume `HELLO_WORLD` is a constant that is always inline. This way it reduces the variations we need to present.
    Since every coding style can abstract away data into a parameter or variable, there is no point for us to show that. All implementations assume `HELLO_WORLD` is a constant that is always inlined. This way it reduces the variations we need to present.

    All implementations also assume `console` is static. In case of OOP, `Console` is assumed to be extendable. In case of functional programming, `console.log` is asumed to be a function that can be passed around without further modification.

  8. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ These examples are presented in an attempt to show how each coding styles attemp

    * Invocation of `console.log`
    * Declaration of `HELLO_WORLD`
    *

    Since every coding style can abstract away data into a parameter or variable, there is no point for us to show that. All implementations assume `HELLO_WORLD` is a constant that is always inline. This way it reduces the variations we need to present.

    All implementations also assume `console` is static. In case of OOP, `Console` is assumed to be extendable. In case of functional programming, `console.log` is asumed to be a function that can be passed around without further modification.
  9. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    These examples are presented in an attempt to show how each coding styles attempts to or does not attempt to isolate side-effects. There are only 2 semantic elements in a barebone "Hello World" implementation:

    * Invocation of `console.log`
    * Declaration of `HELLO_WORLD`
    *
    Since every coding style can abstract away data into a parameter or variable, there is no point for us to show that. All implementations assume `HELLO_WORLD` is a constant that is always inline. This way it reduces the variations we need to present.

    All implementations also assume `console` is static. In case of OOP, `Console` is assumed to be extendable. In case of functional programming, `console.log` is asumed to be a function that can be passed around without further modification.
  10. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    * All implementations assume `HELLO_WORLD` is a constant and does not need to be abstracted and does not need to be passed in as an argument.
    * All implementations assume `console.log` is a static function. In case of functional programming, it can be passed around (not the case in Chrome).
    These examples are presented in an attempt to show how each coding styles attempts to or does not attempt to isolate side-effects. There are only 2 semantic elements in a barebone "Hello World" implementation:
    * Invocation of `console.log`
    * Declaration of `HELLO_WORLD`
    Since every coding style can abstract away data into a parameter or variable, there is no point for us to show that. All implementations assume `HELLO_WORLD` is a constant that is always inline. This way it reduces the variations we need to present.

    All implementations also assume `console` is static. In case of OOP, `Console` is assumed to be extendable. In case of functional programming, `console.log` is asumed to be a function that can be passed around without further modification.

    ## Declarative ##
    ```jsx
  11. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    * All implementations assume `console.log` is a static function with side-effect.
    * All implementations assume `HELLO_WORLD` is a constant and does not need to be abstracted and does not need to be passed in as an argument.
    * All implementations assume `console.log` is a static function. In case of functional programming, it can be passed around (not the case in Chrome).

    ## Declarative ##
    ```jsx
    @@ -226,7 +226,6 @@ console.printHelloWorld();
    ### Eager Eval ###
    #### Side-effect on usage ####
    ```js
    // This is a contrived case as IO is all about side-effect
    function helloWorld() {
    return HELLO_WORLD;
    }
  12. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,7 @@ React.render(HelloWorld, doment.body)
    console.log(HELLO_WORLD);
    ```
    #### Subroutine ####
    Subroutine does not return value.
    ```js
    function printHelloWorld() {
    console.log(HELLO_WORLD);
  13. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,9 @@ function printHelloWorld() {
    printHelloWorld();
    ```

    #### Context-passing / OOP in non-OOP languages ####
    #### Context-passing ####
    This is a technique often used to emulate OOP when OOP is not available such as in languages C and Bash.
    It is also often used when OOP is not practical, for example when recreating the object is an overhead.
    ```js
    function hellWorld(ctx) {
    ctx.log(HELLO_WORLD);
  14. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -12,9 +12,9 @@ React.render(HelloWorld, doment.body)
    ### Procedural ###
    #### Direct invocation ####
    ```js
    console.log(HELLO_WORLD);
    // no implementation
    // usage
    // none - procedural code
    console.log(HELLO_WORLD);
    ```
    #### Subroutine ####
    ```js
  15. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ function hellWorld(ctx) {
    ctx.log(HELLO_WORLD);
    }
    // usage
    printHelloWorld();
    hellWorld(ctx);
    ```

    ### Static-typed object-oriented ###
  16. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ function printHelloWorld() {
    printHelloWorld();
    ```

    #### Context-passing / Idomatic OOP ####
    #### Context-passing / OOP in non-OOP languages ####
    ```js
    function hellWorld(ctx) {
    ctx.log(HELLO_WORLD);
  17. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 49 additions and 39 deletions.
    88 changes: 49 additions & 39 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,15 @@ function printHelloWorld() {
    printHelloWorld();
    ```

    #### Context-passing / Idomatic OOP ####
    ```js
    function hellWorld(ctx) {
    ctx.log(HELLO_WORLD);
    }
    // usage
    printHelloWorld();
    ```

    ### Static-typed object-oriented ###
    #### Static ####
    ##### Static method #####
    @@ -49,6 +58,42 @@ class HelloWorld {
    new HelloWorld().print()
    ```

    ### Inheritance ###
    #### Classical Inheritance ####
    ```js
    // following code would not work as Console is not publically initializable
    class HelloWorld extends Console {
    print() {
    this.log(HELLO_WORLD);
    }
    }
    // usage
    new HelloWorld().log();
    ```

    #### Prototypical inheritance ####
    ```js
    let myConsole = Object.create(console);
    myConsole.print = function () {
    this.log(HELLO_WORLD);
    };
    // usage
    myConsole.print();
    ```

    #### Old-school class pattern ####
    ```js
    // following code would not work as Console is not publically initializable
    function HelloWorld() {
    }
    HelloWorld.prototype = new Console();
    HelloWorld.prototype.print = function () {
    this.log(HELLO_WORLD);
    };
    // usage
    new HelloWorld().log();
    ```

    #### Injection-based ####
    ##### constructor injection #####
    ```js
    @@ -86,8 +131,8 @@ class HelloWorld {
    // usage
    Singleton.get(HelloWorld).print(console);
    ```
    ### Factory ###
    #### Constructor Injection ####
    #### Factory ####
    ##### Constructor Injection #####
    ```js
    function createHelloWorld(f) {
    return {
    @@ -100,7 +145,7 @@ function createHelloWorld(f) {
    createHelloWorld(console).print();
    ```

    #### Parameter Injection ####
    ##### Parameter Injection #####
    ```js
    function createHelloWorld() {
    return {
    @@ -113,7 +158,7 @@ function createHelloWorld() {
    createHelloWorld().print(console);
    ```

    #### IIFE / old-school module pattern ####
    ##### IIFE / old-school module pattern #####
    ```js
    const HelloWorld = (function createHelloWorld() {
    return {
    @@ -126,41 +171,6 @@ const HelloWorld = (function createHelloWorld() {
    HelloWorld.print(console);
    ```

    ### Inheritance ###
    #### Classical Inheritance ####
    ```js
    // following code would not work as Console is not publically initializable
    class HelloWorld extends Console {
    print() {
    this.log(HELLO_WORLD);
    }
    }
    // usage
    new HelloWorld().log();
    ```

    #### Prototypical inheritance ####
    ```js
    let myConsole = Object.create(console);
    myConsole.print = function () {
    this.log(HELLO_WORLD);
    };
    // usage
    myConsole.print();
    ```

    #### Old-school class pattern ####
    ```js
    // following code would not work as Console is not publically initializable
    function HelloWorld() {
    }
    HelloWorld.prototype = new Console();
    HelloWorld.prototype.print = function () {
    this.log(HELLO_WORLD);
    };
    // usage
    new HelloWorld().log();
    ```
    ### Dynamic-typed object-oriented ###
    #### Mixin ####
    ##### Static invocation #####
  18. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 40 additions and 40 deletions.
    80 changes: 40 additions & 40 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -86,6 +86,46 @@ class HelloWorld {
    // usage
    Singleton.get(HelloWorld).print(console);
    ```
    ### Factory ###
    #### Constructor Injection ####
    ```js
    function createHelloWorld(f) {
    return {
    print() {
    f.log(HELLO_WORLD);
    }
    }
    }
    // usage
    createHelloWorld(console).print();
    ```

    #### Parameter Injection ####
    ```js
    function createHelloWorld() {
    return {
    print(console) {
    console.log(HELLO_WORLD);
    }
    };
    }
    // usage
    createHelloWorld().print(console);
    ```

    #### IIFE / old-school module pattern ####
    ```js
    const HelloWorld = (function createHelloWorld() {
    return {
    print(console) {
    console.log(HELLO_WORLD);
    }
    };
    })();
    // usage
    HelloWorld.print(console);
    ```

    ### Inheritance ###
    #### Classical Inheritance ####
    ```js
    @@ -169,46 +209,6 @@ console.printHelloWorld = function() {
    console.printHelloWorld();
    ```

    ### Factory / Builder ###
    #### Constructor Injection ####
    ```js
    function createHelloWorld(f) {
    return {
    print() {
    f.log(HELLO_WORLD);
    }
    }
    }
    // usage
    createHelloWorld(console).print();
    ```

    #### Parameter Injection ####
    ```js
    function createHelloWorld() {
    return {
    print(console) {
    console.log(HELLO_WORLD);
    }
    };
    }
    // usage
    createHelloWorld().print(console);
    ```

    #### IIFE / old-school module pattern ####
    ```js
    const HelloWorld = (function createHelloWorld() {
    return {
    print(console) {
    console.log(HELLO_WORLD);
    }
    };
    })();
    // usage
    HelloWorld.print(console);
    ```

    ## Functional programming ##
    ### Eager Eval ###
    #### Side-effect on usage ####
  19. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -160,7 +160,7 @@ Object.assign(console, HelloWorldMixin);
    console.printHelloWorld();
    ```

    #### Monkey-pactching with method ####
    ##### Monkey-pactching with method #####
    ```js
    console.printHelloWorld = function() {
    this.log(HELLO_WORLD);
  20. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -147,7 +147,8 @@ const obj = Object.assign({}, HelloWorldMixin);
    obj.printHelloWorld(console);
    ```

    ##### Mixins on native methods #####
    #### Monkey-pactching ####
    ##### Monkey-pactching with mixins #####
    ```js
    const HelloWorldMixin = {
    printHelloWorld() {
    @@ -159,7 +160,7 @@ Object.assign(console, HelloWorldMixin);
    console.printHelloWorld();
    ```

    ### Monkey-pactching method ###
    #### Monkey-pactching with method ####
    ```js
    console.printHelloWorld = function() {
    this.log(HELLO_WORLD);
  21. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -214,7 +214,7 @@ HelloWorld.print(console);
    ```js
    // This is a contrived case as IO is all about side-effect
    function helloWorld() {
    return HELLO_WORLD';
    return HELLO_WORLD;
    }
    // usage
    console.log(helloWorld());
  22. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 23 additions and 23 deletions.
    46 changes: 23 additions & 23 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    * All implementations assume `console.log` is a static function with side-effect.
    * All implementations assume `Hello world!!!` is a constant and does not need to be abstracted, does not need to be separated into a variable, and thus should always be inlined.
    * All implementations assume `HELLO_WORLD` is a constant and does not need to be abstracted and does not need to be passed in as an argument.

    ## Declarative ##
    ```jsx
    const HelloWorld = (<div>Hello world!!!</div>);
    const HelloWorld = (<div>{HELLO_WORLD}</div>);
    // usage
    React.render(HelloWorld, doment.body)
    ```
    @@ -12,14 +12,14 @@ React.render(HelloWorld, doment.body)
    ### Procedural ###
    #### Direct invocation ####
    ```js
    console.log('Hello world!!!');
    console.log(HELLO_WORLD);
    // usage
    // none - procedural code
    ```
    #### Subroutine ####
    ```js
    function printHelloWorld() {
    console.log('Hello world!!!');
    console.log(HELLO_WORLD);
    }
    // usage
    printHelloWorld();
    @@ -31,7 +31,7 @@ printHelloWorld();
    ```js
    class HelloWorld {
    static print() {
    console.log('Hello world!!!');
    console.log(HELLO_WORLD);
    }
    }
    // usage
    @@ -42,7 +42,7 @@ HelloWorld.print();
    ```js
    class HelloWorld {
    print() {
    console.log('Hello world!!!');
    console.log(HELLO_WORLD);
    }
    }
    // usage
    @@ -57,7 +57,7 @@ class HelloWorld {
    this.console = console;
    }
    print() {
    this.console.log('Hello world!!!');
    this.console.log(HELLO_WORLD);
    }
    }
    // usage
    @@ -68,7 +68,7 @@ new HelloWorld(console).print();
    ```js
    class HelloWorld {
    print(console) {
    console.log('Hello world!!!');
    console.log(HELLO_WORLD);
    }
    }
    // usage
    @@ -80,7 +80,7 @@ new HelloWorld().print(console);
    class HelloWorld {
    $console;
    print(console) {
    this.$console.log('Hello world!!!');
    this.$console.log(HELLO_WORLD);
    }
    }
    // usage
    @@ -92,7 +92,7 @@ Singleton.get(HelloWorld).print(console);
    // following code would not work as Console is not publically initializable
    class HelloWorld extends Console {
    print() {
    this.log('Hello world!!!');
    this.log(HELLO_WORLD);
    }
    }
    // usage
    @@ -103,7 +103,7 @@ new HelloWorld().log();
    ```js
    let myConsole = Object.create(console);
    myConsole.print = function () {
    this.log('Hello world!!!');
    this.log(HELLO_WORLD);
    };
    // usage
    myConsole.print();
    @@ -116,7 +116,7 @@ function HelloWorld() {
    }
    HelloWorld.prototype = new Console();
    HelloWorld.prototype.print = function () {
    this.log('Hello world!!!');
    this.log(HELLO_WORLD);
    };
    // usage
    new HelloWorld().log();
    @@ -127,7 +127,7 @@ new HelloWorld().log();
    ```js
    const HelloWorldMixin = {
    printHelloWorld() {
    console.log('Hello World!!!');
    console.log(HELLO_WORLD);
    }
    };
    // usage
    @@ -139,7 +139,7 @@ obj.printHelloWorld();
    ```js
    const HelloWorldMixin = {
    printHelloWorld(console) {
    console.log('Hello World!!!');
    console.log(HELLO_WORLD);
    }
    };
    // usage
    @@ -151,7 +151,7 @@ obj.printHelloWorld(console);
    ```js
    const HelloWorldMixin = {
    printHelloWorld() {
    console.log('Hello World!!!');
    console.log(HELLO_WORLD);
    }
    };
    // usage
    @@ -162,7 +162,7 @@ console.printHelloWorld();
    ### Monkey-pactching method ###
    ```js
    console.printHelloWorld = function() {
    this.log('Hello world!!!');
    this.log(HELLO_WORLD);
    }
    // usage
    console.printHelloWorld();
    @@ -174,7 +174,7 @@ console.printHelloWorld();
    function createHelloWorld(f) {
    return {
    print() {
    f.log('Hello world!!!');
    f.log(HELLO_WORLD);
    }
    }
    }
    @@ -187,7 +187,7 @@ createHelloWorld(console).print();
    function createHelloWorld() {
    return {
    print(console) {
    console.log('Hello world!!!');
    console.log(HELLO_WORLD);
    }
    };
    }
    @@ -200,7 +200,7 @@ createHelloWorld().print(console);
    const HelloWorld = (function createHelloWorld() {
    return {
    print(console) {
    console.log('Hello world!!!');
    console.log(HELLO_WORLD);
    }
    };
    })();
    @@ -214,7 +214,7 @@ HelloWorld.print(console);
    ```js
    // This is a contrived case as IO is all about side-effect
    function helloWorld() {
    return 'Hello World!!!';
    return HELLO_WORLD';
    }
    // usage
    console.log(helloWorld());
    @@ -223,7 +223,7 @@ console.log(helloWorld());
    #### Side-effect on binding ####
    ```js
    function helloWorld(console) {
    return console.log('Hello world!!!');
    return console.log(HELLO_WORLD);
    }
    // usage
    helloWorld(console);
    @@ -233,7 +233,7 @@ helloWorld(console);
    #### early binding ####
    ```js
    function helloWorld(f) {
    return () => f.log('Hello world!!!');
    return () => f.log(HELLO_WORLD);
    }
    // usage
    helloWorld(console)();
    @@ -242,7 +242,7 @@ helloWorld(console)();
    ##### Late Binding #####
    ```js
    function helloWorld() {
    return (f) => f.log('Hello world!!!');
    return (f) => f.log(HELLO_WORLD);
    }
    // usage
    helloWorld()(console);
  23. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    All implementations assume `console.log` is a static function with side-effect. All implementations assume `Hello world!!!` is a constant and does not need to be abstracted, and thus can be inlined.
    * All implementations assume `console.log` is a static function with side-effect.
    * All implementations assume `Hello world!!!` is a constant and does not need to be abstracted, does not need to be separated into a variable, and thus should always be inlined.

    ## Declarative ##
    ```jsx
  24. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 21 additions and 21 deletions.
    42 changes: 21 additions & 21 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -10,13 +10,13 @@ React.render(HelloWorld, doment.body)
    ## Imperative ##
    ### Procedural ###
    #### Direct invocation ####
    ```
    ```js
    console.log('Hello world!!!');
    // usage
    // none - procedural code
    ```
    #### Subroutine ####
    ```
    ```js
    function printHelloWorld() {
    console.log('Hello world!!!');
    }
    @@ -27,7 +27,7 @@ printHelloWorld();
    ### Static-typed object-oriented ###
    #### Static ####
    ##### Static method #####
    ```
    ```js
    class HelloWorld {
    static print() {
    console.log('Hello world!!!');
    @@ -38,7 +38,7 @@ HelloWorld.print();
    ```

    ##### Static invocation from method #####
    ```
    ```js
    class HelloWorld {
    print() {
    console.log('Hello world!!!');
    @@ -50,7 +50,7 @@ new HelloWorld().print()

    #### Injection-based ####
    ##### constructor injection #####
    ```
    ```js
    class HelloWorld {
    constructor(console) {
    this.console = console;
    @@ -64,7 +64,7 @@ new HelloWorld(console).print();
    ```

    ##### Parameter Injection #####
    ```
    ```js
    class HelloWorld {
    print(console) {
    console.log('Hello world!!!');
    @@ -75,7 +75,7 @@ new HelloWorld().print(console);
    ```

    ##### Autowired Injection #####
    ```
    ```js
    class HelloWorld {
    $console;
    print(console) {
    @@ -87,7 +87,7 @@ Singleton.get(HelloWorld).print(console);
    ```
    ### Inheritance ###
    #### Classical Inheritance ####
    ```
    ```js
    // following code would not work as Console is not publically initializable
    class HelloWorld extends Console {
    print() {
    @@ -99,7 +99,7 @@ new HelloWorld().log();
    ```

    #### Prototypical inheritance ####
    ```
    ```js
    let myConsole = Object.create(console);
    myConsole.print = function () {
    this.log('Hello world!!!');
    @@ -109,7 +109,7 @@ myConsole.print();
    ```

    #### Old-school class pattern ####
    ```
    ```js
    // following code would not work as Console is not publically initializable
    function HelloWorld() {
    }
    @@ -123,7 +123,7 @@ new HelloWorld().log();
    ### Dynamic-typed object-oriented ###
    #### Mixin ####
    ##### Static invocation #####
    ```
    ```js
    const HelloWorldMixin = {
    printHelloWorld() {
    console.log('Hello World!!!');
    @@ -135,7 +135,7 @@ obj.printHelloWorld();
    ```

    ##### Parameter injection #####
    ```
    ```js
    const HelloWorldMixin = {
    printHelloWorld(console) {
    console.log('Hello World!!!');
    @@ -147,7 +147,7 @@ obj.printHelloWorld(console);
    ```

    ##### Mixins on native methods #####
    ```
    ```js
    const HelloWorldMixin = {
    printHelloWorld() {
    console.log('Hello World!!!');
    @@ -159,7 +159,7 @@ console.printHelloWorld();
    ```

    ### Monkey-pactching method ###
    ```
    ```js
    console.printHelloWorld = function() {
    this.log('Hello world!!!');
    }
    @@ -169,7 +169,7 @@ console.printHelloWorld();

    ### Factory / Builder ###
    #### Constructor Injection ####
    ```
    ```js
    function createHelloWorld(f) {
    return {
    print() {
    @@ -182,7 +182,7 @@ createHelloWorld(console).print();
    ```

    #### Parameter Injection ####
    ```
    ```js
    function createHelloWorld() {
    return {
    print(console) {
    @@ -195,7 +195,7 @@ createHelloWorld().print(console);
    ```

    #### IIFE / old-school module pattern ####
    ```
    ```js
    const HelloWorld = (function createHelloWorld() {
    return {
    print(console) {
    @@ -210,7 +210,7 @@ HelloWorld.print(console);
    ## Functional programming ##
    ### Eager Eval ###
    #### Side-effect on usage ####
    ```
    ```js
    // This is a contrived case as IO is all about side-effect
    function helloWorld() {
    return 'Hello World!!!';
    @@ -220,7 +220,7 @@ console.log(helloWorld());
    ```

    #### Side-effect on binding ####
    ```
    ```js
    function helloWorld(console) {
    return console.log('Hello world!!!');
    }
    @@ -230,7 +230,7 @@ helloWorld(console);

    ### Lazy Eval ###
    #### early binding ####
    ```
    ```js
    function helloWorld(f) {
    return () => f.log('Hello world!!!');
    }
    @@ -239,7 +239,7 @@ helloWorld(console)();
    ```

    ##### Late Binding #####
    ```
    ```js
    function helloWorld() {
    return (f) => f.log('Hello world!!!');
    }
  25. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -209,25 +209,25 @@ HelloWorld.print(console);

    ## Functional programming ##
    ### Eager Eval ###
    #### Side-effect on binding ####
    ```
    // functional - eager eval - no side-effect
    function helloWorld(console) {
    return console.log('Hello world!!!');
    }
    // usage
    helloWorld(console);
    ```
    #### Side-effect on usage ####
    ```
    no side-effect
    // This is a contrived case as IO is all about side-effect
    function helloWorld() {
    return 'Hello World!!!';
    }
    // usage
    console.log(helloWorld());
    ```

    #### Side-effect on binding ####
    ```
    function helloWorld(console) {
    return console.log('Hello world!!!');
    }
    // usage
    helloWorld(console);
    ```

    ### Lazy Eval ###
    #### early binding ####
    ```
  26. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -146,7 +146,7 @@ const obj = Object.assign({}, HelloWorldMixin);
    obj.printHelloWorld(console);
    ```

    ##### Monkey-patching mixins #####
    ##### Mixins on native methods #####
    ```
    const HelloWorldMixin = {
    printHelloWorld() {
    @@ -158,7 +158,7 @@ Object.assign(console, HelloWorldMixin);
    console.printHelloWorld();
    ```

    #### Monkey pactching method ####
    ### Monkey-pactching method ###
    ```
    console.printHelloWorld = function() {
    this.log('Hello world!!!');
  27. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -146,7 +146,7 @@ const obj = Object.assign({}, HelloWorldMixin);
    obj.printHelloWorld(console);
    ```

    ##### Target Mixin #####
    ##### Monkey-patching mixins #####
    ```
    const HelloWorldMixin = {
    printHelloWorld() {
    @@ -158,7 +158,7 @@ Object.assign(console, HelloWorldMixin);
    console.printHelloWorld();
    ```

    #### Monkey pactching ####
    #### Monkey pactching method ####
    ```
    console.printHelloWorld = function() {
    this.log('Hello world!!!');
  28. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -208,7 +208,7 @@ HelloWorld.print(console);
    ```

    ## Functional programming ##
    ### eager eval ###
    ### Eager Eval ###
    #### Side-effect on binding ####
    ```
    // functional - eager eval - no side-effect
  29. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -146,15 +146,6 @@ const obj = Object.assign({}, HelloWorldMixin);
    obj.printHelloWorld(console);
    ```

    #### Monkey pactching ####
    ```
    console.printHelloWorld = function() {
    this.log('Hello world!!!');
    }
    // usage
    console.printHelloWorld();
    ```

    ##### Target Mixin #####
    ```
    const HelloWorldMixin = {
    @@ -167,6 +158,15 @@ Object.assign(console, HelloWorldMixin);
    console.printHelloWorld();
    ```

    #### Monkey pactching ####
    ```
    console.printHelloWorld = function() {
    this.log('Hello world!!!');
    }
    // usage
    console.printHelloWorld();
    ```

    ### Factory / Builder ###
    #### Constructor Injection ####
    ```
  30. @leonyu leonyu revised this gist May 16, 2016. 1 changed file with 21 additions and 12 deletions.
    33 changes: 21 additions & 12 deletions coding-styles.md
    Original file line number Diff line number Diff line change
    @@ -9,10 +9,19 @@ React.render(HelloWorld, doment.body)

    ## Imperative ##
    ### Procedural ###
    #### Direct invocation ####
    ```
    console.log('Hello world!!!');
    // usage
    // none
    // none - procedural code
    ```
    #### Subroutine ####
    ```
    function printHelloWorld() {
    console.log('Hello world!!!');
    }
    // usage
    printHelloWorld();
    ```

    ### Static-typed object-oriented ###
    @@ -28,7 +37,7 @@ class HelloWorld {
    HelloWorld.print();
    ```

    ##### Static invocation #####
    ##### Static invocation from method #####
    ```
    class HelloWorld {
    print() {
    @@ -65,7 +74,7 @@ class HelloWorld {
    new HelloWorld().print(console);
    ```

    ##### Autowired injection #####
    ##### Autowired Injection #####
    ```
    class HelloWorld {
    $console;
    @@ -77,7 +86,7 @@ class HelloWorld {
    Singleton.get(HelloWorld).print(console);
    ```
    ### Inheritance ###
    #### Classical inheritance ####
    #### Classical Inheritance ####
    ```
    // following code would not work as Console is not publically initializable
    class HelloWorld extends Console {
    @@ -125,7 +134,7 @@ const obj = Object.assign({}, HelloWorldMixin);
    obj.printHelloWorld();
    ```

    ##### Argument injection #####
    ##### Parameter injection #####
    ```
    const HelloWorldMixin = {
    printHelloWorld(console) {
    @@ -146,7 +155,7 @@ console.printHelloWorld = function() {
    console.printHelloWorld();
    ```

    ##### Target mixin #####
    ##### Target Mixin #####
    ```
    const HelloWorldMixin = {
    printHelloWorld() {
    @@ -159,7 +168,7 @@ console.printHelloWorld();
    ```

    ### Factory / Builder ###
    #### constructor injection ####
    #### Constructor Injection ####
    ```
    function createHelloWorld(f) {
    return {
    @@ -172,7 +181,7 @@ function createHelloWorld(f) {
    createHelloWorld(console).print();
    ```

    #### parameter injection ####
    #### Parameter Injection ####
    ```
    function createHelloWorld() {
    return {
    @@ -200,7 +209,7 @@ HelloWorld.print(console);

    ## Functional programming ##
    ### eager eval ###
    #### Side-effect in binding ####
    #### Side-effect on binding ####
    ```
    // functional - eager eval - no side-effect
    function helloWorld(console) {
    @@ -209,7 +218,7 @@ function helloWorld(console) {
    // usage
    helloWorld(console);
    ```
    #### Side-effect in usage ####
    #### Side-effect on usage ####
    ```
    no side-effect
    function helloWorld() {
    @@ -229,11 +238,11 @@ function helloWorld(f) {
    helloWorld(console)();
    ```

    ##### late binding #####
    ##### Late Binding #####
    ```
    function helloWorld() {
    return (f) => f.log('Hello world!!!');
    }
    // usage
    hellWorld()(console)();
    helloWorld()(console);
    ```