-
Notifications
You must be signed in to change notification settings - Fork 125
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
Tweaks to String.safeForSwiftCode
to inform caller if a change was made
#645
Comments
Sure, I think that's a reasonable change, as long as there's still the existing variant that's used in most places in the codebase. This might also be a good time to rename the closure if we want to, I know @simonjbeaumont had thoughts on the name - ideas? We need two names now, one that returns a string, and another that returns a tuple of a string and a bool. |
I'd be interested to hear Si's thoughts on the naming.
|
What I don't love is that we have a property that stores a closure but sounds like a computed property itself: To keep with your suggested nomenclature but adjust to communicate use, we could use To the point about preserving the ability to call it and automatically discard the boolean, what's the motivation? Presumably to avoid having to always do
I'd like to gently push back on whether we need this change at all though, because if we're just trying to tidy up code like this, it could be spelled as: let caseName = context.swiftSafeName(name), changed = caseName != name
return .enumCase(name: caseName, kind: changed ? .nameWithRawValue(.string(name)) : .nameOnly) ...or even... let caseName = context.swiftSafeName(name)
return .enumCase(name: caseName, kind: caseName == name ? .nameOnly : .nameWithRawValue(.string(name))) And, that would allow the places where we don't care to not pay the cost of the additional comparison, since in the proposed change, we'd implement the one that doesn't care in terms of the one that does. |
Yeah there's only one place in the generator that cares about whether they're the same, but hundreds that don't. Regarding the naming, we've tried to avoid terms like "sanity" and instead use similar terms like "soundness", "safety", etc. |
It was less about the spelling and more about avoiding the comparison when the result of the comparison is already known. But I do acknowledge that the change really is only for the benefit of my PR at the moment, which is why I opted to ask in an isolated question and not just make the change. I do wonder if there are other contexts that do care too and have just gone about it with comparisons.
Sanitize/Sanitizer would be fine though, it's not from "Sanity" but to do with cleaning. |
@theoriginalbit is correct here. Sanitization is to do with cleaning, not making something sane or implying it was previously insane. |
TIL! 🙂 |
Well, I'm happy to close this if the consensus is that it won't provide any benefit. |
While merging
main
into my PR, #628, I noticed the changes introduced by #638 and #640 and it made me think more about theTranslatorContext
and how it could provide more context (😄) to mytranslateVariableCase(_:)
function. Specifically the if statement which needs to compare the two strings to see if theswiftSafeName
function actually changed the output to determine if a raw value is required.My thinking around this is that the
String.safeForSwiftCode
function already has the necessary information to determine if it made changes, without needing to perform string comparisons.Knowing how many places use the closure on the context, I think a purely additive (opt-in) change should be utilised to allow any code that is interested in if there was a change to call an alternative closure. A very quick test I did for this looked like:
It would then allow the changes in my PR to use the bool for the varied logic
What do you think?
The text was updated successfully, but these errors were encountered: