Apr 1, 2021 at 2:14pm UTC
Hi everyone. Im a newbie, because of that i apolige. I have a messy code. I want to clean it and want it shorter. How can i do it?
if(item->parent() == gps)
{
if(scln == "A")
{
seri[0]->setColor(Qt::blue);
seri[0]->setName(gpsList.at(0));
chart->addSeries(seri2[0]);
seri[0]->attachAxis(axisX);
seri[0]->attachAxis(axisY);
chart->legend()->setVisible(true);
queuee[0].enqueue(0.25);
}
if(scln == "B")
{
seri[1]->setName(gpsList.at(1));
seri[1]->setColor(Qt::green);
chart->addSeries(seri2[1]);
seri[1]->attachAxis(axisX);
seri[1]->attachAxis(axisY);
chart->legend()->setVisible(true);
queuee[1].enqueue(0.96);
}
if(scln == "C")
{
seri[2]->setName(gpsList.at(2));
seri[2]->setColor(Qt::yellow);
chart->addSeries(seri[2]);
seri[2]->attachAxis(axisX);
seri[2]->attachAxis(axisY);
chart->legend()->setVisible(true);
queuee[2].enqueue(0.39);
}
if(scln == "D")
{
seri[3]->setName(gpsList.at(3));
seri[3]->setColor(Qt::darkBlue);
chart->addSeries(seri[3]);
seri[3]->attachAxis(axisX);
seri[3]->attachAxis(axisY);
chart->legend()->setVisible(true);
queuee[3].enqueue(0.66);
}
Last edited on Apr 1, 2021 at 2:14pm UTC
Apr 1, 2021 at 2:55pm UTC
First, identify where you have repetition. Then, factor out the parts that are repeating, and supply the differences as
parameters .
In each of your if statements, your instructions are of the following form:
1 2 3 4 5 6 7
seri[i]->setColor( ___ );
seri[i]->setName(gpsList.at(i));
chart->addSeries(seri2[i]);
seri[i]->attachAxis(axisX);
seri[i]->attachAxis(axisY);
chart->legend()->setVisible(true );
queuee[i].enqueue( ___ );
This can be factored into a function with three parameters: The index, the color, and the floating-point number enqueued. Note: Please use a better variable name than 'number'. I am only using 'number' because I don't know what it actually represents.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
void MyClass::update_gps_display(int i, Color color, double number)
{
seri[i]->setColor(color);
seri[i]->setName(gpsList.at(i));
chart->addSeries(seri2[i]);
seri[i]->attachAxis(axisX);
seri[i]->attachAxis(axisY);
chart->legend()->setVisible(true );
queuee[i].enqueue(number);
}
void MyClass::Something()
{
// ...
if (scln == "A" )
{
update_gps_display(0, Qt::blue, 0.25);
}
else if (scln == "B" )
{
update_gps_display(1, Qt::green, 0.96);
}
else if (scln == "C" )
{
update_gps_display(2, Qt::yellow, 0.39);
}
else if (scln == "D" )
{
update_gps_display(3, Qt::darkBlue, 0.66);
}
}
Note: This assumes the other variables are available at a broader scope, such as the class scope. if they are not, I would suggest factoring these variables into some sort of struct or class.
Edit: I like keskiverto's idea even more. It allows you to leave everything in the current scope. You just need some logic to avoid the process if (x < 0 || x > 3).
Last edited on Apr 1, 2021 at 2:58pm UTC
Apr 2, 2021 at 6:04am UTC
thank you for all your answers.
I will pick up which is more make sense to me, implement and i will update my reply
Apr 6, 2021 at 11:20am UTC
Obviously, it must be the second, because it has the word "cod" right in it.
Apr 6, 2021 at 11:47am UTC
My uncertainty is based on both having the word 'tartar' in the name. They are both in my estimation equally unpalateable unless you find tartare sauce a worse item to spread on your cods, or cod if you only have one to satisfy your appetite.
Apr 6, 2021 at 4:09pm UTC
"Tartare" or "tartar"? I don't think I'd like tartare sauce, mainly because I can't stomach raw meat.
Apr 6, 2021 at 4:49pm UTC
I blame my tartar on the Tartar tartar sauce I had with my steak tartare.
Apr 6, 2021 at 4:58pm UTC
Any spare chips to go with the cod?
Apr 6, 2021 at 8:53pm UTC
I'll see if I can find some in my refrigerator.
Apr 8, 2021 at 12:13am UTC
What does Codus Asteriskus taste like with Mongol tartar sauce made from tooth plaque...
Apr 8, 2021 at 3:07am UTC
Don't know but sounds like eating out of a gut bucket left in the sun for a whole day, even though I haven't tried that either.
Apr 8, 2021 at 11:20am UTC
Thanks guys. I haven't been able to check the thread since my last post (2 days ago), and this made my day. I laughed harder than I expected to.
What a great way to start the morning.