ArcticaProject / nx-libs

nx-libs
Other
120 stars 39 forks source link

Suggestion for cleaner code #448

Open uli42 opened 7 years ago

uli42 commented 7 years ago

Using the following macros we can greatly reduce the number of lines in the source files and make the code more readable:

#ifdef DPRINTLOCATION
#define DPRINT(...) fprintf(stderr, "%s:%s(%d) ", __FILE__, __func__, __LINE__);fprintf(stderr, __VA_ARGS__)
#else
#define DPRINT(...) fprintf(stderr, "%s: ", __func__);fprintf(stderr, __VA_ARGS__)
#endif

#ifdef TEST
#define PTEST(...) DPRINT(__VA_ARGS__)
#else
#define PTEST(...)
#endif

#ifdef DEBUG
#define PDEBUG(...) DPRINT(__VA_ARGS__)
#else
#define PDEBUG(...)
#endif

#ifdef WATCH
#define PWATCH(...) DPRINT(__VA_ARGS__)
#else
#define PWATCH(...)
#endif

#ifdef WARNING
#define PWARNING(...) DPRINT(__VA_ARGS__)
#else
#define PWARNING(...)
#endif

#ifdef PANIC
#define PPANIC(...) DPRINT(__VA_ARGS__)
#else
#define PPANIC(...)
#endif

E.g. we can replace common patterns like this

#ifdef TEST
fprintf(stderr, "Somefunction: variable x has value [%d]\n", x); 
#endif

by this oneliner:

PTEST("variable x has value [%d]\n", x);

By defining DPRINTLOCATION you can also print out filename and line number.

I suggest introducing that to increase readability. What do you think?

sunweaver commented 7 years ago

Hi Ulrich,

On Tuesday, April 25, 2017, Ulrich Sibiller wrote:

Assigned #448 to @sunweaver. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

Yes. That looks good. Please go ahead.

Mike

-- Sent from my Fairphone (running Sailfish OS)...