Similar Function, But long code with Macro
Similar Function, But long code
Prerequites
1
C++
What is Similar Function, But long code
If we make a simple class, but the code line is so long over 10K. And that time, we should make a new simple class. The only difference of between classes is former class is add operation and later class is minus operation. But there are so many code lines because there’re initilization parameters, make region and optimizing table and so on but calculation is difference only + or -.
If we want to make two class, we should copy and paste from add class to minus class. However it is sometimes time-consuming or waste of effort.
1. How to deal with this case - 1
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
// macro.h
#ifdef USE_MINUS
#define OP -
#else
#define OP +
#endif
// add.h
class CAdd
{
public:
int Operation();
};
// minus.h
class CMinus
{
public:
int Operation();
};
// add.cpp
#include "macro.h"
#include "c1.h"
int CAdd::Operation()
{
return 1 OP 2;
}
// minus.cpp
#define USE_MINUS
#include "macro.h"
#include "c2.h"
int CMinus::Operation()
{
return 1 OP 2;
}
// main.cpp
#include "add.h"
#include "minus.h"
#include <iostream>
int main()
{
CAdd cAdd;
CMinus cMinus;
int i32Result1 = cAdd.Operation();
int i32Result2 = cMinus.Operation();
std::cout << i32Result1 << '\n';
std::cout << i32Result2 << '\n';
return 0;
}
The code is same, add.h, cpp and minus.h, cpp but the only difference is the setting of #define. It means using #define is good method of making difference.
2. How to deal with this case - 2
Just use macro, but it is not to good at debugging later, so if we want to adapt to class, it is not good, but just for understanding design to minimize code copy and paste.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#define DEFINE_OPERATION_CLASS(CLASSNAME, OPERATOR) \
class CLASSNAME \
{ \
public: \
int Operation() \
{ \
return 1 OPERATOR 2; \
} \
};
DEFINE_OPERATION_CLASS(CAdd, +)
DEFINE_OPERATION_CLASS(CMinus, -)
//main.cpp
CAdd add;
CMinus minus;
add.Operation(); // 3
minus.Operation(); // -1