Ask Your Question

Best practice for default values

asked 2013-09-02 08:55:10 -0600

What is the best practice to use for optional parameter default values inside parametrized class or defined type definitions?

I've seen many modules that use either empty strings or zeros or undef.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2013-10-24 08:36:31 -0600

updated 2013-10-24 08:36:44 -0600

Create simple class params with default values.

And here are my 2 cents on using it:

  1. Inherit your parameterized classes from params and init defaults as params::value. Example. (though it's not applicable for defines)
  2. Init all optional parameters in your parameterized classes/defines, and check them in your class'/define's code. It works for both classes and defines, gives you more control, but requires more code to write. Example:
class ntp (
  $autoupdate        = undef,
  $config            = undef,
)  {°
  if $autoupdate == undef {
    $real_autoupdate        = $ntp::params::autoupdate
  } else {
    $real_autoupdate = $autoupdate

  if $config == undef {
    $real_config        = $ntp::params::config
  } else {
    $real_config = $config
edit flag offensive delete link more

answered 2013-09-02 14:24:34 -0600

nibalizer gravatar image

I tend to use all caps "NONE" because I'm pretty confident that no one will accidentally pass that into my class. Empty strings, zeros, and undefs are all valid arguments in some cases. Later on in the module I do string compares agianst "NONE" to see if the value is set to default or something was passed in.

I'm realizing now that I'm using the "==" operator which is case insensitive so the all caps is actually pointless.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2013-09-02 08:55:10 -0600

Seen: 317 times

Last updated: Oct 24 '13