Skip to content

Instantly share code, notes, and snippets.

@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
@martinsson
martinsson / wrap-access-in-class.ts
Created May 1, 2018 22:05
Bug generator: use of indices
let card = deckOfCardsObject.getNextCard(someStep)
@martinsson
martinsson / use-modulo.ts
Created May 1, 2018 22:03
Bug generator: Use of indices
cardIndex = cardIndex + someStep
if (cardIndex > cards.length) {
cardIndex = 0
}
let card = cards[cardIndex]
// Use of a modulo would be simpler and less error-prone.
cardIndex = (cardIndex + someIndex) % deckOfCards.length
let card = deckOfCards[cardIndex]
@martinsson
martinsson / imperative-style.ts
Created May 1, 2018 22:01
Bug generator: use of indices
let foundAnimal
for (animal in animals) {
if (animal.type === 'cat') {
foundAnimal = animal
break;
}
}
@martinsson
martinsson / higher-order-functions.ts
Created May 1, 2018 21:59
Bug generator: Use of indices
let isCat = animal => animal.type === 'cat'
let foundAnimal = animals.some(isCat)
@martinsson
martinsson / while-loop.ts
Created May 1, 2018 21:54
Bug generator, while loop
let foundAnimal
let i = 0
while (i <= animals.length && !foundAnimal) {
if (animals[i].type === 'cat') {
foundAnimal = animals[i]
}
}
@martinsson
martinsson / easyToTest-realCode.ts
Last active April 15, 2018 22:26
Hidden testable code 4
// This function needs only one test
function hardToTest() {
let data = requestExternalServer()
let lang = persistence.get(data.id))
let langToUpdate = makeLangToUpdate(lang)
return persistence.update(data.id, langToUpdate)
}
@martinsson
martinsson / hardToTest-realCode.ts
Created April 15, 2018 22:24
Hidden testable code 3
function hardToTest() {
let data = requestExternalServer() // hard to test
let lang = persistence.get(data.id)) // hard to test
// below pure transformation logic
let langToUpdate = {};
versionsLangs.map((versionLang) => {
let restPath = versionLang.entity.toRestPath();
lang.dates.langsDates.map(datePayload => {
@martinsson
martinsson / easyToTest-pseudoCode.ts
Created April 15, 2018 22:23
Hidden testable code 2
function hardToTest() {
let data = callToDependency()
let result = easyToTestPureFunction(data)
let dataOther = callToDependency2(result)
}
function easyToTestPureFunction(data) {
//
// pure logic
// pure logic
function hardToTest() {
let data = callToDependency()
//
// pure logic
// pure logic
// pure logic
//
let dataOther = callToDependency2(result)
}