Calm Hill My Random Thoughts

5 Rules of Programming

Rob Pike ရဲ့ Rule တစ်ခုကို မနေ့ညက Twitter မှာတွေ့လိုက်လို့ သူရေးထားတဲ့ဟာတွေ စုထားတာကို ပြန်လိုက်ရှာရတယ်။ အလုပ်ထဲမှာ လက်တွေ့လိုက်နာသင့်တဲ့ လက်တွေ့ကျတဲ့ အကြံပေးချက်တွေပါ တစ်ခုတော့ရှိတယ် ကိုယ်လိုရာကိုယ်ဆွဲပြီးတော့ သူပြောတာနဲ့ ကိုယ်အခြေအနေနဲ့ မကိုက်လည်း အတင်းကိုက်အောင် စကားနိုင်လုသလို အတင်းဆွဲဆက်စပ်တယ် လုပ်နေရင်တော့ ပြဿနာရှိပါတယ် အဲလိုအဖြစ်ကလည်း မကြာခဏမြင်ဖူးတတ်လို့ပါ။

Rob Pike’s 5 Rules of Programming

  • Rule 1. You can’t tell where a program is going to spend its time. Bottlenecks occur in surprising places, so don’t try to second guess and put in a speed hack until you’ve proven that’s where the bottleneck is.
  • Rule 2. Measure. Don’t tune for speed until you’ve measured, and even then don’t unless one part of the code overwhelms the rest.
  • Rule 3. Fancy algorithms are slow when n is small, and n is usually small. Fancy algorithms have big constants. Until you know that n is frequently going to be big, don’t get fancy. (Even if n does get big, use Rule 2 first.)
  • Rule 4. Fancy algorithms are buggier than simple ones, and they’re much harder to implement. Use simple algorithms as well as simple data structures.
  • Rule 5. Data dominates. If you’ve chosen the right data structures and organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming.

Excerpt from: http://users.ece.utexas.edu/~adnan/pike.html

Rule 1 မှာ တကယ်တမ်းပြဿနာ ဘယ်နားမှာဖြစ်နေမှန်းမသိသေးရင် Speed Hack ဆိုတဲ့နေရာမှာ လူအများစုကဒီလိုပဲ လုပ်လေ့ရှိကြတာပါပဲ ဒါပေမယ့် Rule 1 အရဆိုပြီး ဂုဏ်ယူပြီးတော့ ရပ်မထားဖို့တော့လိုတယ် အချိန်ရတာနဲ့ တကယ်တမ်းပြဿနာဖြစ်နေတဲ့ Bottleneck ကိုတော့ ရအောင်ရှာကြရလိမ့်မယ် မဟုတ်ရင်တော့ လက်ပူတိုက်ပြီး တော်သလိုပြင်ရင်းပြင်ရင်းနဲ့ ဖတ်လို့မရတော့တဲ့ ကုဒ်တွေဖြစ်ကုန်ပါလိမ့်မယ်။

Rule 2 ကတော့ လူတိုင်းမှာဖြစ်လေ့မရှိဘူး Measure ဆိုတာက လူတိုင်းလုပ်လေ့မရှိဘူး အများအားဖြင့် Academic ကလူအတော်များများပဲ လုပ်လေ့ရှိကြတယ်။ မှားတတ်တဲ့လူတွေက လိုအပ်တဲ့ Knowledge ထက်ပိုနေတဲ့လူတွေမှာပါ သိတာကများနေတော့ ဒီလိုလုပ်ရင်ကောင်းတယ် ဒီလိုလုပ်ရင်မကောင်းဘူးနဲ့ တကယ်တမ်း Measurement မလုပ်ကြည့်တဲ့အတွက် ဆုံးဖြတ်ချက်မှားတာ တစ်ခါတစ်ရံတော့ လက်တွေ့တိုင်းတာကြည့်တဲ့ Empirical method ကသာ Theoretically ထက်စိတ်ချရပါတယ် ဒါပေမယ့် Measurement လုပ်ဖို့ကလည်း လွယ်တဲ့အလုပ်မဟုတ်သလို အချိန်ပေးရတယ်ဆိုတာလည်း ရှိပါသေးတယ် တကယ်ကတော့ Empirical နဲ့ Theoretical နှစ်ခုမျှနိုင်ရင် အကောင်းဆုံးပါ။

Rule 3 ကတော့ တလွဲအသုံးချခံရဆုံး ဖြစ်လိမ့်မယ် ဗမာစကားပုံတစ်ခုမှာ ဆင်ချည်တဲ့ကြိုးနဲ့ ကြက်ချည်လို့မရဘူးဆိုတာရှိတယ် အင်မတန်နာမည်ကြီးတဲ့ Algorithm တွေရှိတယ် ဒါပေမယ့် နေ့စဉ်သုံး မဟုတ်တာတော့ လက်ခံပါတယ် တလွဲအသုံးခံရပုံက လူအတော်များများက Fancy Algorithm ဆိုတာကို အသုံးချမှားတာမဟုတ်ပဲ ရေးချင်သလိုရေးထားတဲ့ ကုဒ်တွေကို Not Fancying လို့ ဂုဏ်ယူစွာနဲ့ ပြောတတ်ကြတာပါ တကယ်က Fancy, Simple and Stupid Algorithm လို့အမျိုးအစားခွဲရမှာ Simple ဆိုတာကကောင်းတယ် ဒါပေမယ့် Stupid အထိ ဆင်းသွားတာကို တလွဲဆံပင်ကောင်းဖြစ်ပြီး ဂုဏ်ယူမနေဖို့ပါ။

Rule 4 ကတော့ ဘယ်သူမှငြင်းမယ်တော့ မဟုတ်ပါဘူး Fancy Algorithm တွေက Implementation လုပ်ဖို့ဆိုရင် တကယ်လည်း ရေရေရာရာနားလည်ရမှာဖြစ်သလို သင့်လျော်တဲ့ Programming Language ကိုရွေးချယ်ဖို့လိုသလို လုံလောက်တဲ့ ကျွမ်းကျင်မှုလည်းရှိဖိုလိုပါတယ်။ တစ်ခါက Evolution of Programming Knowledge Level မှာ Algorithm အမျိုးမျိုးနဲ့ရေးပြီးတော့ Measurement လုပ်ထားတာရှိတယ် အဲဒါကိုကြည့်ရင် Rule 3 & 4 အတွက် ဥပမာအနေနဲ့ နားလည်နိုင်ပါတယ်။

Rule 5 ကလူအတော်များများ မလိုက်နာဆုံးအချက်ပဲဖြစ်မယ် Algorithm & Data structures ဆိုတာက အစကတည်းကတွဲလျက် ကျောင်းတက်ကတည်းကသင်ပေမယ့် ဘယ်သူမှ အလေးမထားဖြစ်တာ များလိမ့်မယ် Programming တကယ် စိတ်ဝင်စားပါတယ်ဆိုတဲ့လူလည်း ရေးမယ်ဆိုတာကလွဲရင် အဲဒါတွေကို တန်ဖိုးထားမိတဲ့လူက မြင်ဖူးသလောက်ကနည်းပါတယ် အဲဒီလိုနဲ့ စနစ်ကျတဲ့ Structure မရှိတော့ပဲနဲ့ ထင်သလိုသိမ်းဖြစ်သလိုသိမ်း System ကြီးလာတာနဲ့အမျှ Data ပြဿနာကိုရှင်းရတာက ပိုများလာတဲ့ အချိန်ဆိုတာက အလုပ်ထဲရောက်မှနားလည်ပြီးတော့ Rule 5 ကိုသဘောတူကြတာ။

တက်ကျမ်းဖတ်သလိုပဲ ဖတ်ပြီးတော့ ကိုယ့်ဖြစ်နေတာနဲ့ အတင်းဆက်စပ်ပြီးတော့ တူတယ်တူတယ်ဆိုပြီး လုပ်နေရုံနဲ့တော့ တက်လမ်းလည်းမရှိသလို ကျမ်းထဲကလိုချမ်းသာမှာမှမဟုတ်တာ။ ကိုယ့်ရှေ့ကလူတွေ ပြောသွားတဲ့ အကြံပေးချက်ကို လက်ခံသင့်မခံသင့် အရင်ဆုံးဆုံးဖြတ် လက်ခံနိုင်တယ်ဆိုရင်လည်း ကိုယ်တကယ်တမ်း လိုက်နာမိရဲ့လားသုံးသပ် လွဲနေတာကို အတင်းဆွဲဆက်စပ်ပြီးတော့ ဖြေသိမ့်နေရင်တော့ ဘယ်လိုမှလည်း တိုးတက်စရာမှမရှိတာ အားတဲ့အခါတော့ Evolution ကိုရေးသလို Data Structures အလွဲအကြောင်းကို ဥပမာတွေနဲ့ တစ်ခုရေးမယ်တော့စိတ်ကူးတယ် ရုံးမှာလည်းအလုပ်များ သင်တန်းမရှိတဲ့ရက် ညပိုင်းပဲစိတ်ကူးပေါက်ရာ စာကိုအကြမ်းရေးထား နေ့လည်စာစားချိန်ခဏနားရင် ရေးထားတာခဏပြန်ဖတ် စိတ်ကူးပေါက်ရင်တင် ဘာပဲဖြစ်ဖြစ်အချိန်လုပြီးတော့ ရေးပါဦးမယ်။