-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
ToString guidelines #12831
Comments
I'd love it if we could also consider best-practices from the BCL team, i.e. if they have written guidance or tribal knowledge on this, and/or taking great examples of ToString() implementations and figuring out how we can be consistent with those in the Azure SDK. |
Other comments from today's meeting: Considerations:
Concerns about its use:
Ideas for implementation:
|
@tg-msft your format idea resonated with a comment from Chris Sells in the Framework Guidelines:
|
In internal discussion, we can use formattable strings (e.g. In general, I think this is a great idea where there are potentially logs of objects with clear identifying properties like an For example, out of 96 "System.*" assemblies I had loaded in PowerShell and of their 4,199 exported types, only 338 declared/overrode $assemblies = [AppDomain]::CurrentDomain.GetAssemblies() | where FullName -like 'System.*'
$assemblies | measure
# 96
$allTypes = $assemblies.GetExportedTypes()
$allTypes | measure
# 4,199
[type[]]$empty = @()
$allTypes | foreach { $_.GetMethod('ToString', 'Public,Instance,DeclaredOnly', $null, $empty, $null) } | measure
# 338 |
We should have separate suggestions for our For example, |
As for Models, during today's meeting some of the ideas suggested where:
|
From @KrzysztofCwalina perspective, in general, it will be good to have ToString in each client/model but if it involves too much work, then don't do it |
I will start by looking into ToString for our next set of TextAnalytics releases and will come back here with ideas/suggestions/questions ... etc |
This might help. I did an automatic ToString implementation in the past for logging. I defined a format and used reflection to build a consistent string representation of any type automatically. It was extremely useful in logging exceptions: I even have tests for it! https://github.com/MiYanni/Keyboard-Mapper/blob/3fb69d7d78b9933fe2d2fa54659753d6a0f7f823/Common/Extensions/StringExtensionsTests.cs#L184-L189 |
Given we are moving to Typespec and will get the consistency by default for all our generated SDKs I believe any guidelines we want our SDKs to follow should be added to the generation logic directly. Looking at the conversation - as of now the bug seems exploratory and I don't see any consistent ToString() implementation that we would want our generators to follow. Please let me know if my understanding is incorrect. Do we have a tag that we use for experimentation? It is not MQ and not necessarily feature_req but I can see it as an "feature request" for our SDKs? Is there any other tag that will suit this? /cc: @jsquire |
@pallavit, I believe this was a guidelines issue and it might make sense to move it to the azure-sdk repo given that. I agree that implementation of the guidance, once it's established, would likely fall to the generator where appropriate, unless it refers to Core types or other manually-written types. |
What kind of guideline(s) are we looking for here? Do we want guidance from architects on what we should do with ToString() APIs , whether we should have them or what information would be useful for our customers? |
Yes, we are primarily looking for architect guidelines on when we should override |
Hi @maririos, we deeply appreciate your input into this project. Regrettably, this issue has remained inactive for over 2 years, leading us to the decision to close it. We've implemented this policy to maintain the relevance of our issue queue and facilitate easier navigation for new contributors. If you still believe this topic requires attention, please feel free to create a new issue, referencing this one. Thank you for your understanding and ongoing support. |
ToString is very useful for developers, specially in debugging scenarios. As some of our customers (+ @tg-msft ) have pointed out, we’re not very consistent about adding helpful
ToString
methods to our types.The purpose of this issue is to create some guidelines around the usage of
ToString()
.Considerations brought up by .NET team:
ToString()
doesn't change the state of the object, resulting in different behavior with and without attached debugger.The text was updated successfully, but these errors were encountered: