Identifiers

By Nuttanicha Wengwilardpaisarn 650710076

Identifiers คือ?

Identifiers หรือเรียกอีกอย่างว่า Names คือ สตริงที่ใช้ในการระบุสิ่งต่าง ๆ (entity) ให้กับโปรแกรม เช่น ชื่อคลาส ชื่อตัวแปร หรือชื่อฟังก์ชัน เป็นต้น


Identifier Rules

Identifier Rules ในแต่ละภาษาโปรแกรมก็จะมีกฎในการตั้งชื่อที่หลักการคล้ายกัน แต่รายละเอียดแตกต่างกัน โดยภาษา C# ต้องเป็นไปตามกฎดังต่อไปนี้

  • ตัวอักษร (characters) ที่สามารถใช้ได้ คือ ตัวอักษรและตัวเลขทั้งหมด [(A-Z), (a-z), (0-9)] รวมไปถึงสัญลักษณ์พิเศษ '_' (underscore)

ตัวอย่าง

MAX_SIZE

myBirthday

_tempToday

total_price

  • ต้องไม่ขึ้นต้นด้วยตัวเลข ([0-9])

ตัวอย่าง

Score12

A1B2C3

  • ต้องไม่มีช่องว่าง (white spaces)

ตัวอย่าง

My Birthday (ไม่ถูกต้อง เพราะมีช่องว่าง)

MyBirthday (ถูกต้อง เพราะไม่มีช่องว่าง)

  • ใช้คำสงวน (reserved words) หรือคำสำคัญ (keywords) ไม่ได้ นอกจากจะใช้สัญลักษณ์ '@' นำหน้า

ตัวอย่าง

as (ไม่ถูกต้อง เพราะเป็น keyword)

@as (ถูกต้อง เพราะใช้ '@' นำหน้า keyword )

  • อนุญาตให้ใช้ตัวอักษร Unicode ได้

ตัวอย่าง

ผู้ใช้

ค่า_Pi

คำนวณคะแนนสอบ()

  • ต้องระมัดระวังในการใช้ตัวพิมพ์เล็ก, ตัวพิมพ์ใหญ่ (Case Sensitive)

ตัวอย่าง

Variable

variable

ถือว่าเป็นตัวแปรคนละตัวกัน

  • ต้องมีความยาวไม่เกิน 512 ตัวอักษร

  • ห้ามมีสัญลักษณ์ '_' (underscore) ติดกัน 2 ตัว

ตัวอย่าง

__intPrice (ไม่ถูกต้อง เพราะมีการใช้สัญลักษณ์ '_' ติดกัน 2 ตัว)

intPrice (ถูกต้อง เพราะมีสัญลักษณ์ '' แค่ 1 ตัว)


Naming conventions (การตั้งชื่อ)

นอกจาก Identifiers Rules แล้ว ยังมี Naming conventions ที่ใช้ API ใน .NET ด้วย โดยทั่วไปในภาษา C# จะใช้ PascalCase สำหรับ type names, namespaces, public members ทั้งหมด และ ทีม dotnet/docs ใช้กฎการตั้งชื่อดังต่อไปนี้ที่ได้รับการยอมรับจาก ทีมการเขียนโค้ดของ .NET Runtime

  • ชื่อของ Interface ต้องเริ่มต้นด้วยตัว I พิมพ์ใหญ่

public interface IShape {
    double Area();
    double Perimeter();
}
  • ชนิดของ Attribute จะต้องตามด้วย Attribute ด้านหลังด้วย

[AttributeUsage(AttributeTargets.Class)]
public class DocumentationAttribute : Attribute {
    public string Description { get; }

    public DocumentationAttribute(string description) {
        Description = description;
    }
}
  • ชนิดของ Enum

non-flags คือ ต้องใช้ collection เพื่อเก็บค่า เพราะไม่รองรับการเก็บค่าหลายค่าในตัวแปรเดียว ทำให้ต้องใช้หน่วยความจำมากขึ้น

using System;

public enum DayOfWeek {
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
}

flags คือ รวมค่าหลายค่าในตัวแปรเดียว โดยการใช้ Bitwise เช่น AND, OR, NOT

[Flags]
public enum FileAccess {
    Read = 1,       // 0001
    Write = 2,      // 0010
    Execute = 4     // 0100
}

public class Program {
    public static void Main(string[] args) {
        // Bitwise
        FileAccess access = FileAccess.Read | FileAccess.Write;
        ...
    }
}
  • Identifier ห้ามมีสัญลักษณ์ '_' (underscore) ติดกัน 2 ตัว

public int myVariable;
  • ใช้ชื่อที่มีความหมาย หรือสื่อความหมายชัดเจน สำหรับตัวแปร, เมธอด และคลาส

public class Car {

    public string Model { get; set; }
    public int Year { get; set; }
}
  • ควรใช้ชื่อที่ชัดเจนมากกว่าชื่อที่กระชับแต่ไม่ชัดเจน

public void CalculateTotalPrice(decimal price, int quantity) {

    ...
}
  • ใช้ PascalCase สำหรับชื่อคลาส และชื่อเมธอด

PascalCase คือ การตั้งชื่อให้ตัวอักษรตัวแรกของแต่ละคำเป็นตัวพิมพ์ใหญ่

public class Customer {

    public void PlaceOrder() {
        ...
    }
}
  • ใช้ camelCase สำหรับพารามิเตอร์

camelCase คือ การตั้งชื่อให้ตัวอักษรตัวแรกของคำแรกเป็นตัวพิมพ์เล็ก และตัวอักษรตัวแรกของคำถัดไปเป็นตัวพิมพ์ใหญ่

public void CalculateTotal(decimal unitPrice, int quantityInCart) {

    ...
}
  • ใช้ PascalCase สำหรับชื่อคงที่ และค่าคงที่

public const double Pi = 3.14159;
  • ฟิลด์แบบ Private ต้องเริ่มต้นด้วยสัญลักษณ์ '_' (underscore)

public class Person {
    private string _name;
    private int _age;

    public string Name {
        get { return _name; }
        set { _name = value; }
    }
}
  • ฟิลด์แบบ Static ต้องเริ่มด้วย s_

public class ExampleClass {

    private static int s_instanceCount;
}
  • ไม่ควรใช้ตัวย่อหรืออักษรย่อในชื่อ ให้ใช้ชื่อที่สื่อความหมายชัดเจน นอกจากจะเป็นคำย่อที่ได้รับการยอมรับเป็นวงกว้างหรือรู้จักกันโดยทั่วไป

public void CalculateInterestRate() {

    ...
}
  • การใช้ namespces ที่มีความหมายชัดเจน โดยใช้ reverse domain name notation สำหรับจัดระเบียบโค้ด

namespace MyCompany.Project.Module {

    public class MyClass {
        ...
    }
}
  • เลือกชื่อ Assembly ที่แสดงให้เห็นถึงวัตุประสงค์หลัก

Assembly Name: MyCompany.Project
  • ไม่ควรตั้งชื่อที่มีตัวอักษรเพียงตัวเดียว ให้ใช้ชื่อที่ชัดเจน

public void ProcessItem(string itemId) {

    ...
}

ยกเว้นในกรณีของ loop ง่าย ๆ

public class Program {
    public static void Main() {
    
        Student s = new Student { Name = "John", Age = 20 };
        ...
    }
}

เปรียบเทียบ Identifiers ในภาษา C#, Java, C, Python

  • Identifiers Rules

คุณสมบัติ
C#
Java
C
Python

เริ่มต้นชื่อ

สามารถใช้ตัวอักษร (A-Z, a-z) หรือสัญลักษณ์ '_' , '$’

สามารถใช้ตัวอักษร (A-Z, a-z) หรือสัญลักษณ์ '_' , '$

สามารถใช้ตัวอักษร (A-Z, a-z) หรือสัญลักษณ์ '_'

สามารถใช้ตัวอักษร (A-Z, a-z) หรือสัญลักษณ์ '_’

ตัวเลขนำหน้า

ตามด้วยอักษร

ตามด้วยตัวเลข (0-9)

ใช้สัญลักษณ์ ‘_’

ใช้ได้ทั้งนำหน้าและตาม

ใช้ได้ทั้งนำหน้าและตาม

ใช้ได้ทั้งนำหน้าและตาม

ใช้ได้ทั้งนำหน้าและตาม

ใช้สัญลักษณ์ ‘$’

ใช้ได้ทั้งนำหน้าและตาม (แต่ไม่แนะนำ)

ใช้ได้ทั้งนำหน้าและตาม (แต่ไม่แนะนำ)

การแยกตัวพิมพ์ใหญ่, ตัวพิมพ์เล็ก

✅ (case-sensitive)

✅ (case-sensitive)

✅ (case-sensitive)

✅ (case-sensitive)

คำสำคัญ (keyword)

ใช้ไม่ได้

ใช้ไม่ได้

ใช้ไม่ได้

ใช้ไม่ได้

ความยาว

ไม่จำกัด

ไม่จำกัด

ไม่จำกัด (แต่ไม่ควรยาวเกินไป)

ไม่จำกัด

รูปแบบการตั้งชื่อ

camelCase/

PascalCase

camelCase

underscore

snake_case

ตัวอย่าง code

  • C#

using System;

class MyClass {
    static void Main(string[] args) {
        int myVariable = 10;
        int AnotherVar = 20;
        int _myVar = 30;
        Console.WriteLine(myVariable + AnotherVar + _myVar);
    }
}

มีการใช้ camelCase, PascalCase ในชื่อ และใช้สัญลักษณ์ ‘_’ นำหน้าชื่อ

  • Java

public class MyClass {
    public static void main(String[] args) {
        int myVariable = 10;
        int $anotherVar = 20;
        int _myVar = 30;
        System.out.println(myVariable + $anotherVar + _myVar);
    }
}

มีการใช้ camelCase ในชื่อ และใช้สัญลักษณ์ ‘$’ , ‘_’ นำหน้าชื่อ

  • C

#include <stdio.h>

int main() {
    int my_variable = 10;
    int _anotherVar = 20;
    printf("%d\n", my_variable + _anotherVar);
    return 0;
}

มีการใช้สัญลักษณ์ ‘_’ ในชื่อ และนำหน้าชื่อ

  • Python

def my_function():
    my_variable = 10
    _another_var = 20
    print(my_variable + _another_var)

my_function()

มีการใช้ snake_case และใช้สัญลักษณ์ ‘_’ ในชื่อและนำหน้าชื่อ


Slide

Clip


แหล่งอ้างอิง

C#

Java

C

Python

Last updated