PEMROGRAMAN LANJUT Code Smells Bloaters Oleh Politeknik Elektronika
PEMROGRAMAN LANJUT Code Smells: Bloaters Oleh Politeknik Elektronika Negeri Surabaya 2018
Konten • • Code Smells: OVERVIEW Long Parameter List Long Method Large Class Data Clumps Primitive Obsession Percobaan
Code Smells and Refactoring
Bloaters: Long Parameter List build. Customer. Summary(parameter 1, parameter 2, …, parameter n) APA YANG SALAH DALAM POTONGAN KODING INI ? PERUBAHAN APA YANG ANDA LIHAT DIBANDINGKAN DENGAN POTONGAN KODING INI ? 4
BUG ? Technically Incorrect ? CODE SMELL If(Code Smells == Program Error) ? Weakness (Slowdown) Bug. . Bugs. . King of Bugs 5
MASALAH CODE SMELL SOLUSI REFACTORING 6 Refactoring is a controllable process of improving code without creating new functionality. It transforms a mess into clean code and simple design.
When Fixing a Bug Rule of Three When Adding a Feature 7 When to Refactor? During Code Review
Rule of Three When you are doing something for the third time, start refactoring. Refactor 8
Checklist of Refactoring done right way 1. Code should become cleaner. should functionality New 2. during refactoring. Finish all the program functionally first, then do Refactoring! 3. All existing tests must pass after refactoring. 9
Features of Clean Code : 1. 2. 3. 4. 5. 10 Clean Code is obvious for other programmer. Clean Code doesn’t contain duplication. Clean Code contains a minimal number of classes and other moving parts. Clean Code passes all tests Clean Code is easier and cheaper to maintain
Code Smells Couplers Dispensable Change Prventers Bloaters Object Oriented Abuser
Code Smells Couplers Dispensable Change Prventers Bloaters Object Oriented Abuser
Code Smells: Bloaters Long Parameter List Data Clumps Large Class Bloaters Long Method Primitive Obsession
Code Smells: Bloaters Long Parameter List Data Clumps Large Class Bloaters Long Method Primitive Obsession
Long Method yang memiliki banyak line ( >10 Lo. C) 15
Contoh Long Method 16
Refactoring Long Method 17
Code Smells: Bloaters Long Parameter List Data Clumps Large Class Bloaters Long Method Primitive Obsession
Contoh Large Class TUGAS CEO 19
Contoh Large Class 20
Refactoring Large Class 21
Refactoring Large Class 22
Refactoring Large Class 23
Code Smells: Bloaters Long Parameter List Data Clumps Large Class Bloaters Long Method Primitive Obsession
Refactoring Primitive Obsession 25
Refactoring Primitive Obsession Contoh 2. Menyimpan data Address dalam bentuk String. Kelemahan. 1. Sulit mendapatkan data spesifik dari Address, misal nama jalan, no. rumah, nama kota. 2. Memerlukan algoritma tambahan untuk mendapatkan data spesifik dari Address. Misal: harus melakukan split String. 26
Refactoring Primitive Obsession 27
Code Smells: Bloaters Long Parameter List Data Clumps Large Class Bloaters Long Method Primitive Obsession
Long Parameter List Method dengan jumlah parameter lebih dari empat. 29
Refactoring Long Parameter List 30
Code Smells: Bloaters Long Parameter List Data Clumps Large Class Bloaters Long Method Primitive Obsession
Data Clumps Sekumpulan data-data sejenis yang cenderung mengelompok di beberapa tempat (class). Kelemahan. Ada kemungkinan data tersebut tidak hanya dibutuhkan pada satu class saja. 32
Contoh Data Clumps 33
Refactoring Data Clumps 34
Refactoring Data Clumps 35
PERCOBAAN
37
Referensi • Tutorials. Point, 2017, “Design Pattern”, https: //www. tutorialspoint. com/design_pattern/, (diakses tanggal 24 November 2017) • Java. TPoint, 2017, “MVC Pattern”, http: //www. javatpoint. com/, (diakses tanggal 24 November 2017) • https: //www. youtube. com/watch? v=sz. Ok. Sr. ZGm. HU
- Slides: 39