Comment 5 แบบที่ควรหลีกเลี่ยง! ในการเขียนโปรแกรม
Code ไม่ควรมี Comment
การที่มี Comment แปลว่าเราอ่าน Code ไม่ออก
หรือไม่สามารถเขียน Code ให้อ่านออกได้
public class Program { static void Main(string[] args) { string message = "Hello World!"; // 07/24/2010 Bas Console.WriteLine(message); // 07/24/2010 Bas message = "I am so proud of this code!"; // 07/24/2010 Bas Console.WriteLine(message); // 07/24/2010 Bas } }
@roofimon ได้กล่าวไว้ว่า "Programmer คือสิ่งมีชีวิตที่คิดว่าตัวเองฉลาดที่สุดในโลก"
แน่นอนว่ามีโปรแกรมเมอร์ประเภทที่ Proud สิ่งที่ตัวเองเขียน (มากเกินไปหน่อย) ฮ่าฮ่า
ซึ่งทำให้ Code เลอะเทอะอย่างที่เห็นนั่นแหละ
2. The Obsolete Programmer
public class Program { static void Main(string[] args) { /* This block of code is no longer needed * because we found out that Y2K was a hoax * and our systems did not roll over to 1/1/1900 */ //DateTime today = DateTime.Today; //if (today == new DateTime(1900, 1, 1)) //{ // today = today.AddYears(100); // string message = "The date has been fixed for Y2K."; // Console.WriteLine(message); //} } }
Comment แบบนี้มักจะเจอใน Programmer พวกที่ไม่ค่อยกล้าลบ กลัวจะกระทบ กระทบนี่บ้างล่ะ
แต่ส่วนใหญ่มักจะเจอในพวกที่ไม่รู้ว่าตัวเองเขียนอะไรลงไป และไม่รู้ว่าถ้าต้องกลับมาใช้ จะเขียนขึ้นมาใหม่ได้ไหม
(เชิงเดียวกับพวกที่พูดเร็วๆ แล้วบอกมันว่า "ไหนพูดอีกรอบซิ") แล้วก็ทิ้ง Comment ไว้เลอะเทอะอย่างที่เห็น ถ้าไม่อยากเป็นแบบนี้ Version Control อาจจะเป็นทางออกนะ :)
3. The Obvious Programmer
public class Program { static void Main(string[] args) { /* This is a for loop that prints the * words "I Rule!" to the console screen * 1 million times, each on its own line. It * accomplishes this by starting at 0 and * incrementing by 1. If the value of the * counter equals 1 million the for loop * stops executing.*/ for (int i = 0; i < 1000000; i++) { Console.WriteLine("I Rule!"); } } }Comment แบบนี้ดูจะเป้นคนที่ชัดเจนมาก แต่อะไรที่มันชัดเจนอยู่แล้ว ก็ไม่เข้าใจว่าจะทำไปเพื่ออะไร เหมือนกำลังอธิบายว่า "ไข่" รูปร่างหน้าตาเป็นอะไรให้ "ไก่" ฟัง :P ผมเจอประจำสมัยเป็ยพี่คุม Lab นะ เพราะเด็กๆ มักจะไม่คิดกันว่าตัวเองเขียนอะไร แล้วจะ Comment อะไรแบบนี้ไว้ เช่น "ประกาศตัวแปร a แล้วกำหนดให้ชี้ไปที่ 20"
4. The Life Story Programmer
public class Program { static void Main(string[] args) { /* I discussed with Jim from Sales over coffee * at the Starbucks on main street one day and he * told me that Sales Reps receive commission * based upon the following structure. * Friday: 25% * Wednesday: 15% * All Other Days: 5% * Did I mention that I ordered the Caramel Latte with * a double shot of Espresso? */ double price = 5.00; double commissionRate; double commission; if (DateTime.Today.DayOfWeek == DayOfWeek.Friday) { commissionRate = .25; } else if (DateTime.Today.DayOfWeek == DayOfWeek.Wednesday) { commissionRate = .15; } else { commissionRate = .05; } commission = price * commissionRate; } }Programmer คนนี้น่าจะไปเป็นนักเขียนเนอะ ... Comment แบบนี้ไม่ควรอยู่ใน Code ครั้งที่แรกที่ผมอ่าน ถึงกับขำออกมาทีเดียว อ่านแล้วมันก็อารมณ์ดีและน่าชื่นชมเหมือนกัน แต่ถ้าผมให้ Programmer คนนี้อธิบาย Code ที่เขาเขียนแล้วเขาเล่าได้แบบนี้ ผมว่าน่าสนุกดีทีเดียว แต่มันไม่ควรอยู่ใน Code ล่ะนะ นี่มัน Acceptance Criteria ชัดๆ
5. The Someday Programmer
public class Program { static void Main(string[] args) { //TODO: I need to fix this someday – 07/24/1995 Bob /* I know this error message is hard coded and * I am relying on a Contains function, but * someday I will make this code print a * meaningful error message and exit gracefully. * I just don’t have the time right now. */ string message = "An error has occurred"; if(message.Contains("error")) { throw new Exception(message); } } }
ผมก็เคยทำบ้างนะ แม้จะไม่อธิบายดราม่าขนาดนี้ เช่นเขียนการเรียก Function ไว้ก่อนแล้วมานั่ง Implement ทีหลัง
ประเด็นคือ ถ้าลืมกลับมาเขียนล่ะก็น่าจะดราม่ายกกำลัง 2 กันเลยทีเดียว พอลืมเขียนก็บอกไว้คงจะมาเขียนสักวัน (เมื่อไหร่ล่ะ ? = =')
ทิ้งท้ายไว้หน่อยว่าการเขียน Comment เอาเข้าจริงแล้ว ถ้าไม่ได้เขียน API ให้ใครอ่าน ก็ไม่จำเป็นนะ คือถ้าจะต้องทำ API ที่มีขนาดพอสมควร เขียนเป็น Documentation อธิบายไปเลยดีกว่า ส่วน Code น่ะหรอ ทำได้ไหม ให้ Code มัน Comment ตัวเองน่ะ ? หรือที่เขาเรียกว่า Self-Documented Code ส่วนใหญ่มักพบได้ในภาษาที่สวยอยู่แล้วอย่างเช่น Ruby หรือภาษาที่บังคับให้สวยเช่น Objective-C
ลองดูนะ ชื่อตัวแปร ชื่อฟังก์ชั่นยาวๆ ไม่ใช่เรื่องน่ากลัว ถ้ามันทำให้งานของเราราบรื่นขึ้น :)
Credit Quote ด้านบนจาก @nuboat
Comments
Post a Comment