Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor message formatting #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mbethke
Copy link

@mbethke mbethke commented Jan 19, 2017

Move sprintf message formatting into its own object method of HP::Server and make add_info(), add_extendedinfo() and add_message() use it. This way

  • The first argument is always a format string. Should there be no further arguments, the sprintf is a no-op.
  • Additional arguments can be either scalar references or plain scalars. Every reference is taken to be the name of an attribute to retrieve; plain scalars are taken as-is.

This saves a lot of typing and makes code somewhat easier to read by replacing all the nested
add_info(sprintf "foo %s bar %d%s", $self->{attr1}, $self->{attr2}, 'quux')
calls with
add_info('foo %s bar %d%s', \'attr1', \'attr2', 'quux').

Considering that attribute lookups are much more frequent in arguments to add_info etc. than calculated arguments, it would make sense to reverse the semantics and insert scalar references as-is while plain
scalars would be taken as attribute names. This would save a heap of unsightly backslashes. However, the way it is now is completely compatible with the old calling conventions so it should be the gentler change after all.

I just came up with this while adding --customfanspeeds and actually did it first, however to make the two changes independent I based the customfanspeeds change on the master as well as this is a fairly big one stylistically :)

Move `sprintf` message formatting into its own object method of
`HP::Server` and make `add_info()`, `add_extendedinfo()` and
`add_message()` use it. This way

* The first argument is always a format string. Should there be no
  further arguments, the `sprintf` is a no-op.
* Additional arguments can be either scalar references or plain scalars.
  Every reference is taken to be the name of an attribute to retrieve;
  plain scalars are taken as-is.

This saves a lot of typing and makes code somewhat easier to read by
replacing all the nested `sprintf "foo %s bar", $self->{attr} ...` calls
with `'foo %s bar', \'attr'`.

Considering that attribute lookups are much more frequent in arguments to
`add_info` etc. than calculated arguments, it would make sense to
reverse the semantics and insert scalar references as-is while plain
scalars would be taken as attribute names. This would save a heap of
unsightly backslashes. However, the way it is now is completely
compatible with the old calling conventions so it should be the gentler
change after all.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants