Change nested ifs to a switch

Signed-off-by: Trevor Whitney <trevorjwhitney@gmail.com>
This commit is contained in:
Trevor Whitney 2023-03-13 14:31:49 -06:00
parent b4e324f637
commit e3513d1dd2
No known key found for this signature in database
GPG key ID: 78F930867F302694

View file

@ -192,23 +192,28 @@ func (h *FloatHistogram) Scale(factor float64) *FloatHistogram {
// //
// This method returns a pointer to the receiving histogram for convenience. // This method returns a pointer to the receiving histogram for convenience.
func (h *FloatHistogram) Add(other *FloatHistogram) *FloatHistogram { func (h *FloatHistogram) Add(other *FloatHistogram) *FloatHistogram {
if other.CounterResetHint != h.CounterResetHint { switch {
// The outcome of adding an increment to a guage histogram will always be a GaugeType case other.CounterResetHint == h.CounterResetHint:
if other.CounterResetHint == GaugeType && h.CounterResetHint != GaugeType { // Adding apples to apples, all good. No need to change anything.
h.CounterResetHint = GaugeType case h.CounterResetHint == GaugeType:
} // Adding something else to a gauge. That's probably OK. Outcome is a gauge.
// Nothing to do since the receiver is already marked as gauge.
// This could be legitime if the caller knows what they are doing, but the resulting hint case other.CounterResetHint == GaugeType:
// must be UnknownCounterReset. // Similar to before, but this time the receiver is "something else" and we have to change it to gauge.
if other.CounterResetHint == UnknownCounterReset && h.CounterResetHint != GaugeType { h.CounterResetHint = GaugeType
h.CounterResetHint = UnknownCounterReset case h.CounterResetHint == UnknownCounterReset:
} // With the receiver's CounterResetHint being "unknown", this could still be legitimate
// if the caller knows what they are doing. Outcome is then again "unknown".
// TODO(trevorwhitney): this leaves CounterReset and NotCounterReset. If we have mismatch of // No need to do anything since the receiver's CounterResetHint is already "unknown".
// these hints, that cannot be right, and we should raise a warning when possible. case other.CounterResetHint == UnknownCounterReset:
// if other.CounterResetHint == CounterReset && h.CounterResetHint == NotCounterReset || // Similar to before, but now we have to set the receiver's CounterResetHint to "unknown".
// other.CounterResetHint == NotCounterReset && h.CounterResetHint == CounterReset { h.CounterResetHint = UnknownCounterReset
// } default:
// All other cases shouldn't actually happen.
// They are a direct collision of CounterReset and NotCounterReset.
// Conservatively set the CounterResetHint to "unknown" and isse a warning.
h.CounterResetHint = UnknownCounterReset
// TODO(trevorwhitney): Actually issue the warning as soon as the plumbing for it is in place
} }
otherZeroCount := h.reconcileZeroBuckets(other) otherZeroCount := h.reconcileZeroBuckets(other)
@ -433,9 +438,9 @@ func (h *FloatHistogram) Compact(maxEmptyBuckets int) *FloatHistogram {
// of observations, but NOT the sum of observations) is smaller in the receiving // of observations, but NOT the sum of observations) is smaller in the receiving
// histogram compared to the previous histogram. Otherwise, it returns false. // histogram compared to the previous histogram. Otherwise, it returns false.
// //
// This method will shortcut to true if a CounterReset is detected, and shortcut // This method will shortcut to true if a CounterReset is detected, and shortcut
// to false if NotCounterReset is detected. Otherwise it will do the work to detect // to false if NotCounterReset is detected. Otherwise it will do the work to detect
// a reset. // a reset.
// //
// Special behavior in case the Schema or the ZeroThreshold are not the same in // Special behavior in case the Schema or the ZeroThreshold are not the same in
// both histograms: // both histograms: