What if, in the future, the definition of TOTAL changes to something more complex (and time consuming), or it's changed to have some side-effect on the data? It's far better to code with that in mind and not needlessly recalculate results. |
That's true. Your code is bound to change in the future, and you must think about all the changes that might occur in advance. However, you also have to remember that the extent to which it can change is limited. All programs have a specific purpose, and no program will ever change its purpose. Photoshop will always be a photo editor, Code::Blocks will always be an IDE, etc. These programs undergo changes all the time, but their purpose remains the same. And so, there are
boundaries to how much a program can change.
In this case, we know that the calculation may change and become more complicated, but (due to its purpose) will always remain a basic, arithmetic expression (It won't run loops, access a database, change data, etc). Since we know that, then we can assume that it's safe to put the expression in a function or macro. If the expression ever changes, we can go to wherever we #defined the macro or created the function and change it there.
One other thing I'd like to point out. You always want to avoid "stagnate" data. For example, let's say that we have a square class:
1 2 3 4 5
|
class Square
{
public:
unsigned int width, height;// I know this is a bad class, but it's just for demonstration purposes.
}
|
Let's also say that, at some point, we decide to add a function to calculate the area of the square. Let's also say that we use an 'area' variable to store the results so that we don't have to run the calculation several times when we want it's area.
1 2 3 4 5 6
|
class Square
{
public:
unsigned int width, height, area;
unsigned int calculateArea() { area = width * height; return area; }// Again, I know this looks bad, but it's just an example.
}
|
The problem is that width and height are not constant variables. They may change later in time, and when/if they do, we'll have stagnate data. The area variable will no longer represent the true area of the square. If you wanted the real area, you would have to run the calculateArea() function again.
To avoid stagnate data, you may replace variables with calculations wherever possible. In this case, we can replace 'area' with a function.
1 2 3 4 5 6
|
class Square
{
public:
unsigned int width, height;
unsigned int area() { return width * height; }
}
|
This way, you will never have stagnate data. Also, you know that this expression will
never change, so you don't have to worry about that either.
<mild rant>
I guess what erks me is that the general consensus now seems to be, "Oh, CPUs are powerful enough, don't bother coding efficiently", and then everyone complains when software bogs down their shiny new PC. If we all optimised our code properly we could be running on much lower powered hardware.
</mild rant> |
I agree with you on that too. However, you don't want to optimize your code too much or else, you get
inefficient code.