Welcome and thank you for contributing! 🎉
Readable > Useful > High performance but poorly readable
-
All comments use English.
-
Reduce Confusing abbreviations.
Bad
dl := &net.Dialer{}
Good
dialer := &net.Dialer{}
-
If a comment is meaningless, it is meaningless like this sentence, which is redundant.
-
Readable code from naming, not comments.
-
Use constant as much as possible.
Bad
import (
"net"
N "github.com/sagernet/sing/common/network"
)
func defaultDNS() (net.Conn, error) {
return net.Dial(N.NetworkUDP, "8.8.8.8:53") // Google DNS
}
Good
import (
"net"
N "github.com/sagernet/sing/common/network"
)
func defaultDNS() (net.Conn, error) {
const googleDNS = "8.8.8.8:53"
return net.Dial(N.NetworkUDP, googleDNS)
}
Our style use name to tell reader what's this mean.
-
Use
make fmt_go
andmake test_go
before committing. -
Create unit test as much as possible.
-
Make writing documentions a habbit.
- Reduce
forEach
.
Bad
val numberList = listOf(1, 2, 3)
numberList.forEach(::println)
Good
val numberList = listOf(1, 2, 3)
for (number in numberList) {
println(number)
}
also
sometimes better thanapply
.
Bad
lateinit var textView: TextView
val isVisible = true
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
textView = findViewByID(R.id.textView).apply {
this@apply.isVisible = isVisible
}
}
Good
lateinit var textView: TextView
val isVisible = true
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
textView = findViewByID(R.id.textView).also {
it.isVisible = isVisible
}
}