mardi 19 décembre 2017

The best way to avoid using lots of global variables

I'm translating an old project, written before object oriented programming became something that is common and I have lots of global variables there. It's a project written in Pascal and I try to rewrite it in modern c++11 way, as well as trying to achieve the most possible portability as I can.

And my purpose is to make the project easy to modify for other people. And my problem is that I get lots of LNK2019 errors in Visual Studio right now due to these variables are not well declared.

The thing is that I have some header files that are full of global variables like

bool DS_Flag_0;
...
bool DS_Flag_11;

and

typedef unsigned short int UInt16;
//...

const UInt16 DayManth;
const UInt16 DayYear;
const UInt16 DayLongYear;
const UInt16 DayPer;

const int SecMin;
const double SecQuart; 
const double SecClock;
const double SecHalfDay; 
const double SecDay;
const double SecWeek;
const double SecYear;
const double SecPer;

And I need somehow to use all of these variables inside of my project in almost every file possible. (Yes, that's a really bad designed project but I do not have that much time to rebuild it from stratch so..)

There are some options that I see: 1) Use "extern" modifier and create lots of ".cpp" initialization files because I need to initialize those as well 2) Somehow make it possible (may be) to initialize all of them inside of ".h" headers. It this a good programming practice if I want to make the project readable for other people? 3) May be I somehow can implement object-oriented patterns here so avoid lots of variables and make it more modern?

The purpose of the project is to translate lots of satellite messages from binary file data into another binary format and I need at first to get it to work with these data (but this information is not that neccessary to answer the question).

Aucun commentaire:

Enregistrer un commentaire