publicEncrypt icon indicating copy to clipboard operation
publicEncrypt copied to clipboard

Unhealthy code style

Open chiro-hiro opened this issue 5 years ago • 0 comments

I've found you are always prevent to use for loop but for loop is better than while loop in case of determined loop steps. It's more reliable.

E.g:

function nonZero (len) {
  var out = Buffer.allocUnsafe(len)
  var i = 0
  var cache = randomBytes(len * 2)
  var cur = 0
  var num
  while (i < len) {
    if (cur === cache.length) {
      cache = randomBytes(len * 2)
      cur = 0
    }
    num = cache[cur++]
    if (num) {
      out[i++] = num
    }
  }
  return out
}

Could be:

function nonZero (len) {
  var out = Buffer.allocUnsafe(len)
  var cache = randomBytes(len * 2)
  var num
  for (  var i = 0, cur = 0; i < len; i++) {
    if (cur === cache.length) {
      cache = randomBytes(len * 2)
      cur = 0
    }
    num = cache[cur++]
    if (num) {
      out[i] = num
    }
  }
  return out
}

chiro-hiro avatar Apr 27 '19 09:04 chiro-hiro