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
//-------------------------------------------------------------------------------------- | |
// 6. Concurrency, Testing, Comments | |
//-------------------------------------------------------------------------------------- | |
//-------------------------------------------------------------------------------------- | |
// 6.1 Concurrency | |
//-------------------------------------------------------------------------------------- | |
//-------------------------------------------------------------------------------------- | |
// Tránh async void method | |
// Bad | |
private async void TestAsync() | |
{ | |
} | |
// Good | |
private async Task TestAsync() | |
{ | |
} | |
//-------------------------------------------------------------------------------------- | |
// The Async Way of Doing Things | |
// Bad | |
Task.Wait, Task.Result | |
Task.WaitAny | |
Task.WaitAll | |
Thread.Sleep | |
// Good | |
await | |
await Task.WhenAny | |
await Task.WhenAll | |
await Task.Delay | |
//-------------------------------------------------------------------------------------- | |
// 6.2 Testing | |
//-------------------------------------------------------------------------------------- | |
//-------------------------------------------------------------------------------------- | |
// Bắt buộc sử dụng AAA pattern cho từng test | |
// Bad | |
public class MakeDotNetGreatAgainTests | |
{ | |
[Fact] | |
public void HandleDateBoundaries() | |
{ | |
var date = new MyDateTime("1/1/2015"); | |
date.AddDays(30); | |
Assert.Equal("1/31/2015", date); | |
date = new MyDateTime("2/1/2016"); | |
date.AddDays(28); | |
Assert.Equal("02/29/2016", date); | |
date = new MyDateTime("2/1/2015"); | |
date.AddDays(28); | |
Assert.Equal("03/01/2015", date); | |
} | |
} | |
// Good | |
public class MakeDotNetGreatAgainTests | |
{ | |
[Fact] | |
public void Handle30DayMonths() | |
{ | |
// Arrange | |
var date = new MyDateTime("1/1/2015"); | |
// Act | |
date.AddDays(30); | |
// Assert | |
Assert.Equal("1/31/2015", date); | |
} | |
[Fact] | |
public void HandleLeapYear() | |
{ | |
// Arrange | |
var date = new MyDateTime("2/1/2016"); | |
// Act | |
date.AddDays(28); | |
// Assert | |
Assert.Equal("02/29/2016", date); | |
} | |
[Fact] | |
public void HandleNonLeapYear() | |
{ | |
// Arrange | |
var date = new MyDateTime("2/1/2015"); | |
// Act | |
date.AddDays(28); | |
// Assert | |
Assert.Equal("03/01/2015", date); | |
} | |
} | |
//-------------------------------------------------------------------------------------- | |
// 6.3 Comments | |
//-------------------------------------------------------------------------------------- | |
//-------------------------------------------------------------------------------------- | |
// Tránh các comment không cần thiết và gây nhiễu | |
// Bad | |
//////////////////////////////////////////////////////////////////////////////// | |
// Scope Model Instantiation | |
//////////////////////////////////////////////////////////////////////////////// | |
var model = new[] | |
{ | |
menu: 'foo', | |
nav: 'bar' | |
}; | |
#region Scope Model Instantiation | |
var model = { | |
menu: 'foo', | |
nav: 'bar' | |
}; | |
/** | |
* 2018-12-20: Removed monads, didn't understand them (RM) | |
* 2017-10-01: Improved using special monads (JP) | |
* 2016-02-03: Removed type-checking (LI) | |
* 2015-03-14: Added combine with type-checking (JR) | |
*/ | |
public int Combine(int a,int b) | |
{ | |
return a + b; | |
} | |
public int HashIt(string data) | |
{ | |
// The hash | |
var hash = 0; | |
// Length of string | |
var length = data.length; | |
// Loop through every character in data | |
for (var i = 0; i < length; i++) | |
{ | |
// Get character code. | |
const char = data.charCodeAt(i); | |
// Make the hash | |
hash = ((hash << 5) - hash) + char; | |
// Convert to 32-bit integer | |
hash &= hash; | |
} | |
} | |
// Good | |
var model = new[] | |
{ | |
menu: 'foo', | |
nav: 'bar' | |
}; | |
public int Combine(int a,int b) | |
{ | |
return a + b; | |
} | |
public int Hash(string data) | |
{ | |
var hash = 0; | |
var length = data.length; | |
for (var i = 0; i < length; i++) | |
{ | |
var character = data[i]; | |
// use of djb2 hash algorithm as it has a good compromise | |
// between speed and low collision with a very simple implementation | |
hash = ((hash << 5) - hash) + character; | |
hash = ConvertTo32BitInt(hash); | |
} | |
return hash; | |
} | |
private int ConvertTo32BitInt(int value) | |
{ | |
return value & value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment