← Back to context

Comment by sam_lowry_

14 hours ago

Here are a couple counter-points that come to mind:

Linus explaining taste in code: https://www.youtube.com/watch?v=78Y17hAo96I

Fast inverse square root https://en.wikipedia.org/wiki/Fast_inverse_square_root

Being a humble rank-and-file programmer I did produce pieces of code that I will be proud forever as well, e.g. this snippet to control the length of $PWD in bash that I still use almost unchanged 25 years later:

    #   How many characters of the $PWD should be kept
    local pwdmaxlen=30
    #   Indicator that there has been directory truncation:
    #trunc_symbol="<"
    local trunc_symbol="..."
    if [ ${#PWD} -gt $pwdmaxlen ]
    then
    local pwdoffset=$(( ${#PWD} - $pwdmaxlen ))
     newPWD="${trunc_symbol}${PWD:$pwdoffset:$pwdmaxlen}"
    else
     newPWD=${PWD}
    fi

Not to rain on your fine snippet but you can use negative offsets to index from the end of the string:

  P="$PWD"
  [ ${#P} -gt $pwdmaxlen ] && P="$trunc_symbol${P: -$pwdmaxlen}"