I mean, yeah he's on the side of the good guys, but he's also a complete dick, and he only betrayed Voldemort after Lilly was killed
It's funny because it was obviously intended that we read the books and go "Ooh, Snape was this good and noble guy all along who did everything he did for his love of Harry's mom, Harry should totally name one of his kids after this guy" but
as written Snape was a total "nice guy".
He got rejected by his high school crush and decided that a totally rational response to this was to be a raving dick
for the rest of his life, get a job as a teacher and take out his frustrations on his students. He was probably a regular poster on wizard inc*l forums asking why women choose assholes over nice guys like him, the guy who made bullying children his entire personality.
It's another example of Rowling papering over badly thought-out world-building by just telling the reader what they should think (see also: slavery is good actually), but they're childrens books, what do you expect.