Gejala Code Smell, seberapa bau kode anda ?

Gejala code smell dalam Class

  • komentar: kalau bisa program direfaktor sehingga komentar tidak lagi diperlukan. Kalau terpaksa, harus menjelaskan kenapa (bukan apa)
  • Metode panjang: metode pendek lebih mudah dibaca, dimengerti dan diperbaiki. metode panjang perlu direfaktor sehingga menjadi pendek
  • Parameter yang banyak: batasi parameter
  • Kode berulang: Donot Repeat Yourself !
  • Class besar: perlu di restruktur sehingga menjadi kecil-kecil
  • Menempatkan tipe pada nama metode: hindari hal ini, karena tidak hanya menjadikan redundan, akan tetapi juga menyebabkan penggantian nama jika tipe berganti
  • Nama yang tidak komunikatif: apakah nama metode anda sudah sesuai?menjelaskan fungsinya?
  • Nama yang tidak konsisten: kalau anda punya metode OPEN(), anda perlu punya metode CLOSE()
  • Dead Code: hapuslah kode yang tidak terpakai
  • Speculative Generality: tulislah kode yang diperlukan saat ini, Always implement things when you actually need them, never when you just foresee that you need them
  • Field Sementara: perhatikan objek yang berisi banyak field. Jika melewatkan parameter, pastikan semua field terisi

Referensi : coding horror

Connection Strings

1. PHP, MySQL

mysql_connect(‘server’,’user’,’password’);
mysql_connect_db(‘database’);

2. Visual Studio 2010, MS SQL Server Express 2010

providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=xxx;Integrated Security=true;MultipleActiveResultSets=True;"

Work at Google

Working @ Google inc.
How the hell a fun work environment? For those of you IT professionals may need to try this …

Create Custom Blinking Button with Blackberry SDK

This is a simple code I have writen as Blackberry Developer.
Altough this article is tagged under Java Category, but this article work on Blackberry SDK.
You can change the background color of Button by customize the extending the ButtonField Class. Below is a sample of customizing ButtonField. This Button will blink when you click it and will stop blinking after you click it again:


package puji.padi.PjiDakon2;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.ButtonField;

public class BlinkingButton extends ButtonField{
int width;
int height;
String _text;
int padding = 5;
int _backgroundcolor;
public boolean blinking;
BlinkingButton(String text){
super(text,ButtonField.CONSUME_CLICK);
Font font = Font.getDefault();
_text = text;
width = getPreferredWidth();
height = padding + font.getHeight()+ padding;
height=super.getHeight()+padding*2;
height=getPreferredHeight();
_backgroundcolor = Color.AQUA;
blinking=false;
}
protected void layout(int a, int b){
setExtent(width, height);
}
public void paint(Graphics g){
g.setColor(Color.BISQUE);
g.setColor(_backgroundcolor);
g.drawText(_text, 0, 0);
}
public void setBackgroundColor(int color){
_backgroundcolor = color;
invalidate();
}
public void setLabel(String text){
_text = text;
invalidate();
}
public String getLabel(){
return _text;
}
public void blink(){
blinking=!blinking;
Thread trblink = new Blinky();
trblink.start();
}
class Blinky extends Thread{
public void run(){
while(blinking){
_backgroundcolor = Color.GOLD;
invalidate();
try{
Thread.sleep(200);
}
catch(Exception e){

}
_backgroundcolor = Color.BROWN;
invalidate();
try{
Thread.sleep(200);
}
catch(Exception e){

}
}
}
}
}

the overall code will be like this:


package puji.padi.PjiCustomButton2;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;

public class MainClass extends UiApplication{
MainClass(){
HomeScreen home=new HomeScreen();
pushScreen(home);
}
public static void main(String[] args){
MainClass app=new MainClass();
app.enterEventDispatcher();
}
class HomeScreen extends MainScreen{
HomeScreen(){
BlinkingButton bb = new BlinkingButton("Click to Blink");
add(bb);
bb.setChangeListener(listener);
}
FieldChangeListener listener = new FieldChangeListener(){
public void fieldChanged(Field field, int context){
BlinkingButton bb = (BlinkingButton)field;
bb.blink();
}
};
}
}

BlinkingButton
This will result a Blackberry Button. It will be blinking when you click, and will be stopped when you click again.
Feel free to post your comment …

Thread

Pada pemrograman java, thread dapat dilakukan dengan 2 cara, pertama yaitu melakukan extends pada class Thread, kedua melakukan implements pada interface Runnable.
Contohnya dengan extends Thread adalah sebagai berikut:

class MyThread extends Thread{
public void run(){
for(int cc=0;cc<100;cc++){
System.out.println(Integer.toString(cc));
}
}
}

Contoh dengan implements Runnable adalah sebagai berikut:

class MyThread implements Runnable{
public void run(){
for(int cc=0;cc<100;cc++){
System.out.println(Integer.toString(cc));
}
}
}

implements Runnable dipergunakan apabila class sudah melakukan extends dari class lainnya, hal ini disebabkan karena java tidak bersifat multiple inheritance, yaitu hanya dapat diturunkan dari 1 parent class.

Follow

Get every new post delivered to your Inbox.