DEV Community

Discussion on: State-of-the-Art Shitcode Principles

Collapse
 
sergchr profile image
Serhii • Edited

Thanks, Oleksii, for the great coverage of how a developer should write good code!
From my perspective, I would add the following:

Variables naming

The shorter, the better:

const s = 1000

instead of

const MILLISECONDS_IN_ONE_SECOND = 1000

You should decrease your variables/functions names.

Comments

Write comments in your native language - totally agree!
In case you need to leave a comment about fixing something in the future, please use it like this:

// TODO: fix
// or
// fix
// or
// sửa tôi
function add (a, b) { return a - b }

Not like this:

// FIXME: this function returns an invalid result
// the problem could be in the incorrect algorithm chosen
// to calculate a result
function add (a, b) { return a - b }

One-line code

You should write a one-line code because it can save a ton of memory! Just imagine how much performance gain you reach once you rewrite your app into one-liners!

Errors

Just don't catch them, I agree with Oleksii. You will see errors if your app fails anyway, so why do you need to write additional code to catch them?

Callbacks vs. straightforward code

Use callbacks. Especially, if you have more than 5 of them.

Bonuses:

this manipulating

Don't create too many variables, just use "this" context!
Examples:

this.f = () => {
 return err("error")
}

// as you remember: use shorter names, this is bad name
const err = u => throw new Error(u)

// 1. Also bad function name
// 2. Good names for arguments
// 3. Function takes more than 2-3 arguments which is great
function calculateHash(context, cb, k, e, i, few) {
  if (this.true == context.true) return cb(false)
  if (typeof this.cb === 'function') k(cb(i))
  return md5(k(i), few + e)
}

Functions return signature

Write functions that can return multiple types and unexpected results like this:

function getFirstName(user) {
 if (typeof user === 'object') return user.firstName
 if (typeof user === 'number') return user
 if (typeof user === 'string') return { firstName: user }
 if (user.firstName && user.isReal) return function () { return user }
 if (this.user) {
   return NaN
 } else {
   return this.user + { user }
 }
}

Code linting

Just don't use tools for code linting. They only take additional place in your "package.json".
Write code as you want, especially if there is more than one developer in a team.

Unnecessary code

Don't delete the code your app doesn't use. At most, comment it.

Logic fragmentation

Don't divide a program logic into readable pieces. What if your IDE's search brokes and you will not be able to find the necessary file or function?

  • 10 000 lines of code in one file is pretty OK.
  • 1000 lines of a function body is pretty OK.
  • Dealing with many services (3rd party and internal, also, there are some helpers, database hand-written ORM and jQuery slider) in one service.js? It's OK.
Collapse
 
trekhleb profile image
Oleksii Trekhleb

Very good points! I've updated the main list 👍🏻