Skip to content

Instantly share code, notes, and snippets.

Avatar

TheCubicleBuddha

View GitHub Profile
@TheCubicleBuddha
TheCubicleBuddha / dontBeFlexibleIfYouHaveAnOpinion.ts
Created Apr 19, 2019
This demonstrates why it's best to clarify exactly what you want if you know what you need need. To see the solution, head over to: https://gist.github.com/TheCubicleBuddha/a72746c062b8386b33b9752d5d15d8c4
View dontBeFlexibleIfYouHaveAnOpinion.ts
function getUserById(id: any){
// hits a database and returns the value
}
const guid = "b5f770f2-197c-4bf5-a31f-cfec4f545d06";
const user = getUserById(guid);
// ^^^ Runtime Error: PL/SQL: ORA-00932: inconsistent datatypes: expected NUMBER got - TEXT
@TheCubicleBuddha
TheCubicleBuddha / dontBeFlexibleIfYouHaveAnOpinion.ts
Created Apr 19, 2019
This demonstrates why it's best to clarify exactly what you want if you know what you need need. To see the solution, head over to:
View dontBeFlexibleIfYouHaveAnOpinion.ts
function getUserById(id: any){
// hits a database and returns the value
}
const guid = "b5f770f2-197c-4bf5-a31f-cfec4f545d06";
const user = getUserById(guid);
// ^^^ Runtime Error: PL/SQL: ORA-00932: inconsistent datatypes: expected NUMBER got - TEXT
@TheCubicleBuddha
TheCubicleBuddha / lettingTheDevKnowUpFront.ts
Created Apr 19, 2019
In this example, we no longer get a runtime exception because we've communicated our needs.
View lettingTheDevKnowUpFront.ts
function getUserById(id: number){
// hits a database and returns the value
}
const guid = "b5f770f2-197c-4bf5-a31f-cfec4f545d06";
const user = getUserById(guid);
// ^ Compiler error: Argument of type string is not assignable to parameter of type 'number'.
@TheCubicleBuddha
TheCubicleBuddha / passiveAggressiveUserSaver.js
Last active Aug 30, 2019
This demonstrates code that is not "communicating it's needs." To see an example is upfront about it's requirements, check out the solution at: https://gist.github.com/TheCubicleBuddha/c03a1ac37550f8a34df08a3b03915f40
View passiveAggressiveUserSaver.js
function martyrAlgorithm(newUserToSave){
if(!newUserToSave.lastName){
throw new Error("I did my best, but I can't save this new user since you didn't get me a lastName")
}
globals.databaseHandle.save(newUserToSave);
}
@TheCubicleBuddha
TheCubicleBuddha / forthrightUserSaver.ts
Last active Apr 19, 2019
This demonstrates how you can use TypeScript to clearly communicate your needs upfront
View forthrightUserSaver.ts
import { IUserDbInstance, UserDB } from 'UserDatabase';
interface INewUser {
firstName: string,
middleName?: string,
lastName: string
}
function honestDbMethod(newUserToSave: INewUser, dbInstance: IUserDbInstance){
// if(newUserToSave.lastName){ // This if statement is unnecessary because you asked for what you need
@TheCubicleBuddha
TheCubicleBuddha / theFutureIntroducedABugInTrafficSignals.ts
Last active Apr 19, 2019
Showing how introducing a new type in the ITrafficSignal union will cause a runtime bug. And in this case, a deadly mistake.
View theFutureIntroducedABugInTrafficSignals.ts
type ITrafficLight = "red"
| "redBlinking" // <-- new case
| "green"
| "yellow"
| "yellowBlinking"; // <-- new case
// Can you spot the bug?
// You can skip ahead to the answer here: https://gist.github.com/TheCubicleBuddha/7a8854f244697b9894e41d44e1fc6967
function respondToTrafficSignal(signal: ITrafficLight): "stop" | "go" | "pause" {
@TheCubicleBuddha
TheCubicleBuddha / simpleTrafficSignals.ts
Last active Apr 19, 2019
This is the non-defensive implementation. To see a way to prevent possible errors, check out the defensive implementation: https://gist.github.com/TheCubicleBuddha/7a8854f244697b9894e41d44e1fc6967
View simpleTrafficSignals.ts
type ITrafficLight = "red" | "green" | "yellow";
// Can you see where the function below might go wrong in the future?
// You can skip ahead to the answer here: https://gist.github.com/TheCubicleBuddha/7a8854f244697b9894e41d44e1fc6967
function respondToTrafficSignal(signal: ITrafficLight): "stop" | "go" | "pause" {
if(signal === "red"){
return "stop";
} else if(signal === "yellow"){
return "pause";
@TheCubicleBuddha
TheCubicleBuddha / exhaustiveTrafficSignals.ts
Last active Apr 19, 2019
This shows how to use "Defensive Programming" (and specifically the "exhaustive type checking" features of TypeScript) to make sure that all code handles all future possibilities.
View exhaustiveTrafficSignals.ts
type ITrafficLight = "red" | "redBlinking" | "green" | "yellow" | "yellowBlinking";
function respondToTrafficSignal(signal: ITrafficLight): "stop" | "go" | "pause" {
if(signal === "red"){
return "stop";
} else if(signal === "yellow"){
return "pause";
} else if(signal === "green") {
return "go";
} else {