Note that there are some explanatory texts on larger screens.

plurals
  1. POGood Coding Practices: When to Create New Functions
    primarykey
    data
    text
    <p>I have a certain function that uses the same (few, 2-5 depending on how I may change it to accommodate possible future uses) lines of code 4 times.</p> <p>I looked at <a href="https://stackoverflow.com/questions/3379499/is-it-good-practice-to-create-functions-which-use-other-functions-that-you-built">this question</a>, but it's not specific enough for me, and doesn't match the direction I'm going for.</p> <p>Here's some <strong>pseudo</strong>:</p> <pre><code>function myFunction() { if (something) { // Code line 1 // Code line 2 // Code line 3 } else if (somethingElse) { // Code line 1 // Code line 2 // Code line 3 } else if (anotherThing) { // Code line 1 // Code line 2 // Code line 3 } else if (theLastThing) { // Code line 1 // Code line 2 // Code line 3 } else { // Not previously used code } } </code></pre> <p>Those same 3 lines of code are copy/pasted (constructing the same object if any of these conditions are met). Is it a good practice to create a function that I can pass all this information to and return the necessary information when it's finished? All of these conditional statements are inside a loop that <em>could</em> run up to 1000 or so times.</p> <p>I'm not sure if the cost of preparing the <strong>stack frame</strong>(?) by jumping into another function is more costly over 1000 iterations to be worth having ~15 lines of duplicated code. Obviously function-alizing it would make it more readable, however this is very specific functionality that is not used <em>anywhere else</em>. The function I could write to eliminate the copy/paste mentality would be something like:</p> <pre><code>function myHelperFunction(someParameter, someOtherParameter) { // Code line 1 // Code line 2 // Code line 3 return usefulInformation; } </code></pre> <p>And then call the function in all those conditional statements as 1 line per conditional statement:</p> <pre><code>myHelperFunction(myPassedParameter, myOtherPassedParameter); </code></pre> <p>Essentially turning those 12 lines into 4.</p> <p>So the question - <strong>is this a good practice in general</strong>, to create a new function for a very small amount of code to save some space and readability? Or is the cost for jumping functions too impacting to be worth it? Should one <strong>always</strong> create a new function for any code that they might copy/paste in the future?</p> <p>PS - I understand that if this bit of code were to be used in different (Classes) or source files that it would be logical to turn it into a function to avoid needing to find all the locations where it was copy/pasted in order to make changes. But I'm talking more or less <em>single-file/single-Class or in-function</em> kind of a dilemma. Also, feel free to fix my tags/title if I didn't do it correctly. I'm not really sure how to title/tag this post correctly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload