コード匂い21 -匿名の機能乱用者
12950 ワード
機能、ランムラ、閉鎖.とても高い、非宣言的で、熱い.
保守性テスト可能性 コード再利用 を隠す実装
のデバッグ
ラップ機能/閉鎖 メソッドオブジェクト/戦略 のアルゴリズムを修正します
サンプルコード
クロージャと匿名の関数は、コードブロックをモデル化するために非常に有用です. のプリミティブ の乱用者 結論
TL;DR: Don't abuse closures and functions. Encapsulate them into objects.
問題
保守性
解決策
サンプルコード
間違い
sortFunction = function(arr, fn) {
var len = arr.length;
for (var i = 0; i < len ; i++) {
for(var j = 0 ; j < len - i - 1; j++){
if (fn(arr[j], arr[ j+ 1])) {
var temp = arr[j];
arr[j] = arr[j+1];
arr[j + 1] = temp;
}
}
}
return arr;
}
scores = [9, 5, 2, 7, 23, 1, 3];
sorted = sortFunction(scores, (a,b) => {return a > b});
右
class ElementComparator{
greatherThan(firstElement, secondElement){
return firstElement > secondElement;
//This is just an example. With more complex objects this comparison might not be trivial
}
}
class BubbleSortingStrategy {
//We have a strategy, we cant unit test it, change for a polymorphic,
//Swap and benchmark algorithms etc.
constructor(collection, comparer){
this._elements = collection;
this._comparer = comparer;
}
sorted(){
for (var outerIterator = 0; outerIterator < this.size() ; outerIterator++) {
for(var innerIterator = 0 ; innerIterator < this.size() - outerIterator - 1; innerIterator++){
if (this._comparer.greatherThan(this._elements[innerIterator], this._elements[ innerIterator + 1])) {
this.swap(innerIterator);
}
}
}
return this._elements;
}
size() {
return this._elements.length;
}
swap(position){
var temporarySwap = this._elements[position];
this._elements[position] = this._elements[position + 1];
this._elements[position + 1] = temporarySwap;
}
}
scores = [9, 5, 2, 7, 23, 1, 3];
sorted = new BubbleSortingStrategy(scores,new ElementComparator()).sorted();
検出
sortFunction = function(arr, fn) {
var len = arr.length;
for (var i = 0; i < len ; i++) {
for(var j = 0 ; j < len - i - 1; j++){
if (fn(arr[j], arr[ j+ 1])) {
var temp = arr[j];
arr[j] = arr[j+1];
arr[j + 1] = temp;
}
}
}
return arr;
}
scores = [9, 5, 2, 7, 23, 1, 3];
sorted = sortFunction(scores, (a,b) => {return a > b});
class ElementComparator{
greatherThan(firstElement, secondElement){
return firstElement > secondElement;
//This is just an example. With more complex objects this comparison might not be trivial
}
}
class BubbleSortingStrategy {
//We have a strategy, we cant unit test it, change for a polymorphic,
//Swap and benchmark algorithms etc.
constructor(collection, comparer){
this._elements = collection;
this._comparer = comparer;
}
sorted(){
for (var outerIterator = 0; outerIterator < this.size() ; outerIterator++) {
for(var innerIterator = 0 ; innerIterator < this.size() - outerIterator - 1; innerIterator++){
if (this._comparer.greatherThan(this._elements[innerIterator], this._elements[ innerIterator + 1])) {
this.swap(innerIterator);
}
}
}
return this._elements;
}
size() {
return this._elements.length;
}
swap(position){
var temporarySwap = this._elements[position];
this._elements[position] = this._elements[position + 1];
this._elements[position + 1] = temporarySwap;
}
}
scores = [9, 5, 2, 7, 23, 1, 3];
sorted = new BubbleSortingStrategy(scores,new ElementComparator()).sorted();
タグ
結論
人間はコードを読む.ソフトウェアは匿名関数でOKですが、複数のクロージャが起動されると保守性が損なわれます.
関係
コード匂い06
マキシ・コンティ・ 月25日・ 2分読む
#codenewbie
#tutorial
#beginners
クレジット
Roman MagerのUnsplashによる写真
Object-oriented programming increases the value of these metrics by managing this complexity. The most effective tool available for dealing with complexity is abstraction. Many types of abstraction can be used, but encapsulation is the main form of abstraction by which complexity is managed in object-oriented programming.
レベッカWirfsブロック
ソフトウェア工学大引用
マキシ・コンティ・ Dec 28 ' 20・ 13分読む
#codenewbie
#programming
#quotes
#software
この記事はCodesmellシリーズの一部です.
あなたのコードの臭い部分を見つける方法
マキシ・コンティ・ May 21 ' 21・ 4分読む
#codenewbie
#tutorial
#codequality
#beginners
最終更新日: 2021/07/03
Reference
この問題について(コード匂い21 -匿名の機能乱用者), 我々は、より多くの情報をここで見つけました
https://dev.to/mcsee/code-smell-21-anonymous-functions-abusers-47f5
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
コード匂い06
マキシ・コンティ・ 月25日・ 2分読む
#codenewbie
#tutorial
#beginners
クレジット
Roman MagerのUnsplashによる写真
Object-oriented programming increases the value of these metrics by managing this complexity. The most effective tool available for dealing with complexity is abstraction. Many types of abstraction can be used, but encapsulation is the main form of abstraction by which complexity is managed in object-oriented programming.
レベッカWirfsブロック
ソフトウェア工学大引用
マキシ・コンティ・ Dec 28 ' 20・ 13分読む
#codenewbie
#programming
#quotes
#software
この記事はCodesmellシリーズの一部です.
あなたのコードの臭い部分を見つける方法
マキシ・コンティ・ May 21 ' 21・ 4分読む
#codenewbie
#tutorial
#codequality
#beginners
最終更新日: 2021/07/03
Reference
この問題について(コード匂い21 -匿名の機能乱用者), 我々は、より多くの情報をここで見つけました
https://dev.to/mcsee/code-smell-21-anonymous-functions-abusers-47f5
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
Object-oriented programming increases the value of these metrics by managing this complexity. The most effective tool available for dealing with complexity is abstraction. Many types of abstraction can be used, but encapsulation is the main form of abstraction by which complexity is managed in object-oriented programming.
ソフトウェア工学大引用
マキシ・コンティ・ Dec 28 ' 20・ 13分読む
#codenewbie
#programming
#quotes
#software
あなたのコードの臭い部分を見つける方法
マキシ・コンティ・ May 21 ' 21・ 4分読む
#codenewbie
#tutorial
#codequality
#beginners
Reference
この問題について(コード匂い21 -匿名の機能乱用者), 我々は、より多くの情報をここで見つけました https://dev.to/mcsee/code-smell-21-anonymous-functions-abusers-47f5テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol