Barao's win streak was insane and in his early UFC career had wins over Eddie Wineland, Michael McDonald, and Urijah Faber twice. His fall from grace was pretty bad. Dude's on a 4 fight losing steak and he's only 32.
Cody sorta got shot straight to the title picture. His only significant wins were over Thomas Almeida and Takeya Mizugaki, neither of which were exactly "elite" (Almeida is currently #15 and Mizugaki had a long inconsistent streak and lost 7 out of 10 fights from his loss to Cruz onwards). Styles make fights and Cody's was perfectly tailored to beat Cruz. His speed, slick movement and ability to get under Cruz's skin left Cruz, usually a master of defense, reaching on his punches and visibly frustrated. Cody losing his fights afterwards just speaks to his inexperience and inability to gameplan effectively. His fight against Cruz was "the perfect storm".
Barao's is much worse, as I believe his time as a top bantamweight was essentially over following the Dillishaw loss. Cody's career is just getting started and is definitely salvageable with the right adjustments.