ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Comment ဆိုတာ code ထဲမှာ လူဖတ်ဖို့ ချန်ထားတဲ့ မှတ်စုလေးတွေပါ။ Python မှာ # နောက်က စာအားလုံးကို interpreter က လုံးဝ ကျော်သွားလို့ program ရဲ့ အလုပ်ကို ဘာမှ မပြောင်းပါဘူး။ စာအုပ်ရဲ့ အနားသတ်မှာ ခဲတံနဲ့ ရေးထားတဲ့ မှတ်ချက်လိုပါပဲ — စာသားကို မပြောင်းပေမယ့် နောက်ဖတ်မယ့်သူကို ကူညီပါတယ်။
အဲဒီ နောက်ဖတ်မယ့်သူက ခြောက်လကြာရင် ကိုယ်ကိုယ်တိုင်ပဲ ဖြစ်တတ်ပါတယ်။ Code ဖတ်ရုံနဲ့ ဘာလုပ်လဲ သိနိုင်ပေမယ့် ဘာကြောင့် ဒီလို ဆုံးဖြတ်ခဲ့လဲဆိုတာကိုတော့ comment ကပဲ ပြောပြနိုင်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: code နဲ့ ဆန့်ကျင်နေတဲ့ comment
အောက်က code မှာ comment က 10% discount လို့ ပြောပေမယ့် code ကတော့ 15% လျှော့ပါတယ်။ တစ်ယောက်ယောက်က discount ကို ပြောင်းခဲ့ပြီး comment ကို update မလုပ်ခဲ့တာပါ။ နောက်ထပ် developer တစ်ယောက်က comment ကို ယုံပြီး ငွေစာရင်း report မှာ 10% လို့ ရေးမိပါတယ်။ Line 4 ရဲ့ comment ကလည်း code ကို စကားလုံးနဲ့ ထပ်ပြောနေရုံပဲမို့ အသုံးမဝင်ပါဘူး။
price = 20000
# Apply a 10% member discount
price = price * 0.85
count = count + 1 # add 1 to countဘာကြောင့် ဒီ comment တွေက အန္တရာယ်ရှိတာလဲ
Python က comment ကို မစစ်ပါဘူး၊ ဒါကြောင့် code နဲ့ comment မကိုက်တော့ရင် ဘယ် error မှ မတက်ပါဘူး။ Code ကို ထပ်ပြောတဲ့ comment တွေက ဖတ်ရမယ့် စာကို များစေပြီး code ပြောင်းတိုင်း ပြင်ရမယ့် နေရာ နှစ်နေရာ ဖြစ်စေပါတယ်။ Comment ကောင်းက code မပြောနိုင်တဲ့ အချက် — ဘာကြောင့် 0.85 လဲ၊ ဘယ်သူ ဆုံးဖြတ်ခဲ့လဲ — ကိုပဲ ရေးပါတယ်။
Core concept: comment သုံးမျိုးနဲ့ သူတို့ရဲ့ အလုပ်
Single-line comment က # နဲ့ စပြီး line ရဲ့ အဆုံးထိ သက်ရောက်ပါတယ်။ Line တစ်ကြောင်းလုံး comment ဖြစ်လို့ ရသလို code နောက်မှာ inline comment အဖြစ်လည်း ရေးလို့ ရပါတယ်၊ PEP 8 က code နဲ့ # ကြား space အနည်းဆုံး နှစ်ခု ခြားဖို့ အကြံပြုပါတယ်။ Python မှာ /* ... */ လို multi-line comment syntax သီးသန့် မရှိလို့ line အများကြီးကို comment လုပ်ချင်ရင် line တိုင်းရှေ့မှာ # ထည့်ပါ၊ editor အများစုမှာ Ctrl+/ နဲ့ တစ်ခါတည်း လုပ်လို့ ရပါတယ်။
Docstring ကတော့ comment မဟုတ်ပါဘူး — module, function ဒါမှမဟုတ် class ရဲ့ ပထမဆုံး statement အဖြစ် ရေးထားတဲ့ string literal (များသောအားဖြင့် """...""") ပါ။ Python က သူ့ကို ပစ်မပစ်ဘဲ __doc__ attribute ထဲ သိမ်းထားလို့ help(function_name)၊ editor ရဲ့ hover tooltip နဲ့ documentation tool တွေက ဖတ်ပြနိုင်ပါတယ်။ ပထမ line မှာ function က ဘာလုပ်လဲ ဝါကျတစ်ကြောင်းနဲ့ ရေးပြီး လိုရင် line လွတ်တစ်ကြောင်း ခြားကာ အသေးစိတ် ဆက်ရေးတာက ထုံးစံပါ။
သတိထားရမှာက triple-quoted string ကို code ရဲ့ အလယ်မှာ ရေးရင် docstring မဖြစ်ပါဘူး။ Python က တန်ဖိုးတစ်ခုအဖြစ် ဖတ်ပြီး ဘယ်မှာမှ မသိမ်းဘဲ ပစ်လိုက်လို့ comment လို အလုပ်လုပ်သလို ထင်ရပေမယ့် တကယ်တော့ မဟုတ်ပါဘူး။ တခြားနေရာတွေမှာ ဒါကို "multi-line comment" လို့ ခေါ်တာ တွေ့ရနိုင်ပေမယ့် PEP 8 က block comment တွေကို # နဲ့ပဲ ရေးဖို့ အကြံပြုပါတယ်။
| အမျိုးအစား | Syntax | ဘာအတွက် သုံးလဲ |
|---|---|---|
| Single-line comment | # note | နောက်က code block ဘာကြောင့် ဒီလိုလဲ ရှင်းဖို့ |
| Inline comment | code # note | Line တစ်ကြောင်းရဲ့ ထူးခြားချက်ကို ရှင်းဖို့ |
| Block comment | line တိုင်းရှေ့မှာ # | Line အများကြီးစာ ရှင်းပြချက် ဒါမှမဟုတ် code ကို ယာယီပိတ်ဖို့ |
| Docstring | """Summary.""" (ပထမ statement) | Module, function, class ကို document လုပ်ဖို့၊ help() က ဖတ်တယ် |
Comment ကောင်း ရေးဖို့ စည်းမျဉ်းက ရိုးရှင်းပါတယ် — "ဘာ" ကို code က ပြောပါစေ၊ "ဘာကြောင့်" ကို comment က ပြောပါစေ။ total_with_tax လို ရှင်းတဲ့ နာမည်နဲ့ def area(width: float) -> float လို type hint တွေက comment အများကြီးကို မလိုအပ်အောင် လုပ်ပေးပါတယ်။ TODO comment ရေးရင် ဘာလုပ်ရမလဲ၊ ဘာကြောင့်လဲကို တိတိကျကျ ရေးပါ။
Comment အချို့ကတော့ လူအတွက် မဟုတ်ဘဲ tool တွေအတွက်ပါ။ File ရဲ့ ပထမ line က #!/usr/bin/env python3 (shebang) ဆိုရင် macOS နဲ့ Linux က ./script.py လို့ တိုက်ရိုက် run တဲ့အခါ ဘယ် interpreter သုံးရမလဲ သိပါတယ်။ # noqa က linter ကို အဲဒီ line ကို မစစ်ဖို့ ပြောပြီး # type: ignore က type checker ကို ပြောပါတယ်။ Python 3 မှာ source file တွေက default အားဖြင့် UTF-8 ဖြစ်လို့ မြန်မာစာ comment တွေ ရေးဖို့ # -*- coding: utf-8 -*- line မလိုတော့ပါဘူး။
- # comment: code ဖတ်တဲ့ developer
- Docstring: function ကို သုံးမယ့်သူ
- Name နဲ့ type hint: code ကိုယ်တိုင်
- Interpreter: # တွေကို ကျော်
Docstring က program ထဲမှာ ကျန်နေတဲ့ data ဖြစ်ပြီး # comment ကတော့ source file ထဲမှာပဲ ရှိပါတယ်။
အထွေထွေ ပုံစံ: comment နဲ့ docstring
# A full-line comment explains the next block.
value = compute() # an inline comment: two spaces before #
def function_name(param):
"""One-line summary of what the function returns.
Optional details: rules, units, edge cases.
"""
...# comment ကို ရှင်းပြချင်တဲ့ code ရဲ့ အပေါ် ဒါမှမဟုတ် ဘေးမှာ ရေးပါ။ Docstring ကတော့ def line ရဲ့ အောက်မှာ ချက်ချင်း၊ body နဲ့ indent တူတူ ရေးရပါတယ်။
Example: "ဘာကြောင့်" ကို ရှင်းတဲ့ comment တွေ
ဒီ function က ပစ္စည်းပို့ခကို တွက်ပါတယ်။ Docstring က function ကို ခေါ်မယ့်သူအတွက် စည်းမျဉ်းကို ရှင်းပြပြီး # comment က math.ceil ကို ဘာကြောင့် ရွေးလဲ ရှင်းပြပါတယ်။ Disable လုပ်ထားတဲ့ line ကိုလည်း ဘာကြောင့် ပိတ်ထားလဲ ရေးထားတာကို သတိထားကြည့်ပါ။
import math
BASE_LIMIT_KG = 1.0
def shipping_fee(weight_kg: float) -> int:
"""Return the delivery fee in kyat for a parcel.
The first kilogram costs the base rate. Every started
kilogram after that adds 500 kyat, so 2.3 kg pays for 3.
"""
base = 2000
if weight_kg <= BASE_LIMIT_KG:
return base
# ceil, not round: the courier charges 1.2 kg as 2 kg
return base + (math.ceil(weight_kg) - 1) * 500
print(shipping_fee(0.8))
print(shipping_fee(2.3))
# print(shipping_fee(-1)) # disabled until negative weights are rejected
print(shipping_fee.__doc__.splitlines()[0])Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Lines 7-11
"""Return the delivery fee in kyat for a parcel.def line ရဲ့ အောက်က ပထမ statement ဖြစ်လို့ docstring ပါ။ ပထမ line က အနှစ်ချုပ်ဝါကျ ဖြစ်ပြီး ကျန်တဲ့ line တွေက စည်းမျဉ်းနဲ့ ယူနစ် (kyat, kg) ကို ရှင်းပါတယ်။
- Line 15
# ceil, not round: the courier charges 1.2 kg as 2 kgCode ကို ဖတ်ရင် math.ceil ကို သုံးတာ မြင်ရပေမယ့် round() ကို ဘာကြောင့် မသုံးလဲ မသိနိုင်ပါဘူး။ ဒီ comment က အဲဒီ ဆုံးဖြတ်ချက်ကို ရှင်းပြလို့ တစ်ယောက်ယောက်က round() နဲ့ "ပြင်" မိတာကို ကာကွယ်ပေးပါတယ်။
- Line 21
# print(shipping_fee(-1)) # disabled until negative weights are rejectedLine ရှေ့က # က code ကို ယာယီ ပိတ်ထားပါတယ်၊ နောက်က comment က ဘာကြောင့် ပိတ်ထားလဲ၊ ဘယ်အချိန် ပြန်ဖွင့်လို့ရလဲ ပြောပါတယ်။
- Line 22
print(shipping_fee.__doc__.splitlines()[0])Docstring က program run နေစဉ် __doc__ ထဲမှာ ရှိနေကြောင်း ပြပါတယ်။ splitlines()[0] က ပထမ line ကိုပဲ ယူပါတယ်။ # comment တွေကိုတော့ ဒီလို ပြန်ယူလို့ မရပါဘူး။
2000
3000
Return the delivery fee in kyat for a parcel.Comment မလိုအောင် code ကို ပြင်ရေးခြင်း
Comment ရေးဖို့ လိုလာတိုင်း "နာမည်ကောင်းကောင်း ပေးလိုက်ရင် ဒီ comment လိုသေးလား" လို့ အရင်မေးပါ။ အောက်က ဥပမာ နှစ်ခုက အလုပ်တူတူ လုပ်ပေမယ့် ဒုတိယ version မှာ ဂဏန်းရဲ့ အဓိပ္ပာယ်ကို constant နာမည်က ပြောပြီး function နာမည်က ရလဒ်ကို ပြောလို့ comment မလိုတော့ပါဘူး။
# Before: the comment carries the meaning
d = 7 * 24 * 60 # minutes in a week
print(d)
# After: the names carry the meaning
MINUTES_PER_WEEK = 7 * 24 * 60
def minutes_to_weeks(minutes: int) -> float:
return minutes / MINUTES_PER_WEEK
print(minutes_to_weeks(20160))လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Library တွေရဲ့ documentation website အများစုကို docstring တွေကနေ အလိုအလျောက် ထုတ်ပါတယ် — Sphinx လို tool တွေက function တိုင်းရဲ့ docstring ကို ဖတ်ပြီး web page ဆောက်ပေးပါတယ်။ VS Code မှာ function တစ်ခုပေါ် mouse တင်လိုက်ရင် ပေါ်လာတဲ့ ရှင်းပြချက်ကလည်း docstring ပါပဲ။ ဒါကြောင့် public function တွေမှာ docstring ရေးတာက team ထဲက တခြားသူတွေ အတွက် လက်ဆောင်တစ်ခုပါ။
Code review မှာ reviewer တွေက code ကို ထပ်ပြောတဲ့ comment ကို ဖျက်ခိုင်းပြီး ဆုံးဖြတ်ချက် ရှင်းပြချက် မပါတဲ့ ထူးဆန်းတဲ့ code ကိုတော့ comment ထည့်ခိုင်းကြပါတယ်။ Comment ပိတ်ထားတဲ့ code အဟောင်းတွေကိုလည်း ဖျက်ခိုင်းပါတယ်၊ git history ထဲမှာ ကျန်နေလို့ လိုရင် ပြန်ယူလို့ ရပါတယ်။ Bug ပြင်တဲ့အခါ ticket နံပါတ်ကို comment မှာ ထည့်ရေးတာကလည်း နောက်မှ ဘာကြောင့် ဒီလို code ဖြစ်လာလဲ ခြေရာခံဖို့ အသုံးဝင်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Code ပြောင်းပြီး comment ကို မပြင်တာ
ပြဿနာ section ရဲ့ 10% / 0.85 ဥပမာလိုပါပဲ၊ comment ဟောင်းက comment မရှိတာထက် ပိုဆိုးပါတယ်၊ လူတွေက ယုံကြလို့ပါ။ Code တစ်ကြောင်း ပြင်တိုင်း သူ့အပေါ်က comment ကို ထပ်ဖတ်ပြီး မှန်သေးလား စစ်ပါ၊ မလိုတော့ရင် ဖျက်ပါ။
2. Docstring ကို နေရာမှားမှာ ရေးတာ
Docstring ကို def line ရဲ့ အပေါ်မှာ # နဲ့ ရေးတာ ဒါမှမဟုတ် body ထဲမှာ တခြား code နောက်မှ ရေးတာက __doc__ ကို None ဖြစ်စေပါတယ်၊ help() က ဘာမှ မပြပါဘူး။ Def line ရဲ့ အောက်က ပထမဆုံး line ကို """ နဲ့ စရပါမယ်။
3. Comment ပိတ်ထားတဲ့ code တွေ စုပုံထားတာ
# x = 10 လို ပိတ်ထားတဲ့ line တွေက ဘာကြောင့် ကျန်နေလဲ ဘယ်သူမှ မသိတော့ဘဲ ဖတ်ရ ရှုပ်စေပါတယ်။ စမ်းသပ်ပြီးရင် ဖျက်ပါ၊ ယာယီ ထားဖို့ လိုရင်လည်း ဘာကြောင့်လဲ ဆိုတာ ထည့်ရေးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Public function နဲ့ class တိုင်းမှာ docstring ရေးပါ၊ ပထမ line ကို ဝါကျ အပြည့်အစုံနဲ့ full stop နဲ့ ဆုံးပါ။
- Docstring မှာ ယူနစ် (kg, kyat, seconds) နဲ့ မမှန်တဲ့ input ဝင်လာရင် ဘာဖြစ်မလဲ ဖော်ပြပါ။
- Comment ကို ရှင်းပြချင်တဲ့ code ရဲ့ အပေါ်မှာ indent တူတူနဲ့ ရေးပါ။
- TODO ရေးရင် "TODO: validate negative weights before release" လို ဘာလုပ်ရမလဲ တိတိကျကျ ရေးပါ။
Mini challenge
Mini challenge: ပျမ်းမျှ အမြန်နှုန်း function ကို document လုပ်ပါ
ခရီးအကွာအဝေး (km) နဲ့ အချိန် (မိနစ်) ကနေ ပျမ်းမျှ အမြန်နှုန်း (km/h) တွက်တဲ့ function ကို ရေးပြီး docstring နဲ့ "ဘာကြောင့်" comment တစ်ခု ထည့်ပါ။
- Docstring ရဲ့ ပထမ line က အနှစ်ချုပ် ဝါကျ ဖြစ်ရမယ်၊ ယူနစ်တွေကိုလည်း ဖော်ပြရမယ်
- မိနစ်ကို နာရီ ပြောင်းတဲ့ line မှာ ဘာကြောင့် 60 နဲ့ စားလဲ comment ရေးရမယ်
- average_speed(30, 45) ရဲ့ ရလဒ်ကို ဒသမ တစ်နေရာနဲ့ print ထုတ်ရမယ်
- Docstring ရဲ့ ပထမ line ကို __doc__ ကနေ print ထုတ်ရမယ်
def average_speed(distance_km: float, minutes: float) -> float:
# TODO: write a docstring - summary line first, mention km and minutes
# TODO: convert minutes to hours (comment: why?)
# TODO: return distance divided by hours
...
# TODO: print the speed for 30 km in 45 minutes, e.g. "40.0 km/h"
# TODO: print the first line of average_speed.__doc__ရလဒ်: ထွက်လာမယ့် output: 40.0 km/h Return the average speed in km/h for a trip.
အရိပ်အမြွက် ကြည့်ရန်
f"{value:.1f}" က ဒသမ တစ်နေရာ ပြပါတယ်။ __doc__.splitlines()[0] က docstring ရဲ့ ပထမ line ပါ။
အဖြေ ကြည့်ရန်
def average_speed(distance_km: float, minutes: float) -> float:
"""Return the average speed in km/h for a trip.
distance_km is the trip length in kilometres and minutes
is the travel time; both must be positive.
"""
# speeds are quoted per hour, but trip logs record minutes
hours = minutes / 60
return distance_km / hours
speed = average_speed(30, 45)
print(f"{speed:.1f} km/h")
print(average_speed.__doc__.splitlines()[0])Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- # နောက်က စာကို interpreter က ကျော်ပါတယ်၊ inline comment မှာ code နဲ့ # ကြား space နှစ်ခု ခြားပါ။
- Docstring က module/function/class ရဲ့ ပထမ statement ဖြစ်တဲ့ string ပါ — __doc__ နဲ့ help() က ဖတ်ပါတယ်။
- Code အလယ်က triple-quoted string က docstring မဟုတ်သလို comment syntax လည်း မဟုတ်ပါဘူး။
- Comment က "ဘာကြောင့်" ကို ရှင်းပါစေ၊ "ဘာ" ကိုတော့ နာမည်ကောင်းနဲ့ type hint တွေက ပြောပါစေ။
- Code ပြင်တိုင်း comment ကိုပါ စစ်ပါ၊ comment ဟောင်းက comment မရှိတာထက် ဆိုးပါတယ်။
The Python Tutorial — Python Software Foundation