question ... running `pdk validate` returns this e...
# testing
j
question ... running
pdk validate
returns this error
Copy code
pdk (WARNING): puppet-lint: class parameter with String type defaults to empty string
on this line in a class's parameter list
Copy code
String $xyz = '' ,
... removing the
= ''
makes
puppet-lint
happy, but it changes the parameter from "optional with a default value" (which is what i need in this case) to "required" (which is NOT what i need)
can somebody explain why this check was created in the first place?
b
Because in most situations empty string isn't what you want,but undef
Best practice says that undef is desired for string and Integer,and empty hash/array for hash/array
dead yes 1
j
i don't know that i agree with it, but at the same time i'm only familiar with my own code base, i have very little insight into what "in most situations" is referring to ... speaking for myself, i understand how to set a parameter's default value, and when i give a parameter an empty string as a default value, it's because that parameter needs the empty string as its default value. but if one of my junior guys sees that message, he's going to try and make the linter error go away and may not realize that he's making the parameter "required".
i guess i'll have to add this to the list of linter checks i have to disable in every single module.
for the benefit of PDK users who may not know how to do this ... add this to your module's
.sync.yml
file:
Copy code
Rakefile :
  extra_disabled_lint_checks :
    - 'params_empty_string_assignment'
then run
pdk update
k
undef
resolves to empty string, so I don't see any reason to default to empty string. for junior teammates, it's better to follow the practices that the rest of the puppet world (i.e., Vox Pupuli) uses
y
It depends on the class behaviour. If your class literally wants empty string, then disable the linter for this line with a control comment. If empty string is just a default value and not really used in the class, then converting it to
Optional[String[1]]
$xyz = undef` makes more sense.
dead yes 1
c
Using
undef
instead of
''
allows you to enforce that the string should have at least one character in it. That makes it easier to distinguish between "default value" and "something messed up". Often, a query or data source that mis-fires will return an empty string. For example, a shell script that doesn't produce anything will result in a
stdout
of
''
and using
Optional[String[1]]
allows you to trap that as an error case.
j
i understand all that ... my point is, why is this a
puppet-lint
test which is enabled by default?
c
Because it is generally the best way to handle these class parameters and if the lint check doesn't default to preferring it, then there's no forcing function that improves quality across the ecosystem.