When and why should I move the pawns in front of a castled/non-castled King? I have always struggled with this and I believe there is some rule that someone defined at some point, but I am not sure who (Maybe it was Steinitz).

When and why should I move my pawns in front of my castled or non-castled King?

First, I would assume it matters if the King is castled or not?

I will state my logic behind this and someone with more knowledge can let me know if it is flawed:


I usually move a pawn so my Knight or some other piece does not get pinned.
I usually move a pawn to prevent my opponent from bringing his bishop down to pin my knight or some other piece to my king.
I usually move a pawn so the bishop or some other piece(maybe queen can't put King in check)
I usually don't like to do this because it weakens the king/queen-side depending on where I want to castle.


Let's look at some examples:

In the first scenario,  I am thinking black might move 4...Bb4, which would put my King in check, so to prevent this, I will play 4. a3.  Is this the proper move or is there a better move?  

    [fen ""]

    1. e4 e5 2. Nf3 Nc6 3. d4 exd4 4. a3 Bb4


In my second scenario, let's assume, my Knight was already on c3, so to prevent black from 4...Bb4, I would again play 4. a3, to prevent the future pin if I wanted to move the d2 pawn.

 [fen ""]

 1. e4 e5 2. Nf3 Nc6 3. Nc3 Nf6 4. a3 Bb4


In my third scenario, let's assume I moved by d2 pawn, so black could check or pin my knight with ...Bb4, so to prevent black from 4...Bb4, I would again play 4. a3, to prevent the future pin if I wanted to move the d2 pawn.

 [fen ""]

 1. d4 d5 2. Nf3 Nc6 3. Nc3 e6 4. a3 Bb4


Let's assume that black does succeed in checking my King or pinning my knight to my King, if my King is in check, is it better to do c3 or Bd2.  If my Knight is pinning, is it better to do a3 to try and force the opponent to either exchange or move his bishop or Bd2 in the event if he takes my Knight, I will just recapture with my Bishop.

All of the above assume my King is not castled, but what if it was, my assumption is that I would not want to move any pawns in front of the castled king unless necessary, but I am not sure of when these times would apply.
You should only make a move like a3 or h3 when the pin is so annoying that the loss of a tempo is a fair tradeoff.

Generally speaking, bishop pins are not that big of a deal.  The pin can be broken by moving a bishop (Be2 or Bd2), the knight can be defended by the other knight (Nbd2 or Nge2), or the pinned piece can move away (for white, frequently Qd3 will both break the pin and still defend the knight on f3).  

If this isn't possible, then a3 or h3 can enter into consideration, but even still, the doubled pawns might not be such a big deal.  Especially for white, the move a3 is hardly ever played.  If black takes on c3, then the extra c pawn helps white far more than it hurts (additional control of the d4 square, half open b file, etc.).  h3 is far more common, especially in the Open game.  In this case, Bg4 is prevented, yes, but moves like Ng4 are also prevented, and someday white might expand with g4.  

If the bishop is already on g4 or b4, then sometimes a3 or h3 are decent moves because they will win a tempo or force an exchange of bishop for knight.  Black will nearly always play h6 after white plays Bg5, for example.  The main reason is that the bishop has far less scope on h4 than it does on g5 (cannot return to e3 or d2, no queen battery is possible).  h3 is frequently played with a similar idea for white.

To summarize, a3 is almost always a weak move for white (a6 is a different story, it can be useful for black in many cases), and these pawn moves should only be played prophylactically when the pin is so bad that the loss of a tempo is more than offset by avoiding the pin.