losvedir / transit-lang-cmp

Programming language comparison by reimplementing the same transit data app
MIT License
426 stars 31 forks source link

Simplify Go code #17

Open coder543 opened 2 years ago

coder543 commented 2 years ago

I don't anticipate that this will affect the performance at all, but it will remove a lot of conditionals that appear redundant.

In Go, append(x, someElement), where x is a nil slice, will create a new slice to contain someElement, it won't explode. Similarly, accessing a non-existent element of a map will return the zero value, which will be nil for a slice like []int, it won't explode either. Iterating or checking the length of a nil slice will work fine too, it'll just be the equivalent of doing the same operations to an empty slice.

I haven't actually tested this code because I didn't feel like setting up the whole environment, but I think it should work.

in my opinion, it's also more idiomatic to construct values once the fields are all known, which you were already doing sometimes, so I added a commit to do that in one other place.