-2

I'm new to programming, and In very many cases, i find myself torn whether i should copy paste code to make the program run faster, or gather methods by forcing the program to go through additional loops. I'll give you an example a bit like my current case:

solution1: (copypaste code)

button1.clickevent: saveValue1Method()
button2.clickevent: saveValue2Method()
button3.clickevent: saveValue3Method()

saveValue1Method()
{
doSomethingWithValue1()
buttonlabel.text = "you clicked a button";
doThis();
}
saveValue2Method()
{
doSomethingWithValue2()
buttonlabel.text = "you clicked a button";
doThis();
}
saveValue3Method()
{
doSomethingWithValue3()
buttonlabel.text = "you clicked a button";
doThis();
}


dosomethingwithValue1()
{
mylabel.text = 1;
}

doSomethingWithValue2()
{
mylabel.text = 2;
....
}
doSomethingWithValue3()
{
mylabel.text = 3;
....
}

solution2: (more program instructions)

button1.clickevent: saveValueMethod(1)
button1.clickevent: saveValueMethod(2)
button1.clickevent: saveValueMethod(3)

saveValueMethod(int i)
{
if (i == 1)
  {
  doSomethingWithValue(1)
  }
if (i == 2)
  {
  doSomethingWithValue(2)
  }
if (i == 3)
  {
  doSomethingWithValue(3)
  }

buttonlabel.text = "you clicked a button";
doThis();

}

doSomethingWithValue(int j)
{
if (j == 1)
   {
   mylabel.text = 1;
   ...
   }
if (j == 2)
   {
    mylabel.text = 2;
    }
if  (j == 3)
    mylabel.text = 3;
    }

Keep in mind this is just a simplified example, and doesn't necessarily make sense. In many cases i would have to loop through a lot of values, not just if 1,2 or 3.

Which concept is good coding?

J. Doe
  • 55
  • 6
  • 1
    Why is this tagged with C#? It looks like straight JavaScript. – Tim Feb 10 '16 at 18:18
  • 1
    As you build more complex application, you'll find yourself that reusability is more important. The performance gain you will have is minimal if noticeable, and it's not worth having a code base that's hard to maintain and develop on. – DPac Feb 10 '16 at 18:19
  • Actually this was made completely with C# in mind. apart from the incorrect button event start which is not very rellevant. Besides it's not a 1 language specific question – J. Doe Feb 10 '16 at 18:20
  • What you think is 'a lot', is probably nothing for your computer. Until you find something that gets done, say, a few thousand times *every* second, focus on making your code as simple and clear as possible. – Cecilio Pardo Feb 10 '16 at 18:22

1 Answers1

1

In general you should follow the DRY principles (don't repeat yourself) and never copy blocks of code.

Factoring your code into common functions will make your code easier to read and maintain and is unlikely to make a significant difference in performance. It is universally considered the "right" way to code.

Even when spending serious time optimizing code for performance, it is rarely a function call that is the main cause of a slowdown and other, much more important aspects of your algorithm or task are usually a much more leveraged way to improve performance. But, please realize that in 99.99% of all code, performance of the actual bytes of code is rarely an issue. So, write your code initially for maximum clarity, reusabilty, readability and maintainability and then, only once you've proven that you actually have a performance problem somewhere and have measured exactly what is causing the problem would you consider reoptimizing your lines of code to make it run faster.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks, i'll sacrifice a little performance for it from now on! I was truly wondering about this. But i feel like it's seen as a stupid question, would you say I should delete it? Accepting answer in 30 sec. – J. Doe Feb 10 '16 at 18:30
  • 1
    @J.Doe - I don't see why you'd delete your question. If this answer answers your question, then you've gotten the answer you wanted and that's what stack overflow is for. – jfriend00 Feb 10 '16 at 18:32
  • Some Veterans on here are extremely unforgiving when the question is not on their level. – J. Doe Feb 10 '16 at 18:58
  • @J.Doe - I wouldn't say it's about the level of the question, but more about how the question is proposed. As long as you show your code, you took a reasonable try at figuring it out yourself and you ask a reasonable question and your question is on-topic for the forum, you should be just fine. But, if you're off-base on some of these, then you may get rejected. You got no downvotes or close votes with this question, so it looks like you were OK to me. You can refer to [How to Ask a Good Question](http://stackoverflow.com/help/how-to-ask) for pointers. – jfriend00 Feb 10 '16 at 19:16
  • Can you tell me how this was so terrible then? http://stackoverflow.com/questions/35162245/c-sharp-wpf-check-if-checkbox-is-checked-error – J. Doe Feb 11 '16 at 23:50
  • @J.Doe - Your original version of the question did not have the code or the question in it. That's why it got closed - it did not follow the stack overflow rules for how you post a question or what needs to be in the question. If you posted it as it now appears, I don't think it would have been closed. As I understand the timing, it was not edited to contain the content of the actual question until after it was closed. stackoverflow is picky about the form of the question. You can learn how to post a proper question and get great value from the site. – jfriend00 Feb 12 '16 at 00:04