In Chess, the general rule is to castle early. But there are exceptions to this rule, as all rules have. Delay castling, at times, if the position permits.
When you delay the castling you can assess and plan better. Also, castling early may attract some forceful attack on your King, at times.
So, “Castle when you must; not when you can.”