Vishnu's Pages

Document Your Code for The Next Developer (and You)

Ever had the feeling that when you read your own code after a couple of weeks, and you can't figure out what that code does or why you wrote that piece of code?πŸ˜•πŸ˜•

Don't worry, almost every developer has the same feeling! πŸ˜†

Again, what about someone else who's reading the code in the future? If you can't figure out your own code, how can they? πŸ€·β€β™‚οΈ

By experience, you will understand when code looks cryptic and what code will cheat on you after a few days.

So here's what I do when I think that the code can be hard to understand later (for me or a future reader):

// This code implements the LERP algorithm   
// based on this calculation <link> from Wikipedia.  
/*
  LERP algorithm

  This function will do linear interpolation based on the algorithm as
mentioned in Wikipedia: https://en.wikipedia.org/wiki/Linear_interpolation

  Example usage:
   val result = lerp(36.0, 38.0, 0.3) // returns 36.6

  Ref 1: https://stackoverflow.com/questions/4353525/floating-point-linear-interpolation
  Ref 2: https://www.johndcook.com/interpolator.html
*/
// Ref: stackoverflow.com/answer/1234