Yes!
In fact it is the best practice to think about how you can optimize your code before hand. I have been there where we write our code to just finish the sprint and then think will optimize it later...not gonna happen!
What works for me is I read all the rules listed in the Sonar Cube runner with example and wrote the code by keeping in midn that it needs to pass those rules.