How can i clean my messy code

Apr 1, 2021 at 1:58pm
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?

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
33
34
35
36
37
38
39
40
41
42
43
44
45
 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);
    }
Apr 1, 2021 at 2:53pm
Do not double-post. Other thread: http://www.cplusplus.com/forum/general/277206/
Apr 5, 2021 at 7:29pm
Here:
Don't use a bunch of if statements like that. Use else if instead. And add an else at the end for error checking.
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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);
	}
	else 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);
	}
	else 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);
	}
	else 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);
	}
	// Add an else in here for error checking
	else
	{
		std::cout << "Error. SCLN must be A, B, C, or D. \n";
	}
}

And yeah, please don't double-post. It fills up the website with unnecessary junk that we have to wade through, and it causes people to think you may be a spammer.

Best,
max
Apr 5, 2021 at 8:15pm
Please do not bump double-posts. Thank you.
Apr 6, 2021 at 12:42am
"Bump"? What do you mean exactly? Are you referring to me and @keskiverto? If so, why not? I don't think anyone on this forum likes double-posters, thus the warning to the OP.
Apr 6, 2021 at 1:12am
When one replies to a thread it appears at (is "bumped") to the top of the post queue.

Forum etiquette is to keep all the conversation in one place and also to get useless threads out of the feed. If someone says "this is a duplicate of <link>", ideally one should follow the link and comment there.
Last edited on Apr 6, 2021 at 1:13am
Topic archived. No new replies allowed.