TL;DR: Barney sometimes shows some vulnerability (?) in his own way (like needing reassurance he’s someone’s best friend, etc) with the gang and they always just brush him off and treat him badly when he does, which just reinforces negative beliefs he has about himself that make him act the way he does. Definitely related to childhood trauma he has related to absent parent(s)
To preface, I don’t have strong love/hate feelings towards the character overall. I enjoy the main plot lines of his (his childhood/mom/brother, everything with his dad and confronting him later, etc) and find all the playboy stuff cringey.
I’m watching S2E3 “Brunch” where Ted’s parents come to town and meet everyone, and I honestly felt bad for Barney. Barney shows up before Ted’s parents because he’s excited to meet Ted’s parents after all they’ve heard about him, and Ted says they know nothing about him and are mainly coming to meet Robin. Then he hits him with the classic “Marshall’s my best friend” and adds “Robin’s the most important person in my life”.
I get he’s not exactly take home to parents material, not that that stops Barney from altering his personality to please them, but at this point he’s been friends with Ted for at least a few years. It’d be kind of a slap in the face to hear that imo.
On that note, I definitely get why he has this constant need for claiming himself as Ted’s best friend and in general acts the way he does after his childhood (not having a dad around, a mom who was at times mentally absent, etc). Not that that’s an excuse that he acts like an ass a lot of times, but I tend to enjoy thinking about why someone acts the way they do