Skip to content
CodingThailand's Blog
CodingThailand's Blog

by โค้ชเอก

  • Home
  • About Me
  • CodingThailand.com
CodingThailand's Blog

by โค้ชเอก

4 แนวทางการเขียนโค้ดให้อ่านง่าย ไม่เป็นภาระของตัวเอง และผู้อื่น

14/05/201614/05/2016

picjumbo.com_HNCK2364 (1)

1. อย่าใช้ตัวย่อกับตัวอักษรทุกอย่างที่เขียน ให้เขียนแบบเต็ม เช่น

ถ้าจะเขียน class เกี่ยวกับการสั่งซื้อ ก็อย่าย่อ ใช้แบบเต็มสื่อความหมายไปเลยครับ เช่น
//ไม่ควรเขียนย่อเอง
class Odr {

}
 
//ควรเขียนเป็น
class Order {

}
2. ตัวแปร ตั้งให้สื่อความหมาย
3. ตั้งชื่อ method ให้สื่อความหมายเต็มๆเลยว่ามันเอาไว้ทำอะไรกันแน่
แบบนี้ไม่ควรตั้ง
public function fetch($BillId) {

}
ตั้งแบบนี้ดีกว่า รู้เลยว่า พารามิเตอร์คืออะไร (เคลียร์)
public function fetchByBillId($id) {

}
 
[เพิ่มเติมโดย @Tan Tanangular]
ถ้าเขียนโปรแกรมในแนว OOP method คือ behavior ของ object เพื่อเป็นการบอกว่า object นี้ทำอะไรได้บ้าง ลักษณะการตั้งชื่อ ควรตั้งเป็นคำกิริยา หรือ verb ในภาษาอังกฤษครับ
เพื่อให้อ่านได้ง่าย ว่ามันทำอะไร ถ้าหากเป็น method ที่คาดหวังให้ผลลัพธ์ออกมาในเชิงตรรกะแบบ boolean true หรือ false guideline อย่างนึงคือ ตั้งชื่อด้วย is นำหน้า เช่น
ขอยกตัวอย่าง จาก code php นะครับ
class Car {
var $body_color; // Items in our shopping cart
function getBodyColor() {
....
return $this->body_color;
}
function isBroken () {
...
return false;
}
}
เวลาเรียกใช้
$car = new Car;
$car->isBroken(); // เช็คว่า ตอนนี้รถกำลังเสียหรือป่าวน้าาาาา
$car->getBodyColor() // ขอสีตัวถังรถหน่อย
4. อย่าพยายามใช้ Else มากเกินไปในการเขียนโค้ด เช่น method แบบ return หากเช็ค if ให้จบในตัวเองได้ก็ให้เช็คเลย เช่น ถ้าคิดว่ามี errors อะไรก็ให้เช็ค if แล้ว return errors ออกไปเลย ไม่ต้อง if else
ตัวอย่างที่ไม่ดี
public function store() {
if ($user->status == 'member') {
throw new Exception('คุณไม่มีสิทธิ์ใช้งาน');
} elseif ($validation->passes()) {
return back();
} else {
User->save();
return 'ok';
}
}
———————–
ตัวอย่างที่ดี
public function store() {
if ($user->status == 'member') {
throw new Exception('คุณไม่มีสิทธิ์ใช้งาน');
}
if ($validation->passes()) {
return back();
}
User->save();
return 'ok';
}

 

Views: 1176

Coding Coding

Post navigation

Next post

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Posts ล่าสุด

  • การใช้งาน Prefetching ใน Next.js
  • 14 ข้อคิด เพื่อการเป็น Developer ที่ดีขึ้น จาก “Lee Robinson”
  • เคยเจอ “TypeError: Cannot read property ‘x’ of undefined” หรือเปล่า
  • บันทึกการเขียนเว็บไซต์ใหม่ในรอบ 10 ปี ย้ายมาใช้ Next.js
  • ทำไมการจัดการ Error ใน JavaScript ถึงเป็นเรื่องท้าทาย?

Recent Comments

    หมวดหมู่

    • .NET
    • AI
    • Android
    • Angular
    • Angular 2
    • Coding
    • CSS
    • Database
    • Editor
    • Flutter
    • Git
    • HTML5
    • Ionic 2
    • Ionic 4
    • Ionic Framwork
    • JavaScript
    • Laravel
    • Laravel 5
    • Next.js
    • Node.js
    • PHP
    • PHP 7
    • Plugins
    • React
    • React Native
    • Template
    • Tools
    • TypeScript
    • UI
    • Uncategorized
    • Vue.js
    • XAMPP
    • Yii
    • คอร์สเรียน
    • แรงบันดาลใจ

    Archives

    • July 2025
    • April 2025
    • November 2024
    • October 2024
    • April 2020
    • February 2020
    • August 2019
    • September 2018
    • August 2018
    • February 2018
    • November 2017
    • October 2017
    • August 2017
    • July 2017
    • April 2017
    • October 2016
    • August 2016
    • May 2016

    Tags

    .NET android Angular Angular 2 Atom Coding Coding Standard CSS CSS 3 Datepicker Express.js extensions Git HTML HTML5 Ionic2 JavaScript Laravel5 laravel 5.5 MariaDB Material Design MySQL Node.js npm PHP PHP7 plugins PouchDB recaptcha Restful sail.js template typescript typscript XAMPP Yii2

    ผู้เยี่ยมชม

    • 0
    • 1,818,208
    • 547,574
    • 1,228
    ©2025 CodingThailand's Blog | WordPress Theme by SuperbThemes