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

pkg: improve field and variable names #2623

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alexandear
Copy link
Member

@alexandear alexandear commented Sep 16, 2024

This PR renames y to instConfig because y is too short for its usage scope, which broke the CodeReviewComments#variable-names statement:

The basic rule: the further from its declaration that a name is used, the more descriptive the name must be

@jandubois
Copy link
Member

I don't think yaml is such a good name either. It describes a data format, not what the data is supposed to represent. And the variable has nothing to do with YAML beyond that the struct has been loaded from a YAML file/string.

So if we rename y to something longer, how about instConfig, which is at least descriptive? And it goes well with instName and instDir, which we already use.

@afbjorklund
Copy link
Member

afbjorklund commented Sep 17, 2024

Renaming the field is OK, but I don't like not being able to use short variable names (like i, j and k).

Variable names in Go should be short rather than long. This is especially true for local variables with limited scope. Prefer c to lineCount. Prefer i to sliceIndex.

@alexandear alexandear force-pushed the refactor/improve-var-field-name branch from 57e0393 to 1cc6a8a Compare September 17, 2024 09:23
@alexandear
Copy link
Member Author

So if we rename y to something longer, how about instConfig, which is at least descriptive? And it goes well with instName and instDir, which we already use.

Totally agree with you. Renamed to the instConfig.

@alexandear
Copy link
Member Author

Renaming the field is OK, but I don't like not being able to use short variable names (like i, j and k).

It's okay to use i, j, k in for loops where the variable's scope is short, 2-4 lines. Also, one-letter vars are acceptable for receiver names.

It's not okay to use y as the variable name in our case, where the scope is 83 lines.

The Google Go Style Guide contains explanations on variable naming.

@jandubois
Copy link
Member

Renaming the field is OK, but I don't like not being able to use short variable names (like i, j and k).

I think it is something to decide on a case-by-case basis. Sometimes the short names work fine, either when their declaration and use case all fits on a single screen, or when the variable is used all the time and not just sparingly.

I could go with cfg for a local variable, but I think using instConfig consistently is better in this case.

Note also how FillDefault continues to use y (and o and d) and has not been modified, because there it is used in every other line, and a longer name would just drown out the field names.

@alexandear alexandear force-pushed the refactor/improve-var-field-name branch 2 times, most recently from 229dbd3 to 1c7a78e Compare September 17, 2024 16:42
Comment on lines +75 to +76
Instance *store.Instance
InstConfig *limayaml.LimaYAML
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is outside the scope of this PR, but I'm wondering why the BaseDriver has an InstConfig field. Isn't that a duplicate of Instance.Config?

I don't have time to drill into the code right now, but could InstConfig (nee Yaml) be replaced with Instance.Config everywhere? If not, then we should have a comment explaining when/why they can be different.

@balajiv113 Do you remember why you needed both?

@jandubois
Copy link
Member

jandubois commented Sep 19, 2024

@alexandear Please resolve conflicts.

@afbjorklund Do you still object to the renaming of local variables in this PR, or was that more a general sentiment.

I'll file a separate issue (#2636) to investigate why the BaseDriver type has both a store.Instance and a limayaml.LimaYAML field; that can be sorted out once this PR has been merged.

@alexandear alexandear force-pushed the refactor/improve-var-field-name branch from 1c7a78e to 2fd41a6 Compare September 19, 2024 10:28
@alexandear alexandear changed the title pkg/hostagent: improve field and variable names pkg: improve field and variable names Sep 19, 2024
Signed-off-by: Oleksandr Redko <oleksandr.red+github@gmail.com>
@alexandear alexandear force-pushed the refactor/improve-var-field-name branch from 2fd41a6 to 41102b6 Compare September 19, 2024 10:32
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.

3 participants