How to extract an intention-revealing name using Vim

3 May, 2012

This method has a magic number:

   def wait_time
     @env[QUEUE_WAIT_HEADER].to_i / 1000

Let’s extract that to an intention-revealing name. We’ll type

   /1000<Enter>                           # Find the number we want to extract
   cwmilliseconds_per_second<Esc>         # Replace the number with a variable name
   O<Ctrl+A> = <Esc>p                     # Assign the replaced number to the variable

The result:

   def wait_time
     milliseconds_per_second = 1000
     @env[QUEUE_WAIT_HEADER].to_i / milliseconds_per_second

Nice vim tip for getting rid of magic numbers in code.