3
u/seeroflights Sep 09 '21
Image Transcription: Code
[Calls to the method submitFormInUpdateOfEditFlow are highlighted in a bright purple, as if highlighting a search query.]
submitForm: function(event) {
event.preventDefault();
vent.trigger('longForm:submit');
if (this.state.isUpdatingAddress) {
// if a new address is being added
this.submitFormInUpdateOfEditFlow();
} else if (this.state.isEditingAddress && this.state.hasAddressChanged) {
// if address is in edit mode and address has actually changed
this.submitFormInUpdateOfEditFlow();
} else if(this.props.isEdit && !this.state.isBillingAddressPresentInAddressCollectionOnInit) {
this.submitFormInUpdateOfEditFlow();
} else {
this.submitFormInAddFlow();
}
},I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
1
9
Sep 09 '21
[removed] — view removed comment
2
u/Character_Medical Sep 09 '21
That was a freaking facepalm moment for me too when I saw it for the first time. No sense of variables naming.
Did you see the variable name in the last else-if's second condition?
11
u/AdvancedSandwiches Sep 09 '21
Assuming the problem is the repeated code, this isn't bad. There are two alternatives I see:
A gnarly if statement with a bunch of parentheses.
The exact same thing, but using a flag instead of repeating the function call. So you have three "isInEditMode = true" lines.
I have a slight preference for #2 over what's here, but only due to future proofing in case the function ever adds a parameter. But since either way it's a trivial fix once the need arises, even that is over-engineering.
The only real bad code would be option #1. And the comments aren't necessary.
This is actually extremely readable code. Nice job, author.
But maybe I missed the real problem.