Take the 2-minute tour ×

I program in a style that everything it's expensive or I really do hate repeating anything, mostly because I develop for embedded systems. So I get very annoyed when I have to do something that causes repetition.

An example would be that in my current project, I am creating a Layout Manager. I had this huge function that did all the work of placing everything, which I don't mind as long as I comment well. Some point down the line I realise that something I do in that long function I need to do again somewhere else, like adjusting for something that is bigger than the current. But what got me really annoyed ( and why I'm posting ) is that the needed function requires three variables. To me those three variables are expensive and should only be computed once per call. But to refactor the code the way I need to I have to put the variables in the needed function. Now the variables gets evaluated three times.

This is just an example, and things like this crop up a lot else where, so besides telling me those evaluations aren't expensive, at which point I'll say, "I'm still repeating it though", what can be done about this sort of thing, or, to my huge annoyance, is it unavoidable?

Update: One of the main suggestions given is store the value. Using the example above again, storing the value does not seem viable. Mostly because the needed function takes two needed values which are used to compute the three variables. It would mean I would have to create an object, and feel free to correct me if I'm wrong, store the values and variables into the object and then test them if they had changed.

In short, unless there are other ideas out there, it looks to be unavoidable. The only real thing you can do is minimise up to a limit. How deep do I create objects at the cost of property look ups? How many times to I repeatedly have to create those three variables? I hope you get the idea.

There is also the fact maybe I could better refactor my code. I tend to write in huge functions and only pull out what I need, which is probably KISS's fault. I think need another acronym to balance things out :P .

share|improve this question

closed as not constructive by Mark Trapp Sep 22 '11 at 18:36


"Now the variables gets evaluated three times": what about lazy evaluation? –  MainMa Sep 22 '11 at 0:38
But every time it's evaluated it is needed, thus there is no point to use lazy evaluation. –  NebulaFox Sep 22 '11 at 0:40
Why don't you just save the results of the 3 variables after the function calculates them? –  JeffO Sep 22 '11 at 0:52
You should really read that through again and remove half the text. "Remove half the words on your pages, and then remove half of what's left" (Steve Krug) ;-) –  Omar Kohl Sep 22 '11 at 13:32
Hi NebulaFox, based on your update, it appears what you're asking is rhetorical. Can you focus and revise your question so we can better understand what exactly you want people to answer? –  user8 Sep 22 '11 at 18:37
show 1 more comment

2 Answers

Are you programming for embedded systems? If not then modern hardware is at the point where quibbling over mundane details like that is likely not sensical unless of course you are working on high frequency trading where nanoseconds count.

Please remember that code is written for PEOPLE not for MACHINES. A process being a couple of milliseconds faster with code that takes a smart developer a week to understand is much less desirable than a slightly slower application with code and design that any developer can look at, figure out, add features to quickly and refactor in a pinch.

share|improve this answer
Yes, I do tend to program for embedded systems, but you do make a fair point. But... I'm still repeating it :P I warn you –  NebulaFox Sep 22 '11 at 1:19
You should include the detail about you working on embedded systems because that changes things quite a bit. I don't have much development experience in that area so I can't give you any better advice, but I am interested to see what other peoples opinions are on this... –  maple_shaft Sep 22 '11 at 1:25
Professor used to say 'make it work, now make it fast'. Good strategy unless you are programming in the high performance space. –  Chris Sep 22 '11 at 3:48
Can you save recaulculating them by making them globals, think of the performance gain of not having to pass parameters as an added bonus. –  mattnz Sep 22 '11 at 9:00
@mattnz Another pet hate of mine, a function with tons or parameters. Just got to be careful that is some languages storing into globals or objects for that manner, can cost too. I do understand in this situation you do have to strike a balance. –  NebulaFox Sep 22 '11 at 10:45
add comment

It's a good idea to not repeat yourself, it's part of the DRY principles. However, the compiler will often take care of those minor optimizations.

It's not worth getting worked up over.


It's not worth getting worked up over.

share|improve this answer
add comment
