lostluck commented on a change in pull request #15136:
URL: https://github.com/apache/beam/pull/15136#discussion_r667145311



##########
File path: sdks/go/pkg/beam/testing/passert/floats.go
##########
@@ -0,0 +1,72 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package passert
+
+import (
+       "fmt"
+       "reflect"
+       "sort"
+       "strings"
+
+       "github.com/apache/beam/sdks/go/pkg/beam"
+       "github.com/apache/beam/sdks/go/pkg/beam/core/util/reflectx"
+       "github.com/apache/beam/sdks/go/pkg/beam/internal/errors"
+)
+
+// AllWithinBounds checks that a PCollection of numeric types is within the 
bounds
+// [lo, high]. Checks for case where bounds are flipped and swaps them so the 
bounds
+// passed to the doFn are always lo <= hi.
+func AllWithinBounds(s beam.Scope, col beam.PCollection, lo, hi float64) {
+       t := beam.ValidateNonCompositeType(col)
+       if !reflectx.IsNumber(t.Type()) || reflectx.IsComplex(t.Type()) {
+               panic(fmt.Sprintf("type must be a non-complex number: %v", t))
+       }
+       if lo > hi {
+               lo, hi = hi, lo
+       }
+       s = s.Scope(fmt.Sprintf("passert.AllWithinBounds([%v, %v])", lo, hi))
+       beam.ParDo0(s, &boundsFn{lo: lo, hi: hi}, beam.Impulse(s), 
beam.SideInput{Input: col})
+}
+
+type boundsFn struct {
+       lo, hi float64
+}
+
+func (f *boundsFn) ProcessElement(_ []byte, col func(*beam.T) bool) error {
+       var tooLow, tooHigh []float64
+       var input beam.T
+       for col(&input) {
+               val := 
reflect.ValueOf(input.(interface{})).Convert(reflectx.Float64).Interface().(float64)
+               if val < f.lo {
+                       tooLow = append(tooLow, val)
+               } else if val > f.hi {
+                       tooHigh = append(tooHigh, val)
+               }
+       }
+       if len(tooLow)+len(tooHigh) != 0 {
+               errorStrings := []string{}
+               if len(tooLow) != 0 {
+                       sort.Float64s(tooLow)
+                       errorStrings = append(errorStrings, fmt.Sprintf("values 
below minimum value %v: %v", f.lo, tooLow))
+               }
+               if len(tooHigh) != 0 {
+                       sort.Float64s(tooHigh)
+                       errorStrings = append(errorStrings, fmt.Sprintf("values 
above maximum value %v: %v", f.hi, tooHigh))
+               }
+               return errors.New(strings.Join(errorStrings, "\n"))
+       }
+       return nil

Review comment:
       Here I'd reverse the cases so the indentation is reduced. If there are 
no errors, return out early. Then the 10 line stretch of indented error 
construction can be unindented a level.
   
   This is a kind of subtle interaction between the "indent error path" rule of 
thumb, and the "keep indentation low" rule. Readability is about clarity and 
how much the reader needs to keep in mind when they skip over other blocks. 
   
   Returning early in the "there's no error case" is a pretty clear signal that 
the happy path is done, and there's nothing else to keep in mind when reading 
it.  But put another way, if the happy path after the error handling were 
longer, keeping it as is is probably better for readability.
   
   There's the archaic rule about avoiding making unnecessary diffs to keep the 
"blame/credit" clean, but Readability concerns are about longer term 
maintenance. It's easier to maintain code that is easier to read and think 
about, and reducing scopes where possible, is a fine way of doing that.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to