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

Replace typeof(X).Name with nameof(X) #1077

Open
1 task done
paulirwin opened this issue Dec 25, 2024 · 7 comments
Open
1 task done

Replace typeof(X).Name with nameof(X) #1077

paulirwin opened this issue Dec 25, 2024 · 7 comments
Assignees
Labels
good-first-issue Good for newcomers is:task A chore to be done pri:low up-for-grabs This issue is open to be worked on by anyone

Comments

@paulirwin
Copy link
Contributor

Is there an existing issue for this?

  • I have searched the existing issues

Task description

Split from PR #1074. There have been a few cases we've fixed where the code has been getting the name of a type as a string by using the pattern typeof(SomeType).Name, when code analysis warnings show that we should be using nameof(SomeType) instead. This potentially has a small performance benefit if the compiler/PGO is not already smart enough to recognize this pattern, but at the least it will simplify the code and reduce some analysis warnings.

@paulirwin paulirwin added up-for-grabs This issue is open to be worked on by anyone good-first-issue Good for newcomers pri:low is:task A chore to be done labels Dec 25, 2024
@paulirwin paulirwin added this to the 4.8.0-beta00018 milestone Dec 25, 2024
@manognya-b
Copy link

can i take up this issue?

@paulirwin
Copy link
Contributor Author

can i take up this issue?

Please! Keep us posted on your progress, let us know if you have any questions, and submit a PR when ready. Thanks!

@paulirwin
Copy link
Contributor Author

Using a Regex search might be helpful. I found several hits using this pattern: typeof\([^\)]*\)\.Name. There may be other, similar variations of this pattern, though.

@manognya-b
Copy link

Using a Regex search might be helpful. I found several hits using this pattern: typeof\([^\)]*\)\.Name. There may be other, similar variations of this pattern, though.

Thanks for the tip! As I understand it, typeof(SomeType).Name has to be moved to nameof(SomeType) for all all relevant files in the repository? If this is correct, I will go ahead and use regex to filter out usages and change them appropriately. Thanks!

@paulirwin
Copy link
Contributor Author

That's correct, and be on the lookout for any other similar opportunities to use nameof instead of something off typeof (or .GetType() for i.e. sealed types that cannot be something else at runtime).

@manognya-b
Copy link

Update: I've completed the moving of typeof(SomeType).Name to nameof(SomeType). I have been looking into opportunities to convert other similiar usages, and had some queries:

  1. For usages like x.GetType().NameSpace and x.GetType().Fullname, where the result generated would be different, I was unable to find equivalents like nameof(x). Is there any alternative I can explore for this?
  2. I also wanted some inputs on how to check whether x.GetType().Name is sealed or not as manual checking by locating the parent class and looking for the keywords sealed or struct would be tedious considering the size of the codebase.

Thanks!

@paulirwin
Copy link
Contributor Author

Great! I do not think there is a better option for Namespace and FullName. No worries there.

For .GetType().Name, perhaps my idea is not the best. It might make maintenance more difficult in the future if the type of the variable changes in later versions of Lucene. My original thinking was that if x was a struct or primitive type like int, or string, or a sealed class type, then it can't be anything else at runtime, so might as well use nameof with the type of x. There would not be a need to look at the parent classes, because a parent class by definition can't be sealed. But I hadn't considered the maintenance aspect. Unless you think it's worthwhile to do, I'm good with skipping that for now.

(Aside: this is where it would be nice if C# had an equivalent of C++'s decltype specifier, so we could do nameof(decltype(x)) to make this a compile-time evaluation, but alas, that's not a thing, yet.)

If you feel good that you've captured all occurrences, I look forward to reviewing your PR when you're ready. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good-first-issue Good for newcomers is:task A chore to be done pri:low up-for-grabs This issue is open to be worked on by anyone
Projects
None yet
Development

No branches or pull requests

2 participants