This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//-------------------------------------------------------------------------------------- | |
// 1. Naming - Đặt tên | |
//-------------------------------------------------------------------------------------- | |
//-------------------------------------------------------------------------------------- | |
// Tránh sử dụng tên không rõ nghĩa | |
// Bad | |
int d; | |
// Good | |
int daySinceModification; | |
//-------------------------------------------------------------------------------------- | |
// Tránh sử dụng tên gây hiểu lầm khi đọc code | |
// Bad | |
var dataFromDb = db.GetFromService().ToList(); | |
// Good | |
var listOfEmployee = _employeeService.GetEmployees().ToList(); | |
//-------------------------------------------------------------------------------------- | |
// Tránh sử dụng Hungarian Notation cho C# .NET | |
// Bad | |
int iCounter; | |
string strFullName; | |
DateTime dModifiedDate; | |
public bool IsShopOpen(string pDay, int pAmount) | |
{ | |
// some logic | |
} | |
// Good | |
int counter; | |
string fullName; | |
DateTime modifiedDate; | |
public bool IsShopOpen(string day, int amount) | |
{ | |
// some logic | |
} | |
//-------------------------------------------------------------------------------------- | |
// Nhất quán trong việc viết hoa | |
// Bad | |
const int DAYS_IN_WEEK = 7; | |
const int daysInMonth = 30; | |
var songs = new List<string> { 'Back In Black', 'Stairway to Heaven', 'Hey Jude' }; | |
var Artists = new List<string> { 'ACDC', 'Led Zeppelin', 'The Beatles' }; | |
bool EraseDatabase() {} | |
bool Restore_database() {} | |
class animal {} | |
class Alpaca {} | |
// Good | |
const int DaysInWeek = 7; | |
const int DaysInMonth = 30; | |
var songs = new List<string> { 'Back In Black', 'Stairway to Heaven', 'Hey Jude' }; | |
var artists = new List<string> { 'ACDC', 'Led Zeppelin', 'The Beatles' }; | |
bool EraseDatabase() {} | |
bool RestoreDatabase() {} | |
class Animal {} | |
class Alpaca {} | |
//-------------------------------------------------------------------------------------- | |
// Đặt tên biến có ý nghĩa | |
// Bad | |
public class Employee | |
{ | |
public Datetime sWorkDate { get; set; } // what the heck is this | |
public Datetime modTime { get; set; } // same here | |
} | |
// Good | |
public class Employee | |
{ | |
public Datetime StartWorkingDate { get; set; } | |
public Datetime ModificationTime { get; set; } | |
} | |
//-------------------------------------------------------------------------------------- | |
// Sử dụng CamelCase cho tên biến cục bộ và các params | |
// Bad | |
var employeephone; | |
public double CalculateSalary(int workingdays, int workinghours) | |
{ | |
// some logic | |
} | |
// Good | |
var employeePhone; | |
public double CalculateSalary(int workingDays, int workingHours) | |
{ | |
// some logic | |
} | |
//-------------------------------------------------------------------------------------- | |
// Viết code theo chuẩn mà các lập trình viên khác có thể dễ dàng đọc hiểu mà ko cần giải thích | |
// Good | |
public class SingleObject | |
{ | |
// create an object of SingleObject | |
private static SingleObject _instance = new SingleObject(); | |
// make the constructor private so that this class cannot be instantiated | |
private SingleObject() {} | |
// get the only object available | |
public static SingleObject GetInstance() | |
{ | |
return _instance; | |
} | |
public string ShowMessage() | |
{ | |
return "Hello World!"; | |
} | |
} | |
public static void main(String[] args) | |
{ | |
// illegal construct | |
// var object = new SingleObject(); | |
// Get the only object available | |
var singletonObject = SingleObject.GetInstance(); | |
// show the message | |
singletonObject.ShowMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment