Skip to content

Instantly share code, notes, and snippets.

View anelson's full-sized avatar

Adam Nelson anelson

View GitHub Profile
@anelson
anelson / kyiv_csharp_events.cs
Created August 31, 2010 08:54
Events vs Typed Delegates vs Generic Delegates, Kyiv 2010
public class Foo {
//Event
public event EventHandler UserLoggedIn;
//Typed delegate
public delegate void UserLoggedInDelegate();
public UserLoggedInDelegate UserLoggedIn { get; set; }
//Generic delegate
public Action UserLoggedIn { get; set}
@anelson
anelson / kyiv_principle_least_surprise.cs
Created August 31, 2010 08:59
Principle of Least Surprise, Kyiv 2010
// What are the semantics of IsLoggedIn?
public int IsLoggedIn();
//Wouldn't this be better?
public bool IsLoggedIn();
@anelson
anelson / dont_repeat_yourself.cs
Created August 31, 2010 09:12
Don't Repeat Yourself, Kyiv 2010
public class UserManager {
public bool IsLoggedIn { get; private set; }
public void LogIn(string userName, string password) {
if (IsLoggedIn) {
throw new InvalidOperationException("The user is already logged in");
}
if (userName == _userName && password == _password) {
IsLoggedIn = true;
@anelson
anelson / delegate.cs
Created August 31, 2010 09:34
Delegates vs Events, Kyiv 2010
public class UserManager {
//This class allows other code to perform username/password validation. Which is the better way to do this?
public class AuthenticateUserEventArgs : EventArgs {
public string UserName { get; set; }
public string Password { get; set; }
}
// This is one way
public event EventHandler<AuthenticateUserEventArgs> AuthenticateUser;
@anelson
anelson / using_out_parameter.cs
Created August 31, 2010 09:56
Using return values, ref, and out parameters, Kyiv 2010
// When a method outputs two or more values, use output parameters and return void
public void GetUserInfo(out string userName, out string password) {
...
}
@anelson
anelson / no_naming_convention.cs
Created August 31, 2010 10:09
No naming conventions, Kyiv 2010
//What the hell is going on here? I can't read it!
public class Foo {
private string Name;
private string Password;
public string UserName { get { return Name; } set { Name = value; } }
public bool ValidateUser(string UserName, string Pass) {
string CurrentUserName = UserName;
@anelson
anelson / properties_vs_methods.cs
Created August 31, 2010 10:24
Methods vs Properties, Kyiv 2010
public class UserManager {
// This property is doing WAY too much work to be a property
public string[] Users {
get {
var usersList = new List<string>();
using (var connection = ConnectToDatabase()) {
var users = connection.GetUsersTable();
users.Rows.ForEach((row) => usersList.Add(row.UserName));
}
@anelson
anelson / 1_meaningless_exception.cs
Created August 31, 2010 10:30
Choosing the right exception types, Kyiv 2010
public class UserManager {
public void AuthenticatedUser(string userName, string password) {
if (userName != _userName) {
//WTF does 'ApplicationException' mean? It means "something didn't work"
//That's not very helpful
throw new ApplicationException("Invalid username");
}
}
}
@anelson
anelson / 1_hiding_exception.cs
Created August 31, 2010 10:45
Wrapping exceptions with InnerException, Kyiv 2010
public class UserManager {
public void Authenticate(string userName, string password) {
try {
_userDatabase.Authenticate(userName, password);
} catch (Exception e) {
// Now if authentication fails, when you catch AuthenticationFailedException
// you have no idea what actually caused the authentication to fail.
// That will make debugging, especially in the field, very hard
throw new AuthenticationFailedException();
}
// Generated on 2015-09-14 using generator-angular 0.12.1
'use strict';
// # Globbing
// for performance reasons we're only matching one level down:
// 'test/spec/{,*/}*.js'
// use this if you want to recursively match all subfolders:
// 'test/spec/**/*.js'
module.exports = function (grunt) {