2

Hi this is more of a style question. I have a small script that works with a socket, and I have constants (i know they are not real constants, but heyy, at least they are in capital) that declare some properties,

# Port address for the serial port to read the weight from
PADDR = '/dev/ttyUSB0'
# socket address uri, backlog, and buffer
SADDR = 'localhost:28000'
BACKLG = 0
BUFF = 1024
# for cross platform compatibility use epoch in unix time
EPOCH_START = datetime(1970, 1, 1)

I however might add functionality to change these by parsing arguments. So the question is, should I just change these directly when parsing arguments?

or should I be nice and use the above constants as defaults and change the options which I would then pass around to functions who need them? This seems a bit too much, but then again, I don't want to write code that will be frowned upon :)

2
  • 3
    Why not use an actual argument parser that supports default values properly? Commented Nov 22, 2013 at 0:35
  • I just started doing it actually. But want to keep the constants at the top too, to make things easier. Commented Nov 22, 2013 at 1:07

1 Answer 1

3

Have DEFAULT_PADDR as a constant; but paddr as a variable with DEFAULT_PADDR as its default value. Do not change a "constant", even if it is just a matter of style - your code becomes cryptic.

I.e. for now, do this:

DEFAULT_PADDR = '/dev/ttyUSB0'
# ...
paddr = DEFAULT_PADDR

then you can change paddr when you add argument parsing.

Sign up to request clarification or add additional context in comments.

1 Comment

kool. I was now thinking the same, but didn't occur that i could put DEFAULT before. I didn't want to have PADDR and paddr :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.