Fire in ranks really was one of the stupiest tactics on the battlefield. I dunno why it was even used for such a long time.
I believe it was mainly used to make cavalry unable to attack you. If infantry stood very close together it was almost impossible to make a horse charge into them. Horses aren't stupid animals and wont charge something it believes it can't run through, especially if that mass of humans are pointing sharp objects (spears/pikes) or loud sticks that shoots fire (guns). The fire by rank was then the most efficient way to make as many people able to fire while staying in a concentrated mass. Ofc, it made them more vulnerable to enemy fire and artillery, but it was usually better to have a random chance to be shot by the enemy, than
definitely be ridden down by the enemy cavalry. This is why the skirmishers and other light infantry (those who DIDN'T stand in a centred mass) greatest threat was cavalry. Skirmishers could easily defeat a larger force of normal infantry, and artillery was even less effective, but cavalry would cut them to pieces if they could get to them.