Skip to content

Instantly share code, notes, and snippets.

@mrhampson
Last active August 9, 2018 18:33
Show Gist options
  • Save mrhampson/46b2ac01287f93a1531efcb2e12dce0c to your computer and use it in GitHub Desktop.
Save mrhampson/46b2ac01287f93a1531efcb2e12dce0c to your computer and use it in GitHub Desktop.

Revisions

  1. mrhampson revised this gist Aug 9, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion defaultParamsOveruseExample.ts
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /**
    I created this gist to point out something I consider a bad practice that I've started seeing in our typescript code:
    I created this gist to point out something I consider a bad practice that I've seen in typescript code:
    that is overuse of default parameters. The problem with overusing default parameters is that the default behavior
    of the function without args is completely implicit and requires the developer to reference the function definition and
    memorize whenever they are looking through the code.
  2. mrhampson revised this gist Aug 9, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions defaultParamsOveruseExample.ts
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,7 @@ private retrieveData(activeOnly = true, inactiveOnly = false) {

    // Later on
    // What is this doing? No idea without having memorize the function definition further up.
    // This boolean condition is also copy/pasted multiple times
    this.retrieveData(this.$scope.activeTabsAlerts.id === LaAlerting.ACTIVE_TAB.id);

    // Later on
  3. mrhampson renamed this gist Aug 9, 2018. 1 changed file with 0 additions and 0 deletions.
  4. mrhampson renamed this gist Aug 9, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. mrhampson revised this gist Aug 9, 2018. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion defaultParmsExample.ts
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    /**
    I created this gist to point out something I consider a bad practice that I've started seeing in our typescript code:
    that is overuse of default parameters. The problem with using default parameters too often is that the default behavior
    that is overuse of default parameters. The problem with overusing default parameters is that the default behavior
    of the function without args is completely implicit and requires the developer to reference the function definition and
    memorize whenever they are looking through the code.
    @@ -9,6 +9,9 @@ was hidden behind default parameters. I refactored it by using functions that ar
    then call retrieveData with the appropriate parameters. By providing a function with a descriptive name rather than
    letting parameter default it is much clearer what the code is doing when you are far away from the retrieveData
    function definition.
    To be clear, I'm not suggesting default parameters should never be used. In certain situations they can make things
    clearer and save repeated code.
    */

    /***************************************************************
  6. mrhampson revised this gist Aug 9, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions defaultParmsExample.ts
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ that is overuse of default parameters. The problem with using default parameters
    of the function without args is completely implicit and requires the developer to reference the function definition and
    memorize whenever they are looking through the code.
    In the examples below I took some of our existing code that had such a function retrieveData where it's behavior
    In the examples below I took some of our existing code that had such a function retrieveData where its behavior
    was hidden behind default parameters. I refactored it by using functions that are named something descriptive that
    then call retrieveData with the appropriate parameters. By providing a function with a descriptive name rather than
    letting parameter default it is much clearer what the code is doing when you are far away from the retrieveData
    @@ -24,13 +24,13 @@ private retrieveData(activeOnly = true, inactiveOnly = false) {
    this.retrieveData(this.$scope.activeTabsAlerts.id === LaAlerting.ACTIVE_TAB.id);

    // Later on
    // Same problem no idea what this means. Entirely implicit and relying on the developer memorizing the definition
    // Same problem: no idea what this means. Entirely implicit and relys on the developer memorizing the definition
    this.retrieveData(true)

    /***************************************************************
    * After refactor
    ***************************************************************/
    // The functions below replace the various calls with the default parameters
    // The functions below replace the original function with default parameters

    // By putting the logic for which tab gets what data in this function rather than copy/pasteing a conditional statement
    // that evaluates to the activeOnly paramter we avoid copy/paste code of the conditional and make the logic clearer
  7. mrhampson created this gist Aug 9, 2018.
    65 changes: 65 additions & 0 deletions defaultParmsExample.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    /**
    I created this gist to point out something I consider a bad practice that I've started seeing in our typescript code:
    that is overuse of default parameters. The problem with using default parameters too often is that the default behavior
    of the function without args is completely implicit and requires the developer to reference the function definition and
    memorize whenever they are looking through the code.
    In the examples below I took some of our existing code that had such a function retrieveData where it's behavior
    was hidden behind default parameters. I refactored it by using functions that are named something descriptive that
    then call retrieveData with the appropriate parameters. By providing a function with a descriptive name rather than
    letting parameter default it is much clearer what the code is doing when you are far away from the retrieveData
    function definition.
    */

    /***************************************************************
    * Before refactor
    ***************************************************************/

    private retrieveData(activeOnly = true, inactiveOnly = false) {
    // gets data
    }

    // Later on
    // What is this doing? No idea without having memorize the function definition further up.
    this.retrieveData(this.$scope.activeTabsAlerts.id === LaAlerting.ACTIVE_TAB.id);

    // Later on
    // Same problem no idea what this means. Entirely implicit and relying on the developer memorizing the definition
    this.retrieveData(true)

    /***************************************************************
    * After refactor
    ***************************************************************/
    // The functions below replace the various calls with the default parameters

    // By putting the logic for which tab gets what data in this function rather than copy/pasteing a conditional statement
    // that evaluates to the activeOnly paramter we avoid copy/paste code of the conditional and make the logic clearer
    // and less prone to mistakes
    private getDataForTabId(tabId) {
    if (tabId === LaAlerting.ACTIVE_TAB.id) {
    this.retrieveOnlyActiveAlerts();
    }
    else {
    this.retrieveAlerts();
    }
    }

    private retrieveOnlyActiveAlerts() {
    return this.retrieveData(true, false);
    }

    private retrieveAlerts() {
    return this.retrieveData(false, false);
    }

    private retrieveData(activeOnly:boolean, inactiveOnly:boolean) {
    // gets data
    }

    // Later on in code
    // No repeated boolean logic
    this.getDataForTabId(this.$scope.activeTabsAlerts.id);

    // Later on in code
    // Code is self describing and says what it does
    this.retrieveOnlyActiveAlerts();