Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Most small values in programs are positive (lengths, counts, etc), so it's possible to write a value to something that should be initialised to a constant by mistake, and you'll never know, since the ranges overlap.

But if you use negative values, and you write a positive value, when you see it, you'll know.

Of course this is a very trivial thing, but it helped me many times.

Sometimes it's better to use typed constants with a custom type, then the Go type system will help you and not let you assign things of a different type. Note that I only said sometimes, in many cases it's better to use untyped constants (or use primitive types rather than custom integers), and positive integers because you can calculate the constant corresponds to some value from a delta, or from some other criteria, like here:

https://github.com/golang/go/blob/54789eff385780c54254f822e0...

with the definitions of constants here:

https://github.com/golang/go/blob/54789eff385780c54254f822e0...

Another thing useful when debugging is using constants with non-overlapping peculiar ranges, rather than small integers, then simply by seeing a value in a debugger (or in a print of a value without a String() method), you'll know where that comes from.



Thanks.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: