5 Error Styles in Coding

by 07:27 0 comments
မေန့က O'REILLY ရဲ့ How to make mistakes in Python ဆိုတဲ့စာအုပ္ဖတ္ျပီး အဲ့ထဲကသေဘာက်တဲ့အခန္းတခန္းကိုျပန္ေရးလိုက္တာပါ။
အခုေျပာျပမွာကေတာ့ ကြ်န္ေတာ္တို့ေတြ code ေရးရာမွာမွားယြင္းေနတဲ့ style ၅ ခုအေျကာင္းပါ။ ဒီအမွားေတြက code ကိုအလုပ္မလုပ္တာေတာ့မျဖစ္ပါဘူး ဒါေပသိ ကိုယ္ေရးခဲ့တဲ့ code ကိုတခ်ိန္ျပန္ဖတ္တဲ့အခါက်အေတာ္ကိုဒုခေပးေစပါတယ္။

Hungarian Notation

ပထမဆံုးအမွားကေတာ့ Hungarian Notation လို့ေခါ္ပါတယ္။ သူကေတာ့ Variable Name ေရွ့မွာ variable ကဘာလဲဆိုတာကိုထည့္ေရးတာပါ။
   
 strName = "PyLab"  
 str_First_Name = "Hades.y2k"  
 intYear = 2016  
 lstProducts = []  
 product_list = []  
 dctParams = {}  
ဒီလိုမ်ိုးေရးျကတဲ့သူေတြရွိပါတယ္။ ဘာအတြက္လဲဆို ရည္ရြယ္ခ်က္ကေတာ့ ေနာင္တခ်ိန္ code ကိုျပန္ဖတ္တဲ့အခါဘယ္ေကာင္ကဘာလဲသိဖို့ပါ။ သို့ေပမယ့္ ထင္ထားသလိုျဖစ္မလာပဲ ဖတ္တဲ့အခါပိုျကည့္ရခက္သြားပါတယ္။
ေနာက္ေရးပါမ်ားလာတဲ့အခါေအာက္ကလိုအမွားေတြပါျဖစ္လာပါေတာ့တယ္။
 strCustomNumber = "12345"  
   
 blnFoo = "N"  
ေျပာရမယ္ဆိုပိုလြယ္လာမယ္လို့ထင္ေပမယ့္ ထင္သလိုမျဖစ္ပဲ Hungarian Notation ကအေနွာင့္အယွက္တစ္ခုသာျဖစ္ေစပါတယ္။

PEP-8 Violation

PEP-8 ဆိုတာကေတာ့ Python code ေရးတဲ့အခါလိုက္တာသင့္တဲ့ standardized style တစ္ခုပါ။
ေအာက္ကကုဒ္ကိုတစ္ခ်က္ျကည့္လုိက္ပါ။
 01) class MyGiganticUglyClass(object):  
   
 02) def iUsedToWriteJava(self,x,y = 42):  
   
 03)  blnTwoSpacesAreMoreEfficient = 1  
   
 04)  while author.tragicallyConfused():  
   
 05)    print "Three spaces FTW roflbbq!!1!"  
   
 06)  if (new_addition):  
   
 07)    four_spaces_are_best = True  
   
 08)  if (multipleAuthors \  
   
 09)    or peopleDisagree):  
   
 10)      print "tabs! spaces are so mainstream"  
   
 11)  ...  
   
 12)  return ((pain) and (suffering))  
Line Number 2 မွာေျပာခ်င္တာရွိပါတယ္။ အဲ့တာေတြကေတာ့ Indentation issue, Whitespace issue နဲ့ Inconsistent case ရယ္ပါ။

Indentation issue ကေတာ့ ဒုတိယလိုင္းမွာ class နဲ့ def က whitespace ၂ ခုသာရွိပါတယ္ သို့ေသာ္ def နဲ့ bln... မွာေတာ့ whitespace က၃ခုျဖစ္သြားပါတယ္။ ေနာက္ tab နဲ့ whitespace ကေရာသံုးတာမ်ိုးကိုေရွာင္ပါ။
ကြ်န္ေတာ္ rule ဖတ္ဖူးတာကိုေျပာျပရရင္ အေကာင္းဆံုးကေတာ့ whitespace 4 ခုနဲ့ indentation ကိုလုပ္ပါ။
Python မွာက  program flow အတြက္ whitespace/tab ကိုျကည့္တာပါ ဒီေတာ့ whitespace/tab ေတြေရာသံုးျပီး သက္ဆိုင္ရာ code line က သူနဲ့ဆိုင္တဲ့ block မွာရွိမေနခဲ့ရင္ logic error ပါျဖစ္သြားနိုင္ပါတယ္။

whitespace issue ကိုေျပာခ်င္တာကေတာ့ Line number 2 က parentheses ထဲက , ျပီးရင္ whitespace မခံသလို variable name နဲ့ value ျကားမွာ whitespace ေတြထည့္ထားပါတယ္။ self,x,y = 42
ဒါကအမွားတစ္ခုပါပဲ တကယ္ဆို comma (,) ျပီးရင္ whitespace တစ္ခ်က္၀င္ရသလိုေနာက္တစ္ခုက y=42 ျဖစ္သင့္တာပါ။
ေနာက္ဒီ code တစ္ခုလံုးမွာလဲ class နဲ့ function definitions ေတြျကားမွာ blank line ထည့္လိုက္ရင္ code ကိုဖတ္လုိ့ပိုအဆင္ေျပသြားမွာပါ။

Inconsistent case ကေတာ့ function name ပါ။ Python community ကေျပာတာကေတာ့ variable name ေတြကို lowercase နဲ့ underscore ကိုဖတ္ရလြယ္ကူေစရန္အတြက္အသံုးျပုခိုင္းပါတယ္။ ဥပမာအဲ့ function name
ကိုျပန္ေရးျပရမယ္ဆို i_used_to_write_java ေပါ့။

က်န္တဲ့ line ေတြကိုေျပာရမယ္ဆို
Line number 3 မွာကလဲ Indentation issue, Inconsistent case နဲ့ Hungarian Notation (I already explained at the beginning of the post) erros ေတြျဖစ္ေနပါတယ္။
Line number 4 မွာကေတာ့ Inconsistent case တက္ေနပါတယ္။

Line number 6 မွာကေတာ့ Extraneous parentheses error တက္ေနပါတယ္။ အဲ့တာကေတာ့ argument တစ္ခုပဲရွိတဲ့ေကာင္ကို parentheses နဲ့ပိတ္ထားတာပါ။ တကယ္လို့ Single argument ပဲရွိတယ္ဆို parentheses နဲ့ပိတ္စရာမလိုပါဘူး။

Line number 8 မွာမွားေနတာကေတာ့ Inconsistent case နဲ့ Extraneous line continuations ပါ။
Extraneous line continuations ဆိုတာကိုေျပာရမယ္ဆို parentheses အထဲမွာဆိုရင္ statement ကိုေနာက္တလိုင္းမွာဆက္ေရးတဲ့အခါက်ရင္ backslash (\) မလိုပါဘူး။

ေနာက္ဆံုးလိုင္းမွာဆိုရင္လဲ Extraneous parentheses error ျဖစ္ေနပါတယ္။
ဒီအမွားေတြကိုအကုန္ျပင္ထားတဲ့ေအာက္ကကုဒ္ေလးကိုတစ္ခ်က္ျကည့္ျကည့္ပါ။ ေသခ်ာတာကေတာ့အေပါ္ကေကာင္ထက္ဖတ္
ရတာေတာ္ေတာ္အဆင္ေျပပါတယ္။
 class MyMorePythonicClass(object):  
   
   
   def now_i_write_python(self, x, y=42):  
   
     two_spaces_hamper_readability = True  
   
   
     while author.tragically_confused():  
   
       print "Three spaces? What was I thinking?"  
   
   
     if new_addition:  
   
       four_spaces_are_best = True  
   
   
     if (multiple_authors or  
   
         people_disagree):  
   
       print "Mixing tabs and spaces is dangerous!"  
   
   
     ...  
   
   
     return sunshine and puppies  

Bad Naming

Bad Naming ကေတာ့ကြ်န္ေတာ္ကိုယ္တိုင္လဲေရးေနတဲ့သူထဲမွာပါပါတယ္။ ဘယ္ကေနကူးသလဲဆို Python စာအုပ္ေတြကပါပဲ။
စာအုပ္ေတြက example ေတြမွာဆို x, y တို့လို single character variable ေတြသံုးေလ့ရွိပါတယ္ for loop ပတ္တာေတြဆုိလဲ for x in ...
ေနာက္က်ကြ်န္ေတာ္ကိုယ္တိုင္လဲ script ေတြေရးေတာ့ for loop ပတ္ရင္ ပတ္တဲ့ေကာင္ရဲ့နာမည္ကိုေသခ်ာမစဥ္းစားေတာ့မိပဲ single character variable ေတြနဲ့ေပးျဖစ္ေတာ့တာပဲ။
ဒီလိုေရးတာေတြက တကယ္ဒုခေပးမေပးေအာက္ကေကာင္ေလးဖတ္ျကည့္ပါလား။
 f.write(string.join(map(lambda  
   
 x,y=self.__dicProfiles,z=strPy:"%0.3s %s:  
   
 %s:(%s)" % (z,x,y[x][0],y[x]  
   
 %[1]),self.__dicProfiles.keys()),'\n')  
   
 %+'\n')  
ဒီလိုအက်င့္ေလးေတြကို တကယ္ script ေရးသားတဲ့အခ်ိန္မွာေဖ်ာက္သင့္ပါတယ္ single-character variable အျပင္ေနာက္တခုက long names ဆိုတဲ့ name ကိုအရွည္ျကီးေပးတာေတြပါ ဥပမာေအာက္ကေကာင္လိုေပါ့

 class makeSomethingFancywithThisClass():  

ဒီလိုေရးတာေတြက function name သာဆိုခင္ဗ်ားျပန္ေခါ္တိုင္း characters တိုင္းအတိအက်မွတ္မိနိုင္ေသးလို့လား
typo error ေတြျဖစ္ျပီး coding ပိုင္းမွာမလိုလားအပ္တာေတြျဖစ္ေစနိုင္ပါတယ္။
Name တစ္ခုသတ္မွတ္တဲ့အခါနာမည္ကို လိုရင္းတိုရွင္းျဖစ္ရသလို သူ့ရဲ့လုပ္ေဆာင္ခ်က္နဲ့သက္ဆိုင္တဲ့အဓိပယ္လဲရွိရပါမယ္.

Inscrutable Lambdas

Lambda function ေတြကိုကြ်န္ေတာ္တုိ့ anonymous functions ေတြေရးရင္အသံုးျပုပါတယ္။ Lambda နဲ့ေရးရင္ concise ျဖစ္ေပမယ္လို့ ဖတ္တဲ့သူအတြက္အနည္းငယ္ေတာ့ခက္ခဲပါမယ္။
Lambda function ၂ခု၃ခုကို expression တစ္ခုထဲမွာထည့္သံုးရင္ confuse ျဖစ္ဖို့လဲလြယ္သလို code ကိုျပန္ဖတ္တဲ့အခါ
က်လဲတခ်ိန္တစ္ခုယူျပီးျပန္ဖတ္ရတာမ်ိုးေတြျဖစ္တတ္ပါတယ္။ ေနာက္ anonymous functions ေတြက normal functions ေတြလို reusable မျဖစ္ပါဘူး။ ေအာက္က၂ေကာင္ကိုယွဥ္ျကည့္ပါ။ Lambda နဲ့ေရးတဲ့အခါ same function ကို ၃ခုေရးရေပမယ့္ ပုံမွန္ေရးေနျကအတိုင္းဆို function ကို၁ခါပဲေရးျပီးျကိုက္သေလာက္ျပန္ေခါ္လို့ရပါတယ္။
Using Lambda
 foo = map(lambda x: x[-1].replace('pylab', 'python'), foos)  
   
 example = map(lambda x: x[-1].replace('pylab', 'python'), examples)  
   
 sample = map(lambda x: x[-1].replace('pylab', 'python'), samples)  
In Normal Way
 def pylab_to_python(input):  
   
   return input[-1].lower().replace('pylab', 'python')  
   
   
 foo = map(pylab_to_python, foos)  
   
 example = map(pylab_to_python, examples)  
   
 sample = map(pylab_to_python, samples)  

Incomprehensible Comprehensions

List comprehensions ဆိုတာေတာ္ေတာ္ေလးအသံုး၀င္ပါတယ္။ Example ဆိုေအာက္ကလိုေကာင္မ်ိုးေပါ့
noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]
ပထမ example ကသိပ္မရွုပ္ေပမယ့္ ေအာက္ကေကာင္ေလးထပ္ျကည့္လိုက္ပါ။

 prop_list = [  
   
 FilterProp(prop='P_EXCLUDED', data='_'.join([i, j, k]))  
   
 for i in prop_data[0]  
   
 for j in prop_data[1]  
   
 for k in prop_data[2]]  

ေသခ်ာတာေတာ့ အနည္းငယ္ေတာ့ေတြးရမွာပါ ဘာလုပ္တယ္ဆိုတာကို
List comprehensions ေတြေရးခဲ့အခါမွာ သတိျပုရမွာက comment ေတြထည့္ေပးဖို့ပါ ေနာက္တစ္ခုကအေပါ္ကလို
single-character name ေတြအသံုးမျပုဖို့ပါ ေနာက္ဒီ့ထက္ပိုရွင္းေစခ်င္ရင္ေတာ့ List comprehensions အစား
separate functions ေတြေရးျပီး ရွုပ္ေနတာကိုခြဲထုတ္လိုက္ပါ။

Error Style ၅ခုအေျကာင္းကေတာ့ဒါပါပဲ။
အေပါ္ကေျပာသြားတဲ့အခ်က္ေလးေတြကို တကယ္ေရးတဲ့အခါမွာလဲအက်င့္လုပ္သြားသင့္ပါတယ္။ ဒါက program error မတက္ေပမယ့္လုိ
ကုဒ္ကိုတခ်ိန္ျပန္ဖတ္တာ ဒါမွမပာုတ္တခုခုျပင္တဲ့အခ်ိန္မွာဒီလိုေရးတယ္မေရးဘူးကစကားေျပာလာမွာပါ။
Ref: How to make mistakes in Python (O'Reilly)

PyLab (c) 2016

Pylab

Developer

Keep calm and love Python

0 comments:

Post a Comment