Barrels are one the main components of the firearm affecting its performance. Generally a barrel is not changed from the same gun and swapped back unless for particular types of use such changing a smooth bore shotgun barrel to a rifled barrel when hunting with slugs and back to smoothbore when hunting with shot. A pistol barrel may be changed to an aftermarket match grade barrel for increased accuracy and performance or to a threaded barrel so that a silencer or muzzle device can be used with the same being true for most rifles. AR style rifles or models of guns that can be built from different components allow for selection/customization for particular uses based on the size of the barrel. For handguns, such as the full size Glock 17’s, typically used for home defense, target shooting or law enforcement primary sidearm use, longer barrel tends to provide better accuracy, less recoil and more control, when compared to the subcompact Glock 26’s, typically used for concealed carry or law enforcement backup sidearm use, shorter barrel provides easier concealment.
Barrels for long guns or rifles can directly affect how the firearm is classified. This means that any Rifle barrel with or without a permanently affixed muzzle device with an overall length shorter than 16 inches, would be classified as a short barrel rifle (SBR), an NFA item, requiring BATFE approval along with a $200 tax stamp. The same would include shotguns with barrel lengths under 18 inches, as they would be classified as short barrel shotguns (SBS).
Generally a longer barrel provides better accuracy and potentially increased velocity/performance, however there are many variables that affect these particular characteristics, such as ammunition used, rifling type/style, materials used and overall quality of manufacturing.