Kyle Coberly
kylecoberly.com
kylecoberly.com
function writeTalk(){
const learningMaterial = getBooks("cyclomaticComplexity")
let whatIKnow = [];
const { length } = learningMaterial
for (let index = 0; index < length; index++) {
whatIKnow.push(learningMaterial[index])
}
const enough = Infinity
if (whatIKnow.length < enough) {
return "w/e, don't listen to me"
} else {
return getGoodWisdom(whatIKnow)
}
}
Cyclomatic Complexity is 2
function someShitDreithPosted() {
if (firstBatch !== this) {
if (this._hasChildren) {
expirationTime = this._expirationTime = firstBatch._expirationTime;
this.render(this._children);
}
}
}
`firstBatch` isn't `this`, but has no children
`firstBatch` isn't `this`, but has children
Cyclomatic Complexity is 3
// Bad
function someShitDreithPosted() {
if (firstBatch !== this) {
if (this._hasChildren) {
expirationTime = this._expirationTime = firstBatch._expirationTime;
this.render(this._children);
}
}
}
Cyclomatic Complexity is 3
// A little better
function someShitDreithPosted() {
if (firstBatch !== this && this._hasChildren) {
expirationTime = this._expirationTime = firstBatch._expirationTime;
this.render(this._children);
}
}
if
const daysOfTheWeek = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
const announce = message => console.log(`${message}!`)
// Bad
for (let i = 0; i < daysOfTheWeek.length; i++) {
if (daysOfTheWeek[i] !== "Sat") {
if (daysOfTheWeek[i] !== "Sun") {
console.log(daysOfTheWeek[i])
}
}
}
// Better, some temp variables
const weekendDays = ["Sat", "Sun"]
const weekDays = difference(weekendDays)(daysOfTheWeek)
weekDays.forEach(announce)
// Best, no variables
flow([
difference(["Sat", "Sun"])(daysOfTheWeek),
each(announce),
])(daysOfTheWeek)
// Bad
greetDay(day) {
let greeting
switch(day) {
case "mon":
greeting = "It's Monday!"
break
case "tue"
greeting = "Beautiful Tuesday"
break
case "wed":
greeting = "Should be a great Wednesday"
break
...
}
announce(greeting)
}
// Better
const greetings = {
mon: "It's Monday!",
tue: "Beautiful Tuesday",
wed: "Should be a great Wednesday",
}
announceDay(day){
announce(greetings(day))
}
// Bad
function doubler(input) {
switch (typeof input) {
case "number":
return input + input
case "string":
return input
.split("")
.map((letter) => letter + letter)
.join("")
}
}
// Better
doublerOperations = {
number: (input) => input + input,
string: (input) => input
.split("")
.map((letter) => letter + letter)
.join(""),
}
function doubler(input) {
return doublerOperations[typeof input](input)
}
// Better
function someShitDreithPosted() {
if (firstBatch !== this && this._hasChildren) {
expirationTime = this._expirationTime = firstBatch._expirationTime;
this.render(this._children);
}
}
// Bad
function someShitDreithPosted() {
if (firstBatch !== this) {
if (this._hasChildren) {
expirationTime = this._expirationTime = firstBatch._expirationTime
this.render(this._children)
}
}
}
// Better
function someShitDreithPosted() {
if (firstBatch !== this && this._hasChildren) {
resetExpirationAndRender()
}
}
resetExpirationAndRender() {
expirationTime = this._expirationTime = firstBatch._expirationTime;
this.render(this._children);
}
// Bad
function someShitDreithPosted() {
if (firstBatch !== this && this._hasChildren) {
expirationTime = this._expirationTime = firstBatch._expirationTime;
this.render(this._children);
}
}
// Bad
function finishTalk(){
let whatIKnow = ["Not Nothing"]
const enough = Infinity
if (whatIKnow.length < enough) {
return "So what if it was short"
} else {
return getGoodWisdom(whatIKnow)
}
}
// Better
function finishTalk(){
let whatIKnow = ["Not Nothing"]
const enough = Infinity
return whatIKnow.length < enough
? "It was kind of fun though right"
: getGoodWisdom(whatIKnow)
}
kylecoberly.com