1. Is your boss misinformed/clueless/you're misunderstanding?
2. Does your boss not care about you?
#1 can be cleared up, as others here say, by better communication and by making sure your boss knows your accomplishments.
If answer to #2 is "boss doesn't care" you'll want to start thinking about new job, or new team at least, because this is just a symptom of a deeper problem.
If this happens again, then you can be little more direct.
It would also behoove you to mention your accomplishments in passing to your boss during a casual chat in the morning while you are talking about weather or current events. Bosses for most parts don't have all the minute details of who is doing what.
Even if they are well meaning and fair, eventually they will hit a comprehension limit. At that point arbitration for any technical debate should be escalated to a board of suitably qualified people, but they will be unwilling or literally unable to do it. Eventually as a consequence bad technical decisions will be made.
That's the best case scenario. In the worst case scenario you're already undergoing, your boss has no clue who's the better dev or why in your team.
So, for now ... I say to keep the peace - take it on the chin. Good learning experience for you. You did not properly credit yourself and made the way for this to happen. Note that you must be balanced in life too and not toot your horn too loudly. Have balance, allow others to succeed and get credit too. Maybe you saw the sample code on stack-overflow and refactored it. We all learn from others anyway.
The real problem here is the pat on the back from the boss, not the code itself which you saw as trivial anyway. Be aware of how this company operates. However, don't stop contributing! That will cause you to die inside. Your contributions are great and will help you to grow. So, no matter who gets the credit, keep doing it so that you'll be better off when you leave this place.
If there are no mitigating circumstances here, like "actually we worked together but I did most of it". Then it should be a simple matter to talk to the boss and get to the bottom of how it was misattributed. No accusations, no assumptions but a fact finding mission. The goal here is to find out how it happened so you can avoid it in the future, and as a secondary bonus it will obviously alert your boss to the fact you are the real author.
If people are able to take credit for your work it indicates a larger problem with your visibility in the organization and it should be dealt with swiftly if you care about your career.
Also, this is an example of having a bad boss and is one of the reasons technical people generally prefer working for other technical people.
In the big picture these things don't matter a whole lot, I wouldn't take the time to discuss it with your boss any further.
Maybe talk to your teammate and see if he was trying to take credit for it or if it just happened to look that way.
Try to bring up more what you are working on and ideas you are executing on with your team and boss in the future.
kudos are nice but very minor in the big scheme of things. You are all cogs in a machine to your company and boss. So I wouldn't let it get you down and it's not worth making waves over.
If you want to 'compete', the way to play the game is to promote yourself as most of the time, no one else is going to. This is obviously what your colleague is up to.
I used to get mad about the same thing happening to me and it happened a lot. After talking to different people about ways to manage it, I decided to come to terms with it because I wasn't that interested in the alternative (self-promotion). Now I take comfort from the fact if they have to take credit for it, they are obviously worried about something that I am not.
Significance, Contribution, Growth are core Human Needs.
Was this a big deal or NOT?
Can you quantify the improvement in your work?
Decide what you want -
Guy Kawasaki — 'If you don't toot your own horn, don't complain that there's no music.'
the takeaway would be that you should promote yourself and your work preemptively.