Cheat codes were invented as a debug feature for use by development and QA staff during testing.
During normal play you have to spend potentially all game waiting to get your hands on the full array of weapons, items, abilities, or characters, and you may be limited in your capacity to use them by a variety of resources like ammo and health. You’re limited in your capacity to sustain hits from enemy attacks, and you’re bound to complete content in sequential order, which can make it time-consuming to reach and practice later levels.
Cheats give developers shortcuts for those things.
A cheat code can give you all the items and equipment in the game, allowing you to test them at-will regardless of progression; that’s great, because a weapon might be implemented way ahead of any opportunity to physically collect it. Infinite ammo or MP lets you endlessly test them in numerous use-cases without penalty.
A cheat code can make you invincible, permitting you to sustain an infinite number of enemy attacks, thus allowing you to experiment and play with the AI without interruptions or restarts.
A cheat code can skip you to whatever level you want to test, permitting you to do focused barrages of tests against potentially late-game content to discern bugs with those specific sections of the game. “No clip” cheats and invincibility let you skip to specific sections free of hassle.
A cheat code can give you infinite ammo and resources that trivialize combat, or even let you insta-kill enemies, thus making skips of sections that don’t need testing even quicker. Sometimes the thing you’re testing is just watching monsters die and making sure death works correctly. For that matter you might be ensuring that a cutscene that plays when specific enemies are killed works correctly — why make triggering it any more of a hassle than it needs to be?
These are just a few examples of what cheats can do to help speed testing and iteration along. There’s always a phase of testing that demands repeated playthroughs of the entire game from start to finish to ensure that it’s completable without any show-stopping bugs or crashes, but before that every individual element has to be verified as working correctly, and shortcuts like these are essential to making that process expedient. There is necessarily a need to create menus or toggles for them ingame, as QA often tests on standalone builds for the target platform rather than in a development environment where they might have an editor or where files can be edited.
It just happened that they’re also fun for players to screw around with, so they also get left in as fun secrets to find or unlock.
One of the earliest known examples of this type of cheat is the Konami Code, created in 1986 by Konami developer Kazuhisa Hashimoto as he worked on porting the 1985 arcade game Gradius for use on the Nintendo Entertainment System. Hashimoto is quoted as saying “The arcade version of Gradius is really difficult, right? I never played it that much, and there was no way I could finish the game, so I inserted the so-called Konami code.”