Skip to content

Instantly share code, notes, and snippets.

@martinsson
martinsson / create_connection.go
Last active October 4, 2020 22:36
Error handling in go 3/3: the if statements have gone away. Finally!
tenantID, err1 := store.GetParameter("TENANT_ID")
clientID, err2 := store.GetParameter("CLIENT_ID")
clientSecret, err3 := store.GetParameter("CLIENT_SECRET")
globalErr := multierr.Combine(err1, err2, err3)
return connection{
tenantID,
clientID,
clientSecret,
}, globalErr
@martinsson
martinsson / create_connection.go
Last active October 4, 2020 22:36
Error handling in GO 2/3 : A better version of the error handling. No more if statement per variable
tenantID, err1 := store.GetParameter("TENANT_ID")
clientID, err2 := store.GetParameter("CLIENT_ID")
clientSecret, err3 := store.GetParameter("CLIENT_SECRET")
globalErr := multierr.Combine(err1, err2, err3)
if globalErr != nil {
fmt.Print(globalErr)
return nil, globalErr
}
@martinsson
martinsson / create_connection.go
Last active October 4, 2020 22:35
Error handling trick in GoLang 1/3 : CombineErrors
func createConnection(store Store) (Connection, error) {
tenantID, err := store.GetParameter("TENANT_ID")
if err != nil {
fmt.Print("Unable to get TENANT_ID from parameter store")
return nil, err
}
clientID, err := store.GetParameter("CLIENT_ID")
if err != nil {
fmt.Print("Unable to get CLIENT_ID from parameter store")
@martinsson
martinsson / TestableDependency.java
Last active January 3, 2019 15:15
Using a instance method as a proxy to the real method we can fake it to avoid calling the real method
// this method is fakable
public String getTranslation(String key, Locale locale) {
return getTranslationOld(key, locale);
}
/**
* @deprecated use instance method instead
*/
public static String getTranslationOld(String key, Locale locale) {
Object keys = getTranslationMap().get(locale.toString());
@martinsson
martinsson / LazyInitialiserDependency.java
Last active January 3, 2019 15:11
The static initialiser has been replace by a lazy initialiser, allowing for loading the class without provoking a query to the database.
public class LazyInitialiserDependency {
private static Map<String, Object> translationMap;
// no more static initaliser, the class is mockable, though we'll have to go further by
// adding a version of getTranslation as an instance method if we want to mock it
// this static method is called by the class you'd like to test
public static String getTranslation(String key, Locale locale) {
@martinsson
martinsson / UntestableDependency.java
Last active January 3, 2019 15:12
Full code of an offending class using a database query in it's static initialiser
public class UnTestableDependency {
private static Map<String, Object> translationMap;
static {
translationMap = HibernateUtil.executeQuery("Select ...");
}
// this static method is called by the class you'd like to test
@martinsson
martinsson / UntestableDependency.java
Created January 3, 2019 14:56
Untestable static initialiser
private static Map<String, Object> translationMap;
static {
translationMap = HibernateUtil.executeQuery("Select ...");
}
@martinsson
martinsson / fizzbuzz.idr
Created July 3, 2018 22:18
FizzBuzz as an alias for a tuple
||| the type Fizzbuzz is just an alias for the tuple of FizzT and BuzzT
Fizzbuzz: Nat -> Type
Fizzbuzz n = (FizzT n, BuzzT n)
@martinsson
martinsson / fizzbuzz.idr
Last active July 3, 2018 21:11
the whole first solution
||| proof that k is divisible by 3, can only be called when the compiler can verify it
IsFizz : (k : Nat) -> Type
IsFizz k = (modNatNZ k 3 SIsNotZ = 0) -- modNatNZ is modulos for natural numbers except for 0.
-- SIsNotZ is also a proof about k in this case. Compiler will fail if k is not greater than 0
IsBuzz : Nat -> Type
IsBuzz k = (modNatNZ k 5 SIsNotZ = 0)
||| There are four constructors : Fizz, Buzz, FizzBuzz, and Normal.
@martinsson
martinsson / fizzbuzz.idr
Last active July 3, 2018 12:50
One single constructor of initial solution
data Fizzbuzz : (k: Nat) -> Type where
Fizz : (k: Nat) -> IsFizz k -> Not (IsBuzz k) -> Fizzbuzz k