Skip to content

Instantly share code, notes, and snippets.

View haacked's full-sized avatar
🏠
Code code code

Phil Haack haacked

🏠
Code code code
View GitHub Profile
@haacked
haacked / code-review-checklist.md
Last active March 9, 2020 19:28
Code Review Checklist

General

  1. Unit tests: Review unit tests first. Unit tests are a fantastic way to grasp how code is meant to be used by others and to learn what the expected behavior is. Are there any test gaps that should be there?
  2. Method arguments" Make sure arguments to methods make sense and are validated. Mentally test boundary conditions and edge cases.
  3. Null References" (Yah yah, we know. Use F# and this goes away. We get it already.) Null references are a bitch and it’s worth looking out for them specifically.
  4. Conventions Consistency" Make sure naming, formatting, etc. follow our conventions and are consistent. I like a codebase that’s fairly consistent so you know what to expect.
  5. Disposables: Make sure disposable things are disposed. Look for usages of resources that should be disposed but are not.
  6. Security: There is a whole threat and mitigation review process that falls under this bucket. In simple terms, ask yourself how this code could be exploited. The [STRIDE Threat Mo
@haacked
haacked / csharp-conventions.md
Last active December 23, 2015 07:29
Examples of C# code conventions for http://sideeffect.kr/popularconvention/

Space vs Tab

Space

public string GetSomething()
{
    return something;
}
@haacked
haacked / Microsoft.Powershell_profile.ps1
Created August 16, 2013 17:48
My Microsoft.Powershell_profile.ps1 profile script.
$dir = (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent)
Push-Location $dir
. (Resolve-Path "$dir\vs2010.ps1")
# . "Bradwils\profile.ps1"
$env:path += ";" + (Get-Item "Env:ProgramFiles(x86)").Value + "\Git\bin"
# Set up GitHub environment
Write-Host "Setting up GitHub Environment"
@haacked
haacked / ToDictionaryBetter.cs
Last active December 18, 2015 13:49
An implementation of ToDictionary that provides information about which key had duplicate entries.
public static class EnumerableExtensions
{
public static Dictionary<TKey, TSource> ToDictionaryBetter<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
return source.GroupBy(keySelector).ToDictionary(group => group.Key, group =>
{
try
{
return group.Single();
}
@haacked
haacked / ThrowsAsync.cs
Created January 24, 2013 00:37
An async version of xUnit's Async.Throws. Use it like so: await ThrowsAsync<AuthenticationException>(async () => await obj.GetStuffAsync());
public async static Task<T> ThrowsAsync<T>(Func<Task> testCode) where T : Exception
{
try
{
await testCode();
Assert.Throws<T>(() => { }); // Use xUnit's default behavior.
}
catch (T exception)
{
return exception;
@haacked
haacked / Async-Lambda.cs
Created January 24, 2013 00:03
Example usage of an async lambda.
Assert.Throws<SomeException>(async () => await obj.GetAsync());
public sealed class Dependency : IDisposable
{
public string SomeProperty { get; set; }
public void Dispose()
{
}
}
public sealed class Owner : IDisposable
@haacked
haacked / GetLoadableTypes.cs
Created January 5, 2013 22:14
Method to return all loadable types in an assembly. See [this blog post](http://haacked.com/archive/2012/07/23/get-all-types-in-an-assembly.aspx) for details.
public static IEnumerable<Type> GetLoadableTypes(this Assembly assembly)
{
if (assembly == null) throw new ArgumentNullException("assembly");
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
return e.Types.Where(t => t != null);
@haacked
haacked / continue-after-unit-tests.cs
Created October 8, 2012 22:34
ContinueAfter Unit Tests
public class TheContinueAfterMethod
{
[Fact]
public void RunsNextItemAfterCompletion()
{
var stringSequence = new[] { "one", "two", "three" }.ToObservable();
var observed = new List<String>();
Observable.Return(123).ContinueAfter(() => stringSequence)
.Subscribe(observed.Add);
@haacked
haacked / ServiceResolverAdapter.cs
Created March 11, 2012 19:34
ServiceResolverAdapter: Allows you to use the ASP.NET MVC DependencyResolver for ASP.NET Web API
public class ServiceResolverAdapter : IDependencyResolver
{
private readonly System.Web.Mvc.IDependencyResolver dependencyResolver;
public ServiceResolverAdapter(System.Web.Mvc.IDependencyResolver dependencyResolver)
{
if (dependencyResolver == null) throw new ArgumentNullException("dependencyResolver");
this.dependencyResolver = dependencyResolver;
}