Skip to content

Instantly share code, notes, and snippets.

View andrewbranch's full-sized avatar

Andrew Branch andrewbranch

View GitHub Profile
@andrewbranch
andrewbranch / EntityNotFoundException.cs
Last active August 29, 2015 13:55
Show a custom error page for a custom exception. For example, when trying to access a non-existent database record (app/people/details/9999999).
// You could put more information and functionality here if you want.
// I just needed an exception distinguishable from other exceptions.
// I implemented a "FindOrDie" method in my repository code, from which
// I throw this exception whenever a query comes up empty.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
@andrewbranch
andrewbranch / ModelWithEnum.cs
Last active August 29, 2015 13:57
Make enums available in JavaScript
// Drop ModelWithEnum.ThingTypeDictionary into a ViewModel that gets serialized to JSON
// for handy, scalable use of enums in JavaScript. For example, you can change
//
//
// if (m.ThingTypeId === 3)
//
// to
//
// if (m.ThingTypeId === model.ThingTypeDictionary.Spaceship)
@andrewbranch
andrewbranch / RadioView.swift
Last active August 29, 2015 14:11
Simple, elegant radio button view
class RadioView: UIView {
var selected: Bool = false {
didSet {
UIView.animateWithDuration(self.fadeTime) {
self.inner.alpha = self.selected ? 1 : 0
}
}
}
@andrewbranch
andrewbranch / defer-with-promise.js
Created December 19, 2014 03:20
Explore the effect of Promises on JS speed and order of execution
var trash = [];
function computationallyIntensiveStuff() {
trash.push(Math.pow(Math.pow(Math.cos(Math.sin(Math.random())), Math.random()), Math.random() * 100000));
trash.filter(function (a) { return a > Math.random(); });
}
new Promise(function (resolve, reject) {
for (var i = 0; i < 1000; i++) {
@andrewbranch
andrewbranch / keybase.md
Created March 22, 2015 00:15
Verifying my GitHub identity on keybase.io

Keybase proof

I hereby claim:

  • I am andrewbranch on github.
  • I am andrewbranch (https://keybase.io/andrewbranch) on keybase.
  • I have a public key whose fingerprint is 3F80 A965 F914 BC02 8650 E395 22CC A4B1 20C4 27D2

To claim this, I am signing this object:

@andrewbranch
andrewbranch / promise-bad.js
Created June 30, 2015 20:09
I frequently overcomplicate Promises
someAsyncMethod() {
return new Promise((resolve, reject) => {
somePromiseReturningMethod().then(x => {
resolve(doSomethingTo(x));
});
});
}
@andrewbranch
andrewbranch / sort.js
Last active September 1, 2015 01:16
Variadic sort
function getTestPeople() {
return [{
name: "Kylie",
age: 18
}, {
name: "Andrew",
age: 23
}, {
name: "Andrew",
age: 18
@andrewbranch
andrewbranch / set-seq-splitting-efficiency.js
Last active November 18, 2015 18:53
One of these is around 10x faster than the other two
// filtered is an Immutable.Seq (lazy sequence) with 307 entries
let withinCountry = filtered.filter(p => p.get('locations').some(isSameCountry(yourLocation)));
let outsideCountry1 = filtered.filter(p => !withinCountry.includes(p));
let outsideCountry2 = filtered.filter(p => !p.get('locations').some(isSameCountry(yourLocation)));
let outsideCountry3 = filtered.toSet().subtract(withinCountry);
return withinCountry.sort(someWayOfSorting)
.concat(outsideCountryN.sort(someOtherWayOfSorting))
.take(10);
@andrewbranch
andrewbranch / ActionResultExtensions.cs
Last active December 17, 2015 16:01
A nice notification pattern for MVC .NET, along with a better theme for alertify.js
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using App.Utilities;
namespace App.Helpers
{
public static class ActionResultExtensions
{
@andrewbranch
andrewbranch / ErrorsController.cs
Last active December 18, 2015 02:18
Files (or snippets of files) to set up a highly flexible custom errors system that works for jQuery AJAX and synchronous calls alike for an ASP.NET MVC project.
using System.Net;
using System.Web.Mvc;
namespace ProjectName.Controllers
{
public class ErrorController : Controller
{
public ViewResult NotFound() {
Response.StatusCode = (int)HttpStatusCode.NotFound;