Академический Документы
Профессиональный Документы
Культура Документы
1. import java.util.Random;
2. import java.util.Vector;
3.
4. public class MainSystem {
5.
6. public static void main(String[] args) {
7. String [] it = {"Sandal xl","Sandal l","Sandal v", "Shoes xl","Shoes l", "Shoes v"};
8. int [] pt = {100, 90, 80, 200, 190, 185};
9. int [] hp = {1200, 1150, 1100, 2500, 2300, 2200};
10. int [] m = {1, 1, 2, 3, 3, 4, 5};
11. Random r = new Random(232);
12. Report report = new Report();
13. Transaction t = new Transaction(1, 2016);
14.
15. for(int i=0; i<it.length; i++)
16. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
17. report.AddTransaction(t);
18. t = new Transaction(2, 2016);
19. for(int i=0; i<it.length; i++)
20. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
21. report.AddTransaction(t);
22. t = new Transaction(3, 2016);
23. for(int i=0; i<it.length; i++)
24. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
25. report.AddTransaction(t);
26. t = new Transaction(4, 2016);
27. for(int i=0; i<it.length; i++)
28. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
29. report.AddTransaction(t);
30. t = new Transaction(5, 2016);
31. for(int i=0; i<it.length; i++)
32. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
33. report.AddTransaction(t);
34. t = new Transaction(6, 2016);
35. for(int i=0; i<it.length; i++)
36. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
37. report.AddTransaction(t);
38. t = new Transaction(7, 2016);
39. for(int i=0; i<it.length; i++)
40. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
41. report.AddTransaction(t);
42. t = new Transaction(8, 2016);
43. for(int i=0; i<it.length; i++)
44. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
45. report.AddTransaction(t);
46. t = new Transaction(9, 2016);
47. for(int i=0; i<it.length; i++)
48. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
49. report.AddTransaction(t);
50. t = new Transaction(10, 2016);
51. for(int i=0; i<it.length; i++)
52. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
53. report.AddTransaction(t);
54. t = new Transaction(11, 2016);
1. Kelompok The Bloater, dengan jenis code smell long method yang terdapat pada 2 class,
yaitu class Mainsystem dan class Report, the bloater merupakan kelompok code smell yang
menunjukkan sesuatu yang berkembang terlalu besar sehingga tidak dapat ditangani secara
efisien. Sesuatu tersebut dapat berupa metode yang terlalu panjang, kelas yang terlalu
besar, daftar parameter yang terlalu banyak, penggunaan data primitive yang berlebihan,
ataupun penggunaan data clumps. Pada code tersebut terdapat 2 class yang mengandung
Method yang isinya sangat banyak.
Potongan code pada Class Mainsystem yang mengandung code smell long method yaitu,
Potongan code pada Class Report yang mengandung code smell long method yaitu,
int total = 0;
System.out.println("=================================================");
System.out.println("=================================================");}
2. Kelompok The dispensable dengan jenis code smell Dead Code yang terdapat pada class
MainSystem di baris/line nomor 10. The dispensable adalah adanya sesuatu yang tidak
dibutuhkan dan seharusnya dapat dihapus dari source code. Pada code tersebut terdapat
1 class yang mengandung Dead code dimana kode program dapat dihapus karena tidak
dipakai saat eksekusi program.
Potongan code pada Class Mainsystem yang mengandung code smell Dead Code yaitu,
3. Kelompok The Coupler dengan jenis code smell Feature Envy yang terdapat pada class
MainSystem . The coupler merupakan kondisi dimana terdapat pelanggaran terhadap
prinsip coupling. Coupling merupakan ukuran keterikatan sebuah module dengan module
lainnya. Prinsip coupling menekankan pada penerapan low coupling, sehingga perubahan
pada satu module tidak akan (atau sedikit) mempengaruhi module lainnya. Salah satu
kondisi pelanggaran prinsip coupling adalah dimana sebuah metode mengakses data dari
object lainnya lebih sering dibandingkan data dari dalam kelas itu sendiri. Penggunaan
middle man juga merupakan indikasi pelanggaran dari prinsip low coupling. Pada code
tersebut terdapat 1 class yang mengandung Feature Envy dimana kode program tersebut
mengandung Sebuah method yang sangat bergantung pada data yang ada di class lain,
bukan di data milik class tempat method tersebut berada.
Potongan code pada Class Mainsystem yang mengandung code smell Feature Envy
yaitu,
4. Kelompok The dispensable dengan jenis code smell Duplicate Code yang terdapat pada
class MainSystem . The dispensable adalah adanya sesuatu yang tidak dibutuhkan dan
seharusnya dapat dihapus dari source code. Pada code tersebut terdapat 1 class yang
mengandung Duplicate Code dimana kode tersebut memiliki kode program yang
terduplikasi.
Potongan code pada Class Mainsystem yang mengandung code smell Duplicate Code
yaitu,
public static void main(String[] args) {
report();
b. Dari source code tersebut terdapat beberapa code smell, diantaranya yaitu code
smell long method, duplicate code, dead code, dan feature envy. Berikut hasil
refactoring dari tim kami,
import java.util.Random;
import java.util.Vector;
class Item {
private String name;
private int price;
private int quantity;
private int highSeasonPrice;
private final int HIGH_SEASON_START = 5;
private final int HIGH_SEASON_END = 8;
class Transaction {
private Vector<Item> items = new Vector<>();
private int month;
private int year;
public Transaction(int month, int year) {
this.month= month;
this.year = year;
}
public Vector<Item> getItems() {
return items;
}
public int getMonth() {
return month;
}
public int getYear() {
return year;
}
public void addItem(Item item) {
items.add(item);
}
public int getTotalPrice(){
int total = 0;
for(Item e:items) {
total += e.getPrice(this.month, e.getQuantity());
}
return total;
}
}
class Month {
private String[] months =
{"January","February","March","April","May","June","July","August","Septembe
r","October","November","December"};
protected int month;
public String getText() {
return months[month - 1];
}
}
System.out.println("=================================================");
month = monthStart;
System.out.print("Report from " + getText() + " " + yearStart + " to
");
month = monthEnd;
System.out.println(getText() + " " + yearEnd);
System.out.println("=================================================");
int total = total(monthStart, yearStart, monthEnd, yearEnd);
System.out.println("=================================================");
System.out.format("Total Transaction = %,15d\n", total);
System.out.println("=================================================");
}
https://thesolidsnake.wordpress.com/2013/05/03/belajar-mencium-bau-nya-kode-
program-code-smell/
https://www.altexsoft.com/blog/engineering/code-refactoring-best-practices-
when-and-when-not-to-do-it/
https://dzone.com/articles/smells-in-java-code-do-you-recognize-them
https://dzone.com/articles/staring-into-my-java-crystal-ball-2020
https://refactoring.guru/refactoring/smells
Eclipse – workspace
Intellij IDEA
Lecture Note