If it won't be simple, it simply won't be. [Hire me, source code] by Miki Tebeka, CEO, 353Solutions

Saturday, December 02, 2006

DRY

The DRY (do not repeat yourself) principle is a hard to achieve, but worth the effort.
(This rule is also known as SPOT - Single Point Of Truth).

One good solution I find when some piece of information is very configurable and need to be shard with many different programs is to write a little script that prints the required value.

For example, say we need to decide what is the root directory for something. It can be either the debug version of a standard one.

The script rootdir.py will look something like:
from os import environ
from os.path import join
from getpass import getuser

if "DEBUG" in environ:
# Every user has his/her own test root directory
ROOT_DIR = join("/tmp", "testing", getuser())
else:
ROOT_DIR = "/usr/local/cool"

if __name__ == "__main__":
print ROOT_DIR
If you're using Python, you can just write from root import ROOT_DIR, however if you're using bash you can write ROOTDIR=`./root.py` and then use $ROOTDIR
Post a Comment

Blog Archive