I am trying to get my head wrapped around OOP after programming in more procedural languages for a number of years.

My question: What is the best practice for handling variable values outside of source code?

For example, I have file directory values and regex patterns that may need to be manually changed from time to time. I would like to avoid changing these values directly in the source code. I am thinking of creating a text file that will hold key/value pairs. I would then create an object to set these values so that they can be referenced by any methods in my code.

Is this the right approach or am I violating any OOP principles?


2 Answers 2


Your approach does not violate any OOP principles.

If it is the right approach is unanswerable, because there is no the right approach. I can tell you that it is a right approach, but there are always other approaches possible. That said, if it works for you, stick with it.

  • Great thanks Bart. I should probably remove the "right approach" from my question as I am really only concerned with the OOP perspective. I'll accept your answer shortly (just want to wait a little while in case there are any other viewpoints on this). Thank you for the feedback. Commented Aug 25, 2013 at 17:18
  • @UberNubIsTrue: I mostly added the part about "the right approach" to make my answer more than a one-liner. :-) Commented Aug 25, 2013 at 18:09

This has nothing to do with OOP and everything to do with requirements.

For instance, would it be OK to recompile all or part of the program when the values or patterns change? Then you might chuck them away into a header file or a class (or whatever your language provides) so you wouldn't have to hardcode them into the actual program logic (a good practice anyway).

If not, you probably mean to make the values changeable at runtime. You'd need to read them out of some kind of file format or database and make your program data-driven.

Ultimately, this is something you (or whoever is in charge) need to decide before starting the design because it has direct bearing on what you design. Many procedural, functional or object-oriented approaches tackle both problems, but you need to decide which one you're facing first.

  • Thanks busy_wait, that is helpful. I didn't know if it was better practice (from an OOP perspective) to hardcode each individual value within the classes or if I could keep them external and load them all in a separate object at runtime (while still holding true to OOP principles). OOP is a complete paradigm shift for me so I am questioning all of my previous assumptions around design. Commented Aug 25, 2013 at 18:17
  • That's great. If you don't ask, you'll never know. Hardcoding is always less flexible, but sometimes you don't need or don't want this flexibility. It all depends on what you want/need and how much time you have to spend on each problem. This is part of what makes programming an art.
    – idoby
    Commented Aug 25, 2013 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.