Consider new keyword "methodname" #2164
-
How do people feel about a new keyword "medthodname" - where this is replaced at compile time with the name of the containing method. Motivation here is that when we copy/paste some method several times that contains a nameof(original_method_name) it's easy to overlook and leave the copied methods name inside the nameof. Thoughts... |
Beta Was this translation helpful? Give feedback.
Replies: 13 comments
-
Why stop there? Why not classname, propertyname, eventname, namespacename etc? Also how would this be resolved inside a local method? The innermost method, or the outermost? I think the point I'm trying to make here is that there an infinite number of cases where C# could add a feature to help in one specific use case. But with limited time available to the team, and little spare capacity for students of the language to learn yet another syntax and it's rules, you have to make the case as to why this is so important it makes the cut. I'm not saying this is a bad idea. I'm just asking for a stronger case as to why this is so important it's worth special casing the language for. Have you explored other options like using a Roslyn analyser to warn you when you make a mistake? |
Beta Was this translation helpful? Give feedback.
-
Perhaps the CallerMemberNameAttribute would work for you? |
Beta Was this translation helpful? Give feedback.
-
A language change doesn't seem to be worthwhile, when you can do this with a simple helper method: private string MethodName([CallerMemberName] string name = null) => name; |
Beta Was this translation helpful? Give feedback.
-
If you're copy-pasting the same method several times you've got a problem already. Secondly, the |
Beta Was this translation helpful? Give feedback.
-
Maybe, but refactoring is full of Cut and Pastes. Refactoring tools can help, but sometimes nothing beats a good ol' ctrl-x, ctrl-v. |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt of course; but if it's at the point where this is a serious issue, you're doing it too much. It's the JITters job to inline the methods, not the devs 😛. Unless you've got a million nameofs and a text editor with no find and replace this would never be a major issue imo |
Beta Was this translation helpful? Give feedback.
-
@johnkellyoxford I don't have a problem "already" this is a growing family of methods that share a common pattern which includes writing a log message (containing their names). The fact that I initially create the method by copying another is nothing unusual.
Except for the fact that your quite wrong -
Copying and pasting source code is extremely common, of course cloning and repeating logic is undesirable but that isn't what I did you have assumed that and once again you are in error. The bottom line here is simply to see if C# can be updated to help us refer to the name of the containing method rather than currently having to explicitly refer to that containing method. How these methods come into existence is immaterial, all you need do is consider a class that exposes multiple methods where each method needs to pass its own name (say to a logger). |
Beta Was this translation helpful? Give feedback.
-
@Korporal so you have a very large number of methods all doing similar things in the same class (otherwise nameof(otherMethod) wouldn't be legal)? Why don't you just create a log function that takes
To me, this sounds like a fancy way of getting around saying "I have lots of code duplication". If they're all logging their names use CallerMemberName and a separate log function. |
Beta Was this translation helpful? Give feedback.
-
@johnkellyoxford I don't care how it sounds to you. You cannot evaluate the efficacy of something without seeing it, you have not seen the source code in this case have you? No you have not so please stop implying that you can assess code quality without seeing said code. |
Beta Was this translation helpful? Give feedback.
-
It just seems a very trivial operator when it's so easy to |
Beta Was this translation helpful? Give feedback.
-
@korporal
You've just described one of the primary use cases of |
Beta Was this translation helpful? Give feedback.
-
You're the one asking for this operator. The onus is on your to present a compelling enough case to justify it. That includes providing enogh information and justification to offset how this sounds to people when you make these statements. |
Beta Was this translation helpful? Give feedback.
-
This is already a solved problem. You can use CallerMemberName to pass this info to, say, a logger. If this is insufficient, you need to clarify what is missing and why you need it. |
Beta Was this translation helpful? Give feedback.
A language change doesn't seem to be worthwhile, when you can do this with a simple helper method: