I originally wrote this as an answer to a question on Quora.
“I joined a company as a new grad and currently working with two mid level and one senior designer. It’s a chip design company. One of my co-worker’s code is very badly written and I am pretty sure there are many other good ways to implement the same thing. Badly written code bugs me a lot and I’m tempted to change/improve it. Will it look bad if I go ahead and change his code? Or should I ask his permission beforehand ? I’m not sure what will be a good move as I’m comparatively new in industry. I don’t wanna look arrogant!”
Yes. It’s also rude to write bad code.
The best way to handle that is a code review. It’s not necessary to have anything formal – just spend a few minutes going over the code together so you can point out what had issues and why.
Just changing the code without communication will cause two problems: The original writer is likely to resent it, and the original writer won’t learn anything and will keep doing things the same way. There’s also a risk that you don’t fully understand the intent or scope of the changes and your “fixes” might break something.
Don’t make it adversarial or approach it in a way that puts the other developer on the defensive. That will make them non-receptive to the conversation, and the goal is to improve the code and the developer both now and in the future.