This question deals with the ability to construct a structure that is able to be easily iterated through.
The problem:
I have four different "boxes":
- Box A: Always has 1 apple and 1 pear
- Box B: Always has 1 orange and 1 pear
- Box C: Always has 1 peach
- Box D: Can have varying amounts of apples and oranges (0 - 10+) but always has 1 pear
Each piece of fruit has a different taste value
I currently process this information by creating a queue of the different taste values and box types then process this information with a lots of if-elses in multiple functions.
Here is an example of one function that seeks to get a total sum of the taste values e.g.
void process_crate(vector<string> boxes, vector<double> taste_value, vector<vector<double> > amount){
unsigned int count = 0; // holds index in taste_value
double sum_taste = 0; // holds taste value
for(unsigned int i = 0; i < boxes.size(); i++){
string active_box = boxes[i];
if(active_box == "A"){ // An apple and a pear
sum_taste += taste_box_A(taste_value[count],taste_value[count+1]); // user preference
count += 2;
}else if(active_box == "B"){ // An orange and a pear
sum_taste += taste_box_B(taste_value[count], taste_value[count+1]); // user preference
count += 2;
}else if(active_box == "C"){ // A peach
sum_taste += taste_box_C(taste_value[count], taste_value[count+1]); // user preference
count += 1;
}else{ // Box D varying apples and oranges, but always a pear
unsigned int apples = amount[i][0];
unsigned int oranges = amount[i][1];
vector<double> apple_tastes(apples);
vector<double> orange_tastes(oranges);
double pear_taste = taste_value[count+apples + oranges];
if(apples != 0){
for(unsigned int j = 0; j<apples; j++){
apple_tastes[j] = taste_value[count+j];
}
count += apples;
}
if(oranges != 0){
for(unsigned int j = 0; j<apples; j++){
orange_tastes[j] = taste_value[count+j];
}
count += oranges;
}
sum_taste += taste_box_D(apple_tastes, orange_tastes, pear_taste); // user preference
count += 1;
}
}
}
I think use of IF-ELSE statements combined with lots of vectors to store the data could be greatly improved.
Any suggestions?
1 Answer 1
It's hard to know where to start with this, but here are some things that may help you improve your code.
When seeking a review, provide full code
The posted code is just a fragment with a lot of pieces missing. For instance, all of the taste_box_x
functions are missing and it's not really very obvious what they might be. To get a better review, post a better question.
Pass complex types reference rather than by value
For complex types such as std::vector
, it's usually better to pass by reference rather than by value to avoid forcing each function call to make copies. If the passed object should not be modified by the routine, pass a const
reference.
Use classes
Since this is C++, it would make sense to use classes. For example, one could imagine that there is a base class box
from which each of the box types might be derived. User taste preference could be an std::unordered_set
in which each fruit type is the key and the value would be that user's taste weighting value. Fruits might be an enum class
, etc.
Don't abuse using namespace std
Putting using namespace std
at the top of every program is a bad habit that you'd do well to avoid.
sum_taste
and thecount
? \$\endgroup\$