If you get attacked by a force smaller than your own, or about the same size as your own, and you want to fight, go ahead and defend.
If the force attacking you is much larger than yours, you are better off NOT defending. The reason for this is that you will lose all of your resources and all of your troops in the attack. By not defending you will still lose your resources, but you will save your troops.
When not playing, your forces should generally NOT be defending. If someone scouts you, they will not be able to see how many troops you have. If they DO decide to attack you, it's still better to not have them defending for the reason stated above.