How to handle "Why did you do this?" "I did this because senior told me to do this" ?

Honestly, this is why I hate when senior developers write bad code. I remember a very example when my linter caught bad code. IIRC, it was instance methods writing to static fields. It caught my attention because my coworker was assigned to add functionality to that part and I reviewed the PR. I hate this because people copy code assuming it's what's correct. Heck, I have even seen senior developers I respect copy code in the codebase. Like literally just copy and paste a whole class.

Now, if someone asked me about the code, how can I tell them why I did this?

I would just be honest that your TL told you to do it just for the time being. It's the truth.

And in general, how do you handle this type of situations?

If you're talking about the junior engineer learning bad stuff, in the event you can't write good enough code due to constraints, you should explain your exact reasoning when you see patterns from bad code copied.

/r/cscareerquestions Thread