- vừa được xem lúc

Series viết code sao cho cool ngầu (Phần 1)

0 0 12

Người đăng: Khoi Nguyen

Theo Viblo Asia

Mở đầu

Có một câu mà mình luôn luôn giữ nó trong đầu: "Technical solve old problems and introduces new". Trong cuộc sống cũng vậy, lạm dụng bất kỳ một điều gì đó quá nhiều luôn luôn là một điều không tốt. Không hẳn những cách viết mà mình tổng hợp trong series này chỉ để cho cool ngầu và 0-1-tác dụng, đôi khi nó còn là vấn đề về performance.

1. Multiple catch

Normal:

try
{ int val = int.Parse("number");
}
catch (Exception)
{ throw new InvalidCastException();
} try
{ List<string> contents = File.ReadAllLines("FilePath").ToList();
}
catch (Exception)
{ throw new FileNotFoundException();
}

Cool:

try
{ int val = int.Parse("number"); List<string> contents = File.ReadAllLines("FilePath").ToList();
}
catch (FormatException)
{ throw new InvalidCastException();
}
catch (FileNotFoundException)
{ throw;
}
catch (Exception)
{ throw;
}

2. Yield

Normal:

public List<Student> ReadStudentsFromFile(string fileName)
{ string[] lines = File.ReadAllLines(fileName); List<Student> result = new List<Student>(); foreach (var line in lines) { Student student = ParseTextToStudent(line); result.Add(student); } return result;
}

Cool:

public IEnumerable<Student> ReadStudentsFromFile(string fileName) { string[] lines = File.ReadAllLines(fileName); foreach (var line in lines) { Student student = ParseTextToStudent(line); yield return student; } }

3. Generic methods

Normal:

public void Swap(ref int a, ref int b)
{ var temp = a; a = b; b = temp;
} public void Swap(ref float a, ref float b)
{ var temp = a; a = b; b = temp;
} int a = 10;
int b = 11;
Swap(ref a, ref b) float c = 10.1f;
float d = 11.5f;
Swap(ref c, ref d)

Cool:

public void Swap<T>(ref T a, ref T b)
{ var temp = Activator.CreateInstance<T>(); temp = a; a = b; b = temp;
} int int1 = 10;
int int2 = 11;
Swap(ref int1, ref int2); float flt1 = 10.5f;
float flt2 = 11.5f;
Swap(ref flt1, ref flt1); var obj1 = new Student();
var obj2 = new Student();
Swap(ref obj1, ref obj2);

4. String

Normal:

// 4.1 public string SomeMethod(Student student)
{ return "Student Name is " + student.Name + ". Age is " + student.Age;
} // 4.2
string str = null;
for (int i = 0; i < NumberOfRuns; i++)
{ str += "Hello World" + i;
}

Cool:

// 4.1
public string SomeMethod(Student student)
{ return $"Student Name is {student.Name}. Age is {student.Age}";
} // 4.2
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < NumberOfRuns; i++)
{ stringBuilder.AppendLine("Hello World" + i);
}

5. Toán tử 3 ngôi

Normal:

public Student SomeMethod(Student student)
{ if (student != null) { return student; } else { return new Student() { Name = "Mukesh Murugan" }; }
}

Cool:

public Student SomeMethod(Student student)
{ return student ?? new Student() { Name = "Mukesh Murugan" };
}

Bài cũng dài rồi, các bạn đọc tiếp ở phần 2 nhé. Happy Coding !!!

Bình luận

Bài viết tương tự

- vừa được xem lúc

Các loại tham chiếu Nullable trong C# (Phần 1)

1. Giới thiệu. C# 8.0 giới thiệu kiểu tham chiếu nullable và kiểu tham chiếu non-nullable cho phép bạn đưa ra các lựa chọn quan trọng về thuộc tính cho các biến kiểu tham chiếu:.

0 0 36

- vừa được xem lúc

Command pattern qua ví dụ !

Command pattern là gì . Command pattern khá phổ biến trong C #, đặc biệt khi chúng ta muốn trì hoãn hoặc xếp hàng đợi việc thực hiện một yêu cầu hoặc khi chúng ta muốn theo dõi các hoạt động. Hơn nữa, chúng ta có thể hoàn tác tác chúng. .

0 0 178

- vừa được xem lúc

Hiểu Liskov Substitution Principle qua ví dụ !

Liskov Substitution Principle là gì . Nguyên tắc đóng mở xác đinh rằng các instance của lớp con có thể thay thế được instance lớp cha mà vẫn đảm bảo tính đúng đắn của chương trình.

0 0 24

- vừa được xem lúc

Creating custom Controls Wpf

Introduction. Wpf/winforms provides various controls like Buttons, Textbox, TextBlock, Labels etc.

0 0 41

- vừa được xem lúc

[P1] Chọn công nghệ nào để xây dựng website?

Hiện nay nhu cầu phát triển website, app tăng rất cao do xu hướng "số hóa 4.0" trong và ngoài nước.

0 0 71

- vừa được xem lúc

Kiểu dữ liệu trong C#

Dẫn nhập. Ở bài BIẾN TRONG C# chúng ta đã tìm hiểu về biến và có một thành phần không thể thiếu khi khai báo biến – Đó là kiểu dữ liệu.

0 0 24