Skip to content

Instantly share code, notes, and snippets.

@tuannguyenssu
Created July 9, 2019 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuannguyenssu/6de1b67c4822a344bd92f566e6e5b375 to your computer and use it in GitHub Desktop.
Save tuannguyenssu/6de1b67c4822a344bd92f566e6e5b375 to your computer and use it in GitHub Desktop.
//--------------------------------------------------------------------------------------
// 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