Skip to content

Instantly share code, notes, and snippets.

@tuannguyenssu
Created July 14, 2019 09:06
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/3b23fb788edc84f0e31ba051d522b8c0 to your computer and use it in GitHub Desktop.
Save tuannguyenssu/3b23fb788edc84f0e31ba051d522b8c0 to your computer and use it in GitHub Desktop.
//--------------------------------------------------------------------------------------
// 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