title: At what point does writing, maintaining and most importantly the overhead of a function become a viable alternative to repeated code?
body: <p>I've been wondering this for a while, and thought I'd pose the question today. </p>
  
  <p>Example code:</p>
  
  <pre><code>private void createLinks(int numUsers) {
  for (int i = 1; i &lt;= numUsers; i++) {
      String userId = "ID-" + i;
      String randomId;
      // Generate an ID to link to.
      do {
          randomId = "ID-" + random.nextInt(i);
      } while (randomId.equals(iUserId));
      link(userId, randomId); //links the first argument to the second, 
                              //links are not bi-directional.
  
      // Generate 4 more ID's to link to.
      for (int j = 1; j &lt;= 4; j++) {
          do {
              randomId = "ID-" + random.nextInt(i);
          } while (randomId.equals(iUserId));
          link(userId, randomId);
          link(randomId, userId);
      }
  
      // Generate another ID to link 
      do {
          randomId = "ID-" + random.nextInt(i);
      } while (randomId.equals(iUserId));
      link(randomId, userId)
    }
  }
  </code></pre>
  
  <p><code>#createLinks</code> is invoked a lot, and the <code>do...while</code> code snippet is being repeated in the method. Does it make sense to extract these 3 lines of code out to a method called <code>generateRandomId(int i)</code> and incur the function overhead to avoid this repetition? If <code>createLinks</code> gets invoked a 100 times, <code>generateRandomId</code> would get invoked 100*6 = 600 times. </p>
  
  <p>This is more a language agnostic question rather than one specific to java, but it'd be interesting to know if some languages handle function overhead better than others. E.g. JVM does <a href="https://stackoverflow.com/a/2096382/792238">function inlining</a> to optimize function calls, which might mean that a developer need not wonder about things that I mentioned above.</p>