Most people who went to undergrad (at least the ones in the US) have college loans, so the undergrad debt is unfortunately unavoidable for most (unless they have well-off parents who decided to foot their college bills) regardless of whether you are a med student or not. Actually, it's because of the undergrad loans that only relatively well-off students can afford to go to med school in the US (I wrote another comment related to that in this thread).

For the opportunity cost part, I have to agree with you to an extent. But let me present my rough calculation. I can probably make ~70-120K/year for 10 years before I reach the age of 32 (assuming I don't get laid off in the meantime and assuming I'm not lucky enough to be employed by FAANG). To make it simple, let's assume make an average of 95K/year (average of 70K+120K) for 10 years starting from when I graduate from college. So my take home pay in Los Angeles would be 65K/year. Suppose I can live relatively frugal in LA and save ~30K/year for those 10 years, my investments would have probably grown to about 415K in 10 years (assuming there is no recession and the returns for my investment averages 7%/year; https://www.investor.gov/financial-tools-calculators/calcula...). That is NOT a lot of head-start for those who chose to do programming (or cannot afford to go to med school).

Remember that programmers are actually one of the better-paid professionals among those who graduate from college every year. Also you have to keep in mind that a lot professions have issues keeping their jobs when they get older (e.g., old programmers tend to have a difficult time finding programming jobs while maintaining their senior salaries unless they are really good at what they do; employers will prefer find young, fresh programmers who are willing to be paid less and can spend a lot more time at work than older programmers, but that's for another discussion). Doctors don't have that problem (at least for now because the supply of doctors is artificially limited in the US).

Also, if you are really hard-working (and greedy) as a doctor, you can take two jobs (as a hospitalist or attending) at once. I know two doctors (from my country), who live in CA suburb and make ~500K+ by working at two hospitals (of course, they have to work 6 days a week, alternating 3 days at each hospital). Alternatively, you can run your own practice (either as primary income or just a side hustle on weekends). My wife used to volunteer at several private clinics in NY metro area. Most of the generalist doctors (i.e. Internal Medicine) she volunteered for see ~30-40 patients a day (actually one doctor sees 70-80 patients a day!). They make ~$80 net (that is assuming the patients uses medicaid/medicare, which is actually at the lower end of the pay per visit) from each patient (because my wife enters the code and helps with miscellaneous things around the clinic for them). These clinicians are making each year ~$8030 patients20 days per month * 12 months a year = $576K/year. All of these doctors she volunteered are above 60+ years old. I am not exaggerating about that. Of course, this is in NY-like metro area where population is diverse and dense. Some rural area doctors wouldn't have such opportunity to make that much money, but what I'm saying is that generalist doctors sure have opportunity to make significantly more than 250K/yr average.

All in all, I'm not buying that the opportunity cost of becoming a doctor is a sacrifice one mad. It is (in my opinion) part of the trade-off one have to make to get a steady and pretty good income for the remaining 30 years of your working life (assuming one retires at 62, but if you are a generalist doctor--i.e. not related to ICU or surgery--you can work until 65+ just like the anesthesiologist from my host family. With 250K/year income (https://www.kaptest.com/study/mcat/doctor-salaries-by-specia...), you can easily catch up for the missed opportunity cost in a reasonable time frame.