What's wrong with this code?

March 28, 2025

Today I want to share a bit of code I found in the wild, and challenge you to improve it. First off, let me make it clear that this code “works”. It has unit tests, and the tests all pass correctly. So I’m not looking for bugs, per se.

func decodeString(encoded string) ([]byte, error) {
	var decodedBytes []byte
	var err error

	trimmed := strings.TrimRight(encoded, "=")
	decodedBytes, err = base64.StdEncoding.DecodeString(trimmed)
	if err != nil {
		decodedBytes, err = base64.URLEncoding.DecodeString(trimmed)
		if err != nil {
			decodedBytes, err = base64.RawURLEncoding.DecodeString(trimmed)
			if err !=nil {
				return decodedBytes, nil
			}
		}
	}
	return decodedBytes, err
}

How would you improve this code?

Hit Reply, and let me know. On Monday, I’ll give my answer, and will include some user answers as well (if you don’t want your name included, let me know when you hit reply!)


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Get daily content like this in your inbox!

Subscribe