Author Topic: Using const strings instead of #define  (Read 4254 times)

borfast

  • Guest
Using const strings instead of #define
« on: November 29, 2010, 11:20:37 pm »
At the same time I investigated why openxcom couldn't open my xcom files, I noticed you use
Code: [Select]
#define DATA_FOLDER "./DATA/" instead of something like
Code: [Select]
const std::string DATA_FOLDER = "./DATA/";
Using const instead of #define has advantages over #define and, apart from the work of changing an existing code base, has no disadvantages at all.

The first advantage is that you gain type checking for your constant variable, something you don't have with a #define.
The second advantage is that when using const, scope is respected and you don't pollute the global namespace.

I know you are trying to just get things done and leaving premature optimizations and such for later - but think of this as an investment for the future in the form of more readable code and maintainable code. :)

Offline SupSuper

  • Lazy Developer
  • Administrator
  • Commander
  • *****
  • Posts: 2160
    • View Profile
Re: Using const strings instead of #define
« Reply #1 on: November 30, 2010, 02:50:48 pm »
Thanks, I'll keep it in mind.

The hardcoded DATA_FOLDER is just a temporary thing anyways, I might move it to an Options file once that's implemented.

Offline Eeyoocah5Moh

  • Sergeant
  • **
  • Posts: 28
    • View Profile
Re: Using const strings instead of #define
« Reply #2 on: January 26, 2011, 03:58:05 am »
> The hardcoded DATA_FOLDER is just a temporary thing anyways, I might move it to an Options file once that's implemented.

IMHO it is better to just use current working directory and write less code. On Windows you can set working directory in shortcut properties. On Unix you can write wrapper script or use shortcuts if you are using some DE. Keep it simple.