PEP 8 (Style Guide)

by 00:45 0 comments


ဒီတစ်ခေါက်ကျွန်တော်ပြောချင်တာကတော့ code quality အကြောင်းပါ Code ရေးတဲ့အခါမှာကျွန်တော်တို့ အလုပ်လုပ်ရင်ပြီးကောလုပ်လို့မရပါဘူး။ နောက် modify လုပ်မှာတွေ sharing ပြုလုပ်တာမျိုးတွေအတွက် code အရေးအသားကလဲအလွန်ကိုအရေးကြီးလှပါတယ်။

ဒီတစ်ခေါက်ပြောမှာကတော့ PEP 8 Style Guide အကြောင်းပါ။ အကုန်လုံးတော့ပြောသွားလို့မဖြစ်နိုင်တဲ့အတွက်အရေးပါတာလေးတွေကိုပဲထောက်ပြသွားပါမယ်။

1. Import Statement

Python မှာ import statement ပြီးလို့ function ပိုင်းပေါ့စရေးတော့မယ်ဆို import လုပ်တဲ့အပိုင်းနဲ့ကြားမှာ new line 2 လိုင်းခြားပေးရပါတယ်။

Example

Bad Practice
import urllib

def main():
....

Correct Way
import urllib


def main():
....

2. Parentheses

Parentheses ပြီးရင် space မလာရပါဘူး။ statement တန်းလာရပါမယ်၊ အပိတ်ကလဲထိုနည်းလကောင်းပါပဲ။
Parentheses ဆိုတာ tuple, list, dictionary အကုန်လုံးပါဝင်ပါတယ်။ ဒီသုံးခုလုံးကိုရေးတိုင်းလိုက်နာရမှာဖြစ်ပါတယ်။


Example

Bad Practice
def main( var1 ):

dictionary = { "hello": 1, "world": 2 }

Correct Way
def main(var1):

dictionary = {"hello": 1, "world": 2}

3. Maximum 80 characters par word

Code ကလိုင်း၁လိုင်းမှာ စာလုံးရေ ၇၉ထက်ပိုခွင့်မရှိပါဘူး။

Example

အောက်ကကျွန်တော်ရေးထားတဲ့ code editor ကလိုင်း၁လိုင်းပါ

Bad Practice
self.printAction = QtGui.QAction(QtGui.QIcon("icons/print.png"), "Print document", self)

Correct Way
self.printAction = QtGui.QAction(QtGui.QIcon("icons/print.png"),
                                "Print document", self)

အဲ့မှာပြောစရာရှိတာက parentheses ထဲမှာဆို new line ဆင်းရင် escape (\) ခံစရာမလိုပါဘူးနောက် ပထမလိုင်းက , ပြီးရင်ဘာမှမရှိရပါဘူး including white space ပြီးတော့ ဆင်းလိုက်တဲ့ဒုတိယလိုင်းကလဲ parentheses စထားတဲ့အထဲမှာပဲရှိရပါမယ်။ ဒီမှာကျ Q ရဲ့အောက်ကနေစရေးရတာပေါ့ အောက်ကကောင်ဆိုလဲသူ့အဖွင့် parentheses အောက်ကမစတဲ့အတွက်မှားပါတယ်

Wrong

self.printAction = QtGui.QAction(QtGui.QIcon("icons/print.png"),
"Print document", self)

4. White space after comma

Comma တစ်ခုကုဒ်ပြီးတိုင်း white space လာရပါမယ်။

Example

Bad Practice
array = ['hello','world',101]

Correct Way

array = ['hello', 'world', 101]

5. Comment

Comment ရေးတဲ့အခါမှာလဲ # ပြီးရင် whitespace အရင်လာရပါမယ်။\

Example

Bad Practice
#This is comment

Correct Way

# This is comment

ဒါကတော့အဖြစ်များတာလေးတွေကိုထောက်ပြသွားတာပါ အသေးစိတ်ကို ဒီမှာဖတ်နိုင်ပါတယ်။
PEP 8 Details နောက် Sublime Text အတွက် PEP 8 Linter plugin ရှိပါတယ်
သူက code ရေးရင်နဲ့ PEP 8 rules မလိုက်နဲတဲ့အပိုင်းတွေကိုဝိုင်းပြပေးပါတယ် နောက်ဘယ်ဘက်အောက်မှာအမှားကိုပြောပြပေးပါတယ်။

PyLab (c) 2016

Pylab

Developer

Keep calm and love Python

0 comments:

Post a Comment