Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dame tus tipos pegaso

Karumi
October 10, 2018

Dame tus tipos pegaso

Desde hace años que estamos en Karumi escribiendo código. En este tiempo hemos aprendido unas cuantas técnicas y métodos que donde a traves de los tipos reducimos los errores y hacemos el camino del desarrollo más sencillo.
Los tipos están de moda y vienen para quedarse. Enseñaremos ejemplos, código, usos y casuísticas que nos hemos encontrado en nuestro desarrollo en backend o Android y como usando la 'mítica armadura tipada' resolvemos problemas clásicos del desarrollo.
Con ejemplos de JavaScript, Python, Kotlin y Scala.

Karumi

October 10, 2018
Tweet

More Decks by Karumi

Other Decks in Technology

Transcript

  1. Testing for android & iOS. Trainings Architecture, Patterns and principles

    for Android & iOS. Mastering Git. Advanced mobile development. Testing for android & iOS. Architecture, Patterns and principles for Android & iOS. Companies For Everybody Next open: Kotlin Training 17 Oct. Testing Training 20 Nov. Kotlin Training 12 Dec.
  2. Types are not about static vs dynamic languages. It’s about

    improving by modeling and adding semantic to your domain.
  3. var foo = new Foo((resolve, reject) => { setTimeout(() =>

    { resolve('foo'); }, 300); }); foo.then((value) => { console.log(value); // expected output: "foo" }); QUESTION: WHAT IS IT?
  4. var promise1 = new Promise((resolve, reject) => { setTimeout(() =>

    { resolve('foo'); }, 300); }); promise1.then((value) => { console.log(value); // expected output: "foo" }); Promise
  5. Maybe function getId() { return getCurrentUser() .map(user =>user.id); } function

    getCurrentUser() { return Maybe.None(); //or return currentUser.some(); }
  6. Maybe function get(key) { const value = Maybe.fromNull(store.get(key)); const valueTTLInMillis

    = store.get(getItemTTLKey(key)); const valueSaveDate = store.get(getItemSaveDateKey(key)); return value.bind(v => { const now = moment(); const diff = moment.duration(now.diff(valueSaveDate)); const timeDiffInSeconds = diff.milliseconds(); if (timeDiffInSeconds < valueTTLInMillis) { const recoveredInstance = recoverOptionals(v); return Some(recoveredInstance); } else { removeValue(key); return None(); } }); } #Pedro Vicente Gomez
  7. Maybe function fetchPublications(page = 1) { return (dispatch, getState) =>

    { dispatch(fetchingPage(page)); const maybeCachedFeedPage = get(feedPageCacheKey(page)); if (maybeCachedFeedPage.isSome()) { const feedPage = maybeCachedFeedPage.val; dispatch(feedPageLoaded(feedPage)); } else { getFeedPage(page).then(result => { if (result.isRight()) { const feedPage = result.right(); set(feedPageCacheKey(page), feedPage, feedPageTTLInMillis); dispatch(feedPageLoaded(feedPage)); } else { dispatch(errorFetchingPage(result.left())); } return result; }); } }; } #Pedro Vicente Gomez
  8. Maybe function fetchPublications(page = 1) { return (dispatch, getState) =>

    { dispatch(fetchingPage(page)); const maybeCachedFeedPage = get(feedPageCacheKey(page)); if (maybeCachedFeedPage.isSome()) { const feedPage = maybeCachedFeedPage.val; dispatch(feedPageLoaded(feedPage)); } else { getFeedPage(page).then(result => { if (result.isRight()) { const feedPage = result.right(); set(feedPageCacheKey(page), feedPage, feedPageTTLInMillis); dispatch(feedPageLoaded(feedPage)); } else { dispatch(errorFetchingPage(result.left())); } return result; }); } }; } #Pedro Vicente Gomez Is it the same as if(page) ???
  9. Maybe function fetchPublications(page = 1) { return (dispatch, getState) =>

    { dispatch(fetchingPage(page)); const maybeCachedFeedPage = get(feedPageCacheKey(page)); if (maybeCachedFeedPage.isSome()) { const feedPage = maybeCachedFeedPage.val; dispatch(feedPageLoaded(feedPage)); } else { getFeedPage(page).then(result => { if (result.isRight()) { const feedPage = result.right(); set(feedPageCacheKey(page), feedPage, feedPageTTLInMillis); dispatch(feedPageLoaded(feedPage)); } else { dispatch(errorFetchingPage(result.left())); } return result; }); } }; } #Pedro Vicente Gomez
  10. New data type: Either aka Result function fetchPublications(page = 1)

    { return (dispatch, getState) => { dispatch(fetchingPage(page)); const maybeCachedFeedPage = get(feedPageCacheKey(page)); if (maybeCachedFeedPage.isSome()) { const feedPage = maybeCachedFeedPage.val; dispatch(feedPageLoaded(feedPage)); } else { getFeedPage(page).then(result => { if (result.isRight()) { const feedPage = result.right(); set(feedPageCacheKey(page), feedPage, feedPageTTLInMillis); dispatch(feedPageLoaded(feedPage)); } else { dispatch(errorFetchingPage(result.left())); } return result; }); } }; } #Pedro Vicente Gomez
  11. function getId(){ let user = getCurrentUser(); return getCurrentUser() .map(user =>user.id);

    } function getCurrentUser(): Maybe<User> { return Maybe.fromNull(currentUser) } TypeScript
  12. sealed class DomainError object class UserNotExist : DomainError() data class

    InvalidAccount(account: String) : DomainError() ——————————————————————————————————————————- fun InvalidAccount( domainError: DomainError): string = when (domainError) { is UserNotExist -> “No exist” is InvalidAccount -> “account ${domainError.account}” } Kotlin
  13. sealed class DomainError object class UserNotExist : DomainError() data class

    InvalidAccount(account: String) : DomainError() type Zero = W.`0`.T type Ten = W.`10`.T type Amount = Int Refined Interval.Closed[Zero, Ten] object Amount extends RefinedTypeOps[Amount, Int] class Counter(var amount: Amount) { def increase(): Unit = Amount.from(amount.value + 1) .foreach(v => amount = v) } #Raul Raja Code 47Deg Scala
  14. Adam Tornhill “Modelar es el vinagre de Módena de la

    ensalada, semántica es sabor que te llevas.” Pedro Gomez Technical chef
  15. Bibliography They’re the cracks! TypeScript. Monet.js Comic Time.Pedro Vicente Gomez.

    karumi blog about kotlin. Sergio Gutierrez, Toni López. Better types = fewer tests. Raul Raja Who in Their Right Mind Would Use Monads in Clojure?. muhuk blog refined. fthomas. https://www.reddit.com/r/Python/comments/6d5haz/the_story_of_adding_type_hints_to_one_of_my/ Refinement Types for TypeScript. Panagiotis Vekris, Benjamin Cosman, Ranjit Hhala