# Modularization question...

• 08-04-2005
Chaplin27
Modularization question...
Hello people,
Choose from the two versions of this post:

[THE LENGTHY INTRODUCTION]
So, I'm making some functions, I think I'm real slick, when all of a sudden...WHAM! Right in the face...a question begins to form, making my skin start to crawl, and without me even realizing it, I'm scratching my head...ok enough of the lengthy introduction. So...

[RIGHT TO THE POINT]
I'm writing a program and making some calculation modules. Now the main data is the current salary employee's are getting paid, and then their current salary with an 8% increase tacked on.
Also, the total payroll pre-increase, and the total payroll post-increase.
The functions I have are Calculate_pay_increase: which simply receives a salary and does the math to return the increased salary
The other function is calculate_total_payrolls: which would calculate the pre and post payrolls. However, am I adding more math than necessary when I do this? Should I, instead, calculate the total post-increase payroll within the Calculate_pay_increase function? Or is that making the function do too much? Or should I be willing to do extra calculations for the sake of the modules?
• 08-04-2005
Rashakil Fol
So you're worried about the inefficiency of doing a little extra summing?

Good.

What are you using to store the employee's pay? Presumably a number that gets rounded to the nearest cent or nearest dollar (or rounded down) -- Even if you aren't rounding, that's what they're going to end up getting paid, so you should round.

You should re-add the list of new payrolls, after rounding them to the nearest cent or dollar. Multiplying the other total by 1.08 will give a result that is off by a few cents (or dollars) because of rounding.
• 08-05-2005
C3Pnuts
What you should be doing is writing an object-oriented program since you are using an object-oriented language.

You've got an Employee object that has a salary data member and a getSalary member function. You have a Payroll object that has a totalPayroll data member, an adjustedPayroll data member (for the pay increase amount), and a list of pointers to Employee objects. You could then have a calculateTotalPayroll member function (in Payroll object) that calculates your total payroll, by running through its list of employees and calling a getSalary function for each, which it sums on the fly, and returns it. The same function could calculate your adjusted payroll if you set the function up with an argument that is the payIncrease percentage. Just pass it zero for standard payroll calculation. If the function receives anything other than zero in the argument, it adds the adjusted amount to the adjustedPayroll member instead of the totalPayroll member.

Just an observation.

If you intend to keep your functional approach, consider whether you actually need each employees post-increase pay amount for anything else. If not, then it would be appropriate to go ahead and have the Calculate_pay_increase function do the total post-increase payroll calculation.
• 08-05-2005
orbitz
C++ is more of a multi-paradigm language. How well it supports OOP is questionable. However if efficiency is your concern, you should write the code in the most straightforward way possible, then run it throuhg a profiler to discover any bottlenecks. On *NIX, gprof is commonly used. MSVC++ come swith one too I beleive.
• 08-05-2005
Salem
This is where design comes in, where you sit down and plan all the major functional areas which you'll need before you even get as far as 'int main'
In a short amount of time (far shorter than you've spent coding so far), you'd see your need for summing (or whatever) in several places, and that would assist you with writing the code the right way.