Jump to content

Recommended Posts

Assalamualaikum W.B.T dan Selamat Sejahtera.

 

Merujuk perkara diatas, boleh tak bagi penerangan tentang DRY terms ni dalam programming.

 

dari ape yg sy faham adelah maksudnye jgn ulang code yg sama . mcmtu ke?

 

mohon berkongsi.

terima kasih.

Share this post


Link to post
Share on other sites

contoh mudah

 

kalau ada code yang tugas dia baca file, buat satu function / module untuk baca file je

 

so bila nak baca file, panggil function tu, tak perlu nak tulis benda yang sama ulang kali

 

tujuan contoh : bila kau ubah dari baca file ke baca URL (contohnya), tak perlu ubah software banyak2 tempat, ubah satu tempat je siap

Share this post


Link to post
Share on other sites
secara mudahnya, dry bermaksud kod yang berulang..

conthnya mungkin ada if block routine, dimana if block ni digunakan di banyak tempat dalam aturcara/script

so extract if block tersebut dan letak dalam function dan gantikan semua if block tadi dengan function td

Share this post


Link to post
Share on other sites

Kita ambil contoh paling mudah 

Katakan kita nak calculate antara dua nombor dengan formula kita telah tetapkan. Cuba tengok code pertama (C#)

 

public void calculateANumber()
{
textbox1.Text= (((1+1)+2)*5).ToString();
textbox2.Text= (((2+2)+2)*5).ToString();
textbox3.Text= (((3+3)+2)*5).ToString();
textbox4.Text= (((4+4)+2)*5).ToString();
textbox5.Text= (((5+5)+2)*5).ToString();
}

 

Apabila bro tengok, setiap kali kita nak buat calculation kita akan tulis semula formula dia setiap textbox. Jadi inilah maksud DRY dimana bro kena tulis semula benda yang sama setiap kali nak paparkan hasil. Jadi kita tengok pula contoh code no 2.

 

public void calculateANumber()
{
textbox1.Text= calculation(1,1);
textbox2.Text= calculation(2,2);
textbox3.Text= calculation(3,3);
textbox4.Text= calculation(4,4);
textbox5.Text= calculation(5,5);

}

public string calculation(int value1, int value2)
{
int total = 0;

total = (((value1+value2)+2)*5);

return total.ToString();
}

Kalau bro dapat perhatikan, kita akan tambah function baru yang dipanggil sebagai calculation. Apa yang ada didalam calculation itu adalah formula untuk mendapatkan hasil nombor.

Jadi, kita hanya perlu panggil function calculation dengan meletakkan value yang di inginkan. So bro tak perlu tulis semula formula yang sama berulang kali sebab function calculation itu dah ada formula tersebut. Berbezakan dengan code 1?

:D

Share this post


Link to post
Share on other sites
sekarang ni pun saya tengah buat kod berulang2... huhuhuh :P mula2 ok je tengok, bila kod dah memanjang kenalah buat sesuatu... Edited by syahmixp

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...