Am going to speak from a missile designer's perspective, not because I was a missile designer, but because of due to certain aspects of my (once) job, I had to know something about designing missiles, specifically air-air missiles.
Q: Why do we have categories of 'long', 'middle', and 'short' range missiles ?
Before we answer that question, there is one thing we must know that is COMMON to every missile, whether it is air-air, air-ground, or ICBMs, which is surface-surface.
That common thing is that for a period of time starting immediately after the missile exited its restrains, the carriage rail or the underground silo, its sensor package is INACTIVE. During this time, the missile, while in free flight, is essentially blind. The missile maybe designed to receive target updates from the parent, but that info does not occur until a certain period of time.
The reason is very simple: Safety.
We do not want to kill the launcher, whether it is an aircraft or the underground silo.
I, the missile, do not want to endanger the parent launcher while I am severing restrains and controls. During these milliseconds, I will leave the launcher in as stable flight as possible. So during those same milliseconds, I will not allow the sensor package to give me target information, no matter how bad or good that information maybe. It does not matter even if target information came from the parent. I will not allow any attributes of my physical state, such as vibrations from my rocket motor and/or from aerodynamic stresses on my body, to endanger the parent. I will fly in as stable a flight as possible.
Everything in the above paragraph are represented by mathematics, which are then translated into computer languages, which are known as 'flight control and guidance laws'.
So then...Why do we have categories of 'long', 'middle', and 'short' range missiles ?
In interception laws, everything must be exploited to their fullest. If the target is 'long', no matter how arbitrary is this 'long' distance figure, the missile should have an altitude advantage. So we program the missile to gain some altitude BEFORE its sensor package can have influence. The flight profile would have a sharp nose up to gain altitude, a stable burn in a plateau, then sensor guidance begins.
Skip the 'medium' range and jump to the 'short' range missile.
What happens for the 'short' range missile is that there is no gain in altitude. The assumption is that the target is so close that high altitude gain to get that 'big picture' perspective, then gravity assist to gain speed are meaningless. It is best that, as soon as I am safely away from the parent, I am going to turn on my sensor and begins accepting its guidance.
A: The post launch safety margin is common to all missile designs. What the missile DOES after this time and distance is what create the categories of 'long', 'medium', and 'short' range air-air missile.
Will there be a situation where the target is so close, once that safety margin is passed, that the target is too close that the missile cannot adequately detect and maneuvers to intercept ? Yes, and that is where the gun comes in.
Modern avionics are sophisticated enough to warn the pilot that even his specifically designed missiles have reduced odds of success and that reduction is so great that not even the computer cannot compensate for target variations. Hence, the gun.
With the gun, the fighter aircraft basically said to the pilot: 'F-kit. I cannot handle this. Shoot him yourself.'
Now it falls back to Basic Fighter Maneuvers (BFM). This is where pilot training in BFM shines. Either the pilot survives to paint his jet, or he dies. This is why the USAF have Fighter Weapons School, the USN have 'Top Gun', and everybody tries to go to Red Flag.
This is why selecting your loadout is so time consuming. You have a finite hardpoint count. What is your mission ? If you are required to VISUALLY identify your target in an interception, then 'long' and 'medium' missiles are useless. Visual ID pretty much put both into each other's short range missiles and gun range. On the other hand,if Command says this part of the sky is hostile, then a 'long' and 'medium' range loadout make sense. As soon as you make radar contact and if the IFF response is not valid, you shoot at the longest distance your guidance suggest. With modern fighters backed up by sophisticated radars, the gun is the weapon of final resort.