ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Comment တွေက code ကို ဖတ်မယ့်သူတွေအတွက် — တစ်လလောက်ကြာရင် ကိုယ်ကိုယ်တိုင်အပါအဝင် — ရေးထားတဲ့ မှတ်ချက်တွေပါ။ Compiler က ဘာမှ မဘာသာပြန်ခင်ကတည်းက သူတို့ကို လွှင့်ပစ်လိုက်လို့ program output ထဲ ဘယ်တော့မှ မရောက်သလို run ချိန်မှာလည်း ဘာမှ မကုန်ပါဘူး။ C မှာ နှစ်မျိုး ရှိပါတယ် — // က line အဆုံးအထိ ရှိတဲ့ မှတ်ချက်ကို စပြီး /* ... */ က line အများကြီး ဖြန့်နိုင်တဲ့ မှတ်ချက်ကို ဝိုင်းထားပါတယ်။
Comment ကို ဟင်းချက်နည်းပေါ်က sticky note လို့ မြင်ကြည့်ပါ — "ဆားလျှော့ထည့်ပါ၊ ဟင်းရည်က ငန်ပြီးသားပါ"။ ချက်နည်းက ဘာလုပ်ရမလဲ ပြောပြီး note ကတော့ ဘာကြောင့်လဲ၊ ဘာကို သတိထားရမလဲ ပြောပါတယ်။ Line တိုင်းမှာ note မလိုပါဘူး၊ ဖတ်သူ ရပ်ပြီး တွေးတောရမယ့် နေရာတွေမှာပဲ လိုပါတယ်။
Problem → Why → Solution
ပြဿနာ: code နဲ့ ငြင်းခုံနေတဲ့ comment တွေ
ဒီ price list program ကို ကြည့်ပါ။ ပထမ comment က hello print ထုတ်မယ်လို့ ပြောပေမယ့် code က Price list ကို print ထုတ်ပါတယ်။ ဒုတိယ comment က tax 5% လို့ ပြောပေမယ့် code က 1.07 နဲ့ မြှောက်ပါတယ် — ဒါက 7% ပါ။ တတိယ comment ကတော့ "နောက်အပတ်" ဖယ်ဖို့ ပြောထားပေမယ့် ဘယ်အပတ်လဲ၊ ဘယ်သူ ဖယ်ရမလဲ ဘယ်သူမှ မသိပါဘူး။
Team ထဲက တစ်ယောက်က "tax is 5%" ကို ဖတ်ပြီး ကိုယ့်ရဲ့ 7% တွက်ချက်မှုမှာ bug ရှိတယ်လို့ report လုပ်ပါတယ်၊ ဒါမှမဟုတ် comment ကို ယုံပြီး code ကို 5% လို့ "ပြင်" လိုက်ပါတယ်။ ဘယ်ဟာ မှန်လဲ code ကနေ မသိနိုင်ပါဘူး။
#include <stdio.h>
int main(void) {
// print hello
printf("Price list\n");
// tax is 5%
printf("Tea: %.2f\n", 1.50 * 1.07);
/* TODO: remove this next week */
printf("Coffee: %.2f\n", 2.00 * 1.07);
return 0;
}Comment တွေ ဘာကြောင့် လိမ်ညာလာတာလဲ
Compiler က comment တွေကို ဘယ်တော့မှ မဖတ်လို့ comment တစ်ခုက code နဲ့ မကိုက်တော့တဲ့အခါ ကိုယ့်ကို မပြောနိုင်ပါဘူး။ Comment တိုင်းက အသိပညာ တစ်ခုခုရဲ့ ဒုတိယ မိတ္တူဖြစ်ပြီး မိတ္တူတွေက တဖြည်းဖြည်း ကွဲလွဲလာပါတယ်။ Code ကို ထပ်ပြောတဲ့ comment တွေက အမြန်ဆုံး ကွဲလွဲလာပါတယ်၊ code ကို ပြင်တိုင်း comment ထဲမှာပါ ပြန်ပြင်ရလို့ပါ။ ကောင်းတဲ့ comment တွေက code မပြောနိုင်တာကိုပဲ သိမ်းထားပါတယ် — အကြောင်းရင်း၊ number တစ်ခုရဲ့ ရင်းမြစ်၊ သတိပေးချက်။
Core concept: comment တွေ ဘယ်လို ပျောက်သွားလဲ
C မှာ comment ပုံစံ နှစ်မျိုး ရှိပါတယ်။ Block comment က /* နဲ့ စပြီး line ဘယ်နှစ်ကြောင်း ကြာကြာ နောက်မှာ ပထမဆုံး တွေ့တဲ့ */ မှာ ဆုံးပါတယ်။ Line comment က // နဲ့ စပြီး line ရဲ့ အဆုံးမှာ ဆုံးပါတယ်၊ ဒါကို C99 မှာ ထည့်ခဲ့တာဖြစ်ပေမယ့် compiler တွေက အဲဒီမတိုင်ခင် ကြာကြာကတည်းက extension အနေနဲ့ လက်ခံခဲ့ပါတယ်။ String literal ထဲမှာတော့ ဘယ်ပုံစံမှ အထူးမဟုတ်ပါဘူး — printf("// not a comment\n") က slash တွေကို print ထုတ်ပါတယ်။
Comment တွေက စောစောစီးစီး ပျောက်သွားပါတယ်။ Standard သတ်မှတ်ထားတဲ့ translation phase တွေမှာ preprocessing directive တွေ မ run ခင်ကတည်းက comment တစ်ခုချင်းကို space တစ်ခုတည်းနဲ့ အစားထိုးပါတယ်၊ ဒါက compiler က type တွေ စစ်တာထက် အများကြီး စောပါတယ်။ ဒါကြောင့် int/* note */count; ကို int count; လို့ ဖတ်ပြီး comment တစ်ခုက token နှစ်ခုကို ဘယ်တော့မှ ပူးကပ်မပေးနိုင်ပါဘူး။ Directive line ထဲက comment တွေလည်း ဒီအတိုင်းပါပဲ — #define WIDTH 80 /* columns */ က WIDTH ကို 80 သက်သက်အဖြစ်ပဲ define လုပ်ပါတယ်။ Comment နဲ့ ပတ်သက်တာ ဘာမှ executable ထဲ မရောက်လို့ comment ကောင်းကောင်း ရေးထားတဲ့ program က comment မပါတဲ့ program နဲ့ အမြန်နှုန်း အတူတူပါပဲ။
"ပထမဆုံး */ မှာ ဆုံးတယ်" ဆိုတာကနေ အကျိုးဆက် နှစ်ခု ထွက်လာပါတယ်။ Block comment တွေ nest မလုပ်နိုင်ပါဘူး — /* outer /* inner */ is ... */ က အတွင်းဘက် */ မှာ ဆုံးသွားပြီး ကျန်တဲ့ text က syntax error ဖြစ်လာပါတယ်။ နောက်တစ်ခုက line splicing က ဒီထက်တောင် စောစော ဖြစ်လို့ // line ရဲ့ အဆုံးဆုံးမှာ backslash ရှိရင် နောက် line ကို comment ထဲ ဆွဲထည့်ပြီး အဲဒီ code line ကို အသံတိတ် ဖျက်ပစ်ပါတယ်။
ဒါဆို comment က ဘာပြောသင့်လဲ။ အသုံးဝင်ဆုံး comment တွေက code ဘာကြောင့် ဒီပုံစံ ဖြစ်နေလဲ ရှင်းပြတယ်၊ number တွေအတွက် unit နဲ့ ရင်းမြစ် ပေးတယ်၊ code မှီခိုနေတဲ့ ယူဆချက်တွေကို ဖော်ပြတယ်၊ ထောင်ချောက်တွေကို သတိပေးပါတယ်။ File တစ်ခုက ပုံမှန်အားဖြင့် သူ့ရည်ရွယ်ချက်ကို ဖော်ပြတဲ့ block comment နဲ့ စပါတယ်။ /* add 1 to i */ လို code ကို ထပ်ပြောတဲ့ comment တွေကတော့ သတင်းအချက်အလက် မတိုးဘဲ ဖတ်ရတဲ့အချိန်ကိုပဲ တိုးစေပါတယ်။
- Backslash-newline တွေကို အရင် ဆက်
- Comment တစ်ခုစီက space တစ်ခု ဖြစ်
- Directive တွေ run ပြီးမှ compiler
- Executable ထဲမှာ comment ခြေရာ မရှိ
Comment တစ်ခုကို ဖယ်လိုက်ရင် code ရဲ့ အဓိပ္ပာယ် ပြောင်းသွားမယ်ဆိုရင် တစ်ခုခု မှားနေပါပြီ — နေရာလွဲနေတဲ့ */ ဒါမှမဟုတ် line အဆုံးက backslash ကို စစ်ပါ။
အထွေထွေ ပုံစံ
/*
* file_name.c - what this file is for.
* Block comments can span several lines.
*/
int main(void) {
statement; // line comment: runs to the end of this line
statement; /* block comment on a single line */
result = a /* note */ + b;
return 0;
}File header အတွက် block comment ကို သုံးပြီး statement တစ်ခုရဲ့ ဘေးက မှတ်ချက်တိုလေးတွေအတွက် // ကို သုံးပါ။ Block comment ကို token နှစ်ခုကြားမှာ ထည့်လို့ရပေမယ့် ဖတ်ရခက်လို့ ခဏခဏ မသုံးပါနဲ့။
Example: comment ပုံစံတိုင်း တစ်နေရာတည်းမှာ
ဒီ program မှာ file header, line comment, line နှစ်ကြောင်း block comment, ဘာကြောင့်လဲ ရှင်းတဲ့ comment, expression ထဲက comment နှစ်ခုနဲ့ string ထဲမှာ ပါနေတဲ့ comment သင်္ကေတတွေ ပါပါတယ်။ Output ကို ကြည့်ပါ — comment တစ်ခုမှ မပေါ်ပေမယ့် string ထဲက slash တွေကတော့ ပေါ်ပါတယ်။
#include <stdio.h>
/*
* greeting.c - prints the welcome banner for the course.
* Block comments like this one work well as file headers.
*/
int main(void) {
// Print a welcome message
printf("Welcome to C!\n");
/* This block can be used
for longer notes. */
printf("Comments make code easier to read.\n");
// 80-column terminal minus a 4-column margin on each side
printf("Text width: %d\n", 80 /* columns */ - 2 * 4 /* margins */);
printf("// and /* inside quotes are just text */\n");
return 0;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Lines 3-6
* greeting.c - prints the welcome banner for the course.Block comment ထဲက file header ပါ။ Line တိုင်းရှေ့က * က အလှဆင်တာသက်သက်ဖြစ်ပြီး comment က line 6 ရဲ့ */ မှာ ဆုံးပါတယ်။
- Line 9
// Print a welcome messageLine comment ပါ။ Line အဆုံးမှာ ဆုံးလို့ အောက်က printf က ပုံမှန် code ပါ။ ပထမဆုံး program မှာ ဒီလောက် ရှင်းနေတဲ့ မှတ်ချက်က အဆင်ပြေပေမယ့် တကယ့် code မှာတော့ သတင်းအချက်အလက် ထပ်ပေးတဲ့ comment တွေကို ဦးစားပေးပါ။
- Lines 12-13
/* This block can be usedLine နှစ်ကြောင်း ဖြန့်ထားတဲ့ block comment ပါ။ */ အထိ အရာအားလုံး၊ line break တွေအပါအဝင် လျစ်လျူရှုခံရပါတယ်။
- Line 16
// 80-column terminal minus a 4-column margin on each sideဘာကြောင့်လဲ ရှင်းတဲ့ comment ပါ — နောက် line က number တွေ ဘယ်ကလာလဲ ရှင်းပြပေးပြီး ဒါကို code တစ်ခုတည်းက မပြောနိုင်ပါဘူး။
- Line 17
printf("Text width: %d\n", 80 /* columns */ - 2 * 4 /* margins */);Comment တွေက token တွေကြားမှာ ရှိလို့ရပါတယ်။ တစ်ခုချင်းကို space နဲ့ အစားထိုးလို့ compiler မြင်တဲ့ expression က 80 - 2 * 4 ဖြစ်ပြီး ရလဒ်က 72 ပါ။
- Line 18
printf("// and /* inside quotes are just text */\n");String literal ထဲမှာ // နဲ့ /* က ရိုးရိုး character တွေဖြစ်လို့ ဒီ line က သူတို့ကို print ထုတ်ပါတယ်။
Welcome to C!
Comments make code easier to read.
Text width: 72
// and /* inside quotes are just text */Code ကို ပိတ်ဖို့ /* */ အစား #if 0
Block comment တွေ ပါပြီးသား code အပိုင်းတစ်ခုကို ပိတ်ချင်တဲ့အခါ /* */ နဲ့ ဝိုင်းရင် ပထမဆုံး အတွင်းဘက် */ က ကိုယ့် comment ကို ဆုံးစေလို့ မအောင်မြင်ပါဘူး။ Preprocessor ရဲ့ #if 0 ... #endif ကတော့ ကြားထဲက line အားလုံးကို comment တွေပါမကျန် ဖယ်ရှားပေးပြီး ဘေးကင်းစွာ nest လုပ်နိုင်ပါတယ်။ အဲဒီ block ကို မလိုတော့တာနဲ့ ဖျက်ပစ်ပါ — code အဟောင်းကို version control က မှတ်ထားပေးပါတယ်။
#include <stdio.h>
int main(void) {
printf("start\n");
#if 0
printf("debug: step 1\n"); /* temporary */
printf("debug: step 2\n"); /* temporary */
#endif
printf("done\n");
return 0;
}Documentation comment များ
Project အတော်များများက function declaration အပေါ်မှာ /** နဲ့ စတဲ့ comment ကို ထားပြီး @param လို tag တွေ သုံးကြပါတယ်။ C ကိုယ်တိုင်အတွက်တော့ ဒါက ရိုးရိုး block comment ပါပဲ၊ ဒါပေမယ့် Doxygen လို tool တွေက ဒါတွေကို API documentation အဖြစ် ပြောင်းပေးပြီး editor အများစုက function ခေါ်တဲ့နေရာကို hover လုပ်တဲ့အခါ ပြပေးပါတယ်။ Header file ထဲမှာ ဒီလိုပုံစံ ရှိတတ်ပါတယ်:
/**
* Returns the price including tax.
* @param net price before tax, in kyat
* @param rate tax rate as a fraction, e.g. 0.05 for 5%
*/
double with_tax(double net, double rate);လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
C project ကြီးကြီး တစ်ခုခုကို ဖွင့်ကြည့်ရင် file တိုင်းရဲ့ ပထမ line တွေက comment ဖြစ်နေတတ်ပါတယ်။ Linux kernel က သူ့ source file တွေကို license ကို စက်က ဖတ်လို့ရတဲ့ ပုံစံနဲ့ ဖော်ပြတဲ့ SPDX-License-Identifier comment နဲ့ စပြီး project အများအပြားက header နောက်မှာ file ရဲ့ ရည်ရွယ်ချက်ကို အတိုချုပ် ဖော်ပြပါတယ်။ Tool တွေကလည်း comment တွေကို ဖတ်ပါတယ် — Doxygen က function အပေါ်က /** ... */ block တွေကို browse လုပ်လို့ရတဲ့ API documentation အဖြစ် ပြောင်းပေးပါတယ်။
Code review မှာ reviewer တွေက အကြောင်းပြချက်ကို comment တွေထဲမှာ ရှာပါတယ်။ "Sensor က တခါတလေ ဟောင်းနေတဲ့ reading ပြန်ပေးလို့ သုံးကြိမ် retry လုပ်တယ်" ဆိုတာက retry တွေကို တမင်လုပ်ထားတာဖြစ်ကြောင်း နောက်လာမယ့် maintainer ကို ပြောပြပါတယ်။ Team တွေက TODO နဲ့ FIXME marker တွေကို grep နဲ့ ရှာကြပြီး TODO တစ်ခုမှာ နာမည်၊ ရက်စွဲ ဒါမှမဟုတ် issue number ပါမှ အများကြီး ပိုအသုံးဝင်ပါတယ်။ Reviewer တွေ ငြင်းဆန်တာကတော့ comment ပိတ်ထားတဲ့ code ပါ — git က version အဟောင်းတွေကို မှတ်ထားပြီးသားမို့ comment ထဲက အသုံးမဝင်တော့တဲ့ code က ဖတ်သူကို ရှုပ်ထွေးစေရုံပါပဲ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Block comment တွေကို nest လုပ်တာ
/* outer /* inner */ still outer */ က အလုပ်မလုပ်ပါဘူး — comment က ပထမဆုံး */ မှာ ဆုံးပြီး ကျန်တာကို code အဖြစ် parse လုပ်ပါတယ်။ GCC က -Wall နဲ့ "'/*' within comment" လို့ warning ပေးပြီး "expected expression before '/' token" လို error နဲ့ ကျသွားပါတယ်။ ကြီးတဲ့ code အပိုင်းတွေကို ပိတ်ဖို့ #if 0 ကို သုံးပါ။
2. // comment ရဲ့ အဆုံးက backslash
// path is C:\temp\ လို line comment က backslash နဲ့ ဆုံးနေလို့ နောက် line ကို comment ထဲ ဆွဲထည့်ပါတယ်။ စမ်းသပ်တဲ့ program တစ်ခုမှာ နောက်က total = total + 5; ပျောက်သွားလို့ 15 အစား 10 ကို print ထုတ်ခဲ့ပြီး GCC ရဲ့ -Wall က "multi-line comment" လို့ပဲ သတိပေးခဲ့ပါတယ်။ Backslash နဲ့ ဆုံးမယ့် path တွေကို block comment ထဲ ထည့်ပါ ဒါမှမဟုတ် အဆုံးမှာ စာလုံးတစ်လုံး ထပ်ထည့်ပါ။
3. Code ကို ထပ်ပြောတဲ့ ဒါမှမဟုတ် ဆန့်ကျင်တဲ့ comment
total = total + 5; အပေါ်က // add 5 က ဖတ်သူကို ဘာမှ မပြောပါဘူး၊ နောက်ပိုင်း တစ်ယောက်ယောက်က 5 ကို 7 လို့ ပြောင်းတဲ့အခါ comment က လိမ်ညာချက် ဖြစ်သွားပါတယ်။ နာမည်တွေကို ကောင်းကောင်း ပေးပြီး အကြောင်းရင်းကို comment ရေးပါ၊ code ပြောင်းတဲ့ edit တစ်ခုတည်းမှာပဲ comment တွေကို update လုပ်ပါ ဒါမှမဟုတ် ဖျက်ပါ။
Mini challenge
Mini challenge: comment ကောင်းတွေနဲ့ ဟင်းချက်နည်း
အဆင့်သုံးဆင့်ပါတဲ့ လက်ဖက်ရည် ချက်နည်းကို print ထုတ်တဲ့ program တစ်ခု ရေးပြီး ဒီ lesson ရဲ့ comment ပုံစံတိုင်းကို သင့်တော်တဲ့ နေရာမှာ သုံးပါ။
- File ကို file နာမည်နဲ့ ရည်ရွယ်ချက်ပါတဲ့ block comment header နဲ့ စရမယ်
- အဆင့်တစ်ခုချင်းရဲ့ printf အပေါ်မှာ ဘာလဲ မဟုတ်ဘဲ ဘာကြောင့်လဲ ရှင်းတဲ့ comment ရှိရမယ်
- Steeping အချိန်ကို 3 * 60 လို့ တွက်ပြီး expression ထဲမှာ /* ... */ မှတ်ချက် တစ်ခု ထည့်ရမယ်
- Debug printf တစ်ခုကို file ထဲမှာ ထားပေမယ့် #if 0 ... #endif နဲ့ ပိတ်ထားရမယ်
// TODO: replace this line with a block comment header:
// the file name and what the program does
#include <stdio.h>
int main(void) {
// TODO: step 1 - print "1. Boil water (100 C)" and explain WHY above it
// TODO: step 2 - print the steeping time in seconds, computed as
// 3 * 60, with an inline /* ... */ note inside the expression
// TODO: step 3 - print "3. Add milk" with a why comment
// TODO: a debug printf that is disabled with #if 0 ... #endif
return 0;
}ရလဒ်: Comment တွေ ဘယ်လောက်ပါပါ output မှာ step line သုံးကြောင်းပဲ ပေါ်ရပါမယ်: 1. Boil water (100 C) 2. Steep for 180 seconds 3. Add milk
အရိပ်အမြွက် ကြည့်ရန်
"ဘာကြောင့်လဲ" comment ကို စမ်းကြည့်ဖို့ comment ကို ဖတ်ပြီး code ကို ဖုံးထားကြည့်ပါ — comment က code ကို ထပ်ပြောရုံဆိုရင် ဖျက်ပြီး အကြောင်းရင်းကို ရေးပါ။ #if 0 နဲ့ #endif ကို သူတို့ line ပေါ်မှာ သီးသန့် ထားရပါမယ်။
အဖြေ ကြည့်ရန်
/*
* tea.c - prints a three-step recipe for milk tea.
* Written as practice for C comments.
*/
#include <stdio.h>
int main(void) {
// Black tea needs fully boiling water to release its flavour.
printf("1. Boil water (100 C)\n");
// Longer steeping makes the tea bitter, so we stop at three minutes.
printf("2. Steep for %d seconds\n", 3 * 60 /* minutes to seconds */);
// Milk goes in last so it does not cool the water while steeping.
printf("3. Add milk\n");
#if 0
printf("debug: recipe printed\n"); /* enable while testing */
#endif
return 0;
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- // comment တွေက line အဆုံးအထိ ရှိပြီး /* */ comment တွေက line တွေ ဖြတ်ကျော်ပြီး ပထမဆုံး */ အထိ ရှိပါတယ်။
- Compiler မ run ခင် comment တစ်ခုချင်းက space တစ်ခု ဖြစ်သွားလို့ run ချိန်မှာ ဘာမှ မကုန်ပါဘူး။
- Block comment တွေ nest မလုပ်နိုင်ပြီး // line အဆုံးက backslash က နောက် line ကို မျိုချပါတယ် — code ပိတ်ဖို့ #if 0 ကို သုံးပါ။
- ဘာကြောင့်လဲ၊ unit နဲ့ ယူဆချက်တွေကို comment ရေးပါ၊ code ကို ထပ်ပြောရုံ comment တွေကို ဖျက်ပြီး ကျန်တာတွေကို ပြောင်းလဲမှုတိုင်းနဲ့ update လုပ်ပါ။
GNU C compiler diagnostics — GNU Project