On Friday, August 17, 2018 at 5:56:05 PM UTC+8, Liam wrote:
>
> People have a wide range of perceptive and cognitive abilities. People
> also have difficulty imagining the very different abilities that others
> possess.
>
>
I find constant vertical scanning and scrolling to be distracting and thus
> inefficient. So I won't use a tool that adds line breaks to my code, and
> I'd benefit from a one-line switch.
>
> My apology for shoving everyone into drinking tea hardly, forgotten to
realize there are coffee lovers in the room too.
This is a good compromise, less cryptic than ?:
>
> v = if t a; else b // else can be on next line
> v = if (t) a; else b // parens isolate the test if required
>
> I'm willing to compromise only as far as this pattern, standing on tea
sides for coffee folks:
v = b ; if (t) { v = a }
v = b ; if (t) { a }
v = b ; if t { a }
v = b ; if t { v = a }
Here are the tests, based on comparing the readability, between the pattern
vs. the Ternary:
// Oneline
v := "blue"; if data == nil { v = "red" }
v := data == nil ? "red" : "blue"
v := (data == nil) ? "red" : "blue"
// multiple lines
status := "blue"; if data == nil { status = "red" }
latitude := device.gps("lat"); if data == nil { latitude := "000.00000" }
longitude := device.gps("long"); if data == nil { latitude := "000.00000" }
status := data == nil ? "blue" : "red"
latitude := data == nil ? device.gps("lat") : "000.00000"
longitude := data == nil ? device.gps("long") : "000.00000"
// nested conditions oneline
v := "blue"; if (data == nil && (device == nil || wifi == nil)) { v = "red"
}
v := data == (data == nil && (device == nil || wifi == nil)) ? "red" :
"blue"
// nested conditions multiple lines
status := "blue"; if (data == nil && (bluetooth == nil ||
wifi == nil)) { status = "red" }
latitude := device.gps("lat"); if (data == nil || accuracy < 30) {
latitude := "000.00000" }
longitude := device.gps("long"); if (data == nil || accuracy < 30) {
latitude := "000.00000" }
status := (data == nil && (bluetooth == nil || wifi == nil) ? "blue" :
"red"
latitude := (data == nil || device.gps.accuracy() < 30) ? device.gps(
"lat") : "000.00000"
longitude := (data == nil || device.gps.accuracy() < 30) ? device.gps(
"long") : "000.00000"
Rationale:
1. Maintain clarity between control path and data path.
As I said earlier, I still see it is a costly feature vs vertical only
source codes. I just can't see the value pursuing such direction. There
must be a strong solid reason the developers who developed go did not
implement such feature at the first place. Will I use it? No. If the core
developers agreed to pursue, I can't object either but I do my part raising
my past experience with that scary magical "?".
> Note that go fmt allows this clumsy construct
>
> if err = pkg.ActionItem(); err != nil { // which op is tested?
> return err
> }
>
> While exploding this clear one
>
> err = pkg.ActionItem()
> if err != nil { return err }
>
>
because this is clearer?
err := pkg.ActionItem()
if err != nil {
return err
}
As far as I understand, to simplify, the above, line can be re-written to:
if err = pkg.ActionItem(); err != nil {
return err
}
Which is a clear 2 lines statement, error checking only. Anyway, I use the
former. Again, I'm a tea folk. It's not a rule.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.