Last active
August 9, 2018 18:33
-
-
Save mrhampson/46b2ac01287f93a1531efcb2e12dce0c to your computer and use it in GitHub Desktop.
Revisions
-
mrhampson revised this gist
Aug 9, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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. -
mrhampson revised this gist
Aug 9, 2018 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
mrhampson renamed this gist
Aug 9, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
mrhampson renamed this gist
Aug 9, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
mrhampson revised this gist
Aug 9, 2018 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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. */ /*************************************************************** -
mrhampson revised this gist
Aug 9, 2018 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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 relys on the developer memorizing the definition this.retrieveData(true) /*************************************************************** * After refactor ***************************************************************/ // 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 -
mrhampson created this gist
Aug 9, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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();