Code cleanup

My primary function is about 1000 lines long. But I have to create two configurations : Debug & Release version. In general they are the same but they also have many differences. My source file becomes very long and each time I have to update, it's hard to update quickly both two configurations.

1
2
3
4
5
6
7
8
9
10
11
void function_Debug(...)
{
   // lots of generic code
   // "Debug" code
}

void function_Release(...)
{
   // lots of generic code
   // "Release" code
}


Are there any methods to wrap these configurations into a single function? That makes my code easier to work then. I don't want to use if because it directly affects my program's performance.
Last edited on
I'm not sure, but you could try something like this:
1
2
3
4
5
6
7
8
void function( ... )
{
#ifdef DEBUG
    // code for debug config

#else
    // code for release
}
@ Fransje
Thanks for your reply. But it causes an another problem : It only produces a function, "Debug", or "Release". I want two functions with two different configurations, but my code should be clear as possible. For example, a normal function, but when compiling it can produce multiple functions with multiple different configurations, is it possible?
you might as well write it like so:

1
2
3
4
5
#ifdef DEBUG
function_Debug(...); // call this function in case of DEBUG
#else
function_Release(...); // otherwise call this
#endif 


For example, a normal function, but when compiling it can produce multiple functions with multiple different configurations, is it possible?
What do you expect? The compiler cannot write code for you?
@ coder777

What do you expect?

I want to keep my code as short & clear as possible.

In your example, you only specify the "debug" or "release" case, but I need both. That's why I need a generic function but it's still able to create multiple functions with multiple different configurations (for example it will create two functions (Debug and Release versions)).
> My primary function is about 1000 lines long

And who is going to debug/maintain it?
Factor your code into manageable chunks.


> That's why I need a generic function but it's still able to create multiple functions with multiple different configurations

Templates. Something like this:

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <vector>
#include <cassert>
#include <type_traits>

struct release
{
    static void foo() { std::cout << "foo for release build\n" ; }

    template< typename T > using vector = std::vector<T> ;

    // more release build specific stuff
    // ...
};

struct debug
{
    static void foo() { std::cout << "foo for debug build\n" ; }

    template< typename T > struct vector : std::vector<T>
    {
        using base = std::vector<T> ;
        using base::base ; // requires support for inherited constructors
                            // clang 3.3  GCC 4.8

        using typename base::size_type ;
        // ...

        T& operator[] ( size_type pos )
        {
            validate_pos(pos) ;
            return this->at(pos) ;
        }

        const T& operator[] ( size_type pos ) const
        {
            validate_pos(pos) ;
            return this->at(pos) ;
        }

        void validate_pos( size_type pos ) const
        {
            if( pos >= this->size() ) std::clog << "vector: out of range access\n" ;
            assert( pos < this->size() && "vector: out of range access" ) ;
        }
    };

    // more debug build specific stuff
    // ...
};

template < bool DEBUG = false > void my_common_function()
{
    using config = typename std::conditional< DEBUG, debug, release >::type ;
    typename config::template vector<int> vec { 1, 2, 3, 4, 5 } ;
    std::size_t pos = 10 ;
    int value = 5 ;

    // ...

    config::foo() ;

    // ...

    vec[pos] = value  ;

    // ...
}

int main()
{
    std::cout << "call release version of my_common_function\n\n" ;
    my_common_function<>() ; // release version
    std::cout << "\ndone\n\n" ;

    std::cout << "call debug version of my_common_function\n\n" ;
    my_common_function<true>() ; // debug version
    std::cout << "\ndone\n" ;
}


call release version of my_common_function

foo for release build

done

call debug version of my_common_function

foo for debug build
vector: out of range access
Assertion failed: pos < this->size() && "vector: out of range access", ...
Works for me. Thank you - I appreciate it!
Topic archived. No new replies allowed.