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
public sealed class Dependency : IDisposable
{
public string SomeProperty { get; set; }
public void Dispose()
{
}
}
public sealed class Owner : IDisposable
@haacked
haacked / Async-Lambda.cs
Created January 24, 2013 00:03
Example usage of an async lambda.
Assert.Throws<SomeException>(async () => await obj.GetAsync());
@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 / 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 / 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 / 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 / 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
using System;
using System.Text.RegularExpressions;
using System.Reflection;
namespace RegexLibraryBuilder
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class RegexBuilderMain
{
@haacked
haacked / Disqus.html
Created December 10, 2013 04:01
Disqus Jekyll template include
{% comment %} Load script if disquss comments are enabled and `page.comments` is either empty (index) or set to true {% endcomment %}
{% if site.disqus_short_name and page.comments != false %}
<script type="text/javascript">
var disqus_shortname = '{{ site.disqus_short_name }}';
{% if page.comments == true %}
{% comment %} `page.comments` can be only be set to true on pages/posts, so we embed the comments here. {% endcomment %}
// var disqus_developer = 1;
var disqus_identifier = '{% if page.disqus_identifier %}{{ page.disqus_identifier}}{% else %}{{ site.url }}{{ page.url }}{% endif %}';
var disqus_url = '{{ site.url }}{{ page.url }}';
var disqus_script = 'embed.js';
@haacked
haacked / rx-pipeline-example.fs
Created January 5, 2014 22:37
Rx pipelining with F#
open System
open System.Reactive
open System.Reactive.Linq
let seq = [1; 2; 3; 4].ToObservable()
// Add curryable versions of Rx methods
let select (selector:'TSource -> 'TResult) (source:IObservable<'TSource>) =
Observable.Select(source, selector)