chantastic / ama

Ask me anything!
1 stars 0 forks source link

Can you wax lyrical on style in programming? #1

Closed hufort closed 4 years ago

hufort commented 4 years ago

Coming from a photography background, there is much emphasis put on an individual's style.

To what degree can a unique style be expressed in a line of code, and in your opinion, what makes for good style (or poor).

Hugs.

chantastic commented 4 years ago

My favorite book on style in programming is If Hemingway Wrote JavaScript by Angus Croll.

Code is communication. Computers communicate in beeps and boops. So, code is for humans.

Your style is formed by product domain, rate of change, used technologies, collaborators, and personal tendency toward cleverness or directness.

I code like I write — Several short sentences. My code is direct and embraces duplication. When duplication becomes more attractive to bugs than abstraction, I keep my abstractions singularly focused and composable — like language.

Functions are verbs. They should never — themselves — contain conjunctions. If a function can be described as doing this and that, this or that, or this if that, it's not composable:

// Good
doThis(with something) and doThat(with something);

// Bad
doThisAndDoThat(with something);

You can always compose the functions while leaving the individual functions composable:

function process(thing) {
  doThis(thing);
  doThat(thing);
  doOther(thing);
}

process(something)

I recommend the Hemingway book. I think you'll like it.

Links to books are Amazon affiliate links