What are some good books or online resources for learning how to write better code? I am looking for actual code examples demonstrating best practices for someone who already has programming experience but who wants to improve. Examples of topics I am looking to see covered would be whether a function should always include a return, when it makes sense to use try-catch instead of handling bad input with control flow, or when to use different approaches for handling invalid input.
In addition to the course that Alex mentioned, one very specific tactic I can recommend:
If you haven't already, I recommend going through my code quality course: [Course] Level Up Your Code Quality As A Software Engineer
As the course digs into, there's no such thing as objectively good code and anything claiming to be "best practice" needs to be taken with a grain of salt. My overall approach to code is "When in Rome, do as the Romans do". So for all the scenarios you mentioned, you should just figure out what your teammates are doing and then do that. Consistency is a huge component behind readability, and readability lets engineers iterate on code faster to improve it for the end-user (the ultimate goal of any and all code).
At the end of the day, coding is all about trade-offs. There are very, very few scenarios where it's 100% correct to always do a certain thing. If you want to understand the trade-offs, feel free to start discussions in the Taro forum with code samples (we use Markdown so this is supported). I'm personally happy to chime in on any of those.
Here's an example of a code snippet:
try {
// Some crazy code here
} catch (exception: ScaryException) {
// Should I be doing this instead of using if statements?
}