Skip to content

Instantly share code, notes, and snippets.

@adamterlson
Last active December 4, 2015 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamterlson/d22563f2ea7b93834bdb to your computer and use it in GitHub Desktop.
Save adamterlson/d22563f2ea7b93834bdb to your computer and use it in GitHub Desktop.
English?
let user = {
teacherId: 1,
first: 'Bob',
last: 'Smith',
room: {
floor: 2,
number: 40
}
};
// Switchish
if (user.teacherId) {
// is it a teacher?
// More switchish
if (user.room) {
if (user.room.floor === 1) {
// Do stuff
} else if (user.room.floor ===2) {
// Do other stuff
}
}
} else if (user.studentId) {
// is it a student?
} else { ... }
// The point: Once I know that this object is not a student but a teacher, I go on to
// make more specific checks about the state. At no point would my initial "narrowing down"
// become invalid where I would want to "fall through" the teacher bits to see if it's a student.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment