It is true that code examples look broken at times, but most of the times turning the kindle to landscape orientation does the trick.
You should avoid feeding PDFs to your Kindle, since they only look good on a PC or on a 4:3 tablet (ex. iPad or Galaxy Tab S2), although, after reading a chapter on your Kindle, it is positive to skim through the PDF to get a grasp of the details you might have missed.
As a Kindle owner, the format you really want is .MOBI (or .EPUB converted with kindlegen[1] and trimmed down with kindlestrip[2]).
Most .MOBIs you'll encounter are actually conversions from .EPUB with kindlegen, so you should trim them down with kindlestrip too.
Finally, if you're a Kindle power user, then you should give Calibre[3] a try to convert other formats to .MOBI, but be warned that the conversion is a hit-or-miss.
[1] https://www.amazon.com/gp/feature.html?docId=1000765211
That's where a multi-format PDF would come in handy -- you could still have a human editor lay out the pages for 2 or 3 different page sizes, and then the reader would pick the appropriate one (given the constraints of the reader device and font size).
PDF spec actually supports different page sizes (crop box, trim box, bleed box, media box, etc) for different pages. But it seems like what you what is essentially two separate PDFs with different page sizes combined together, and the reader picks one based on the viewport. I'm not sure PDF spec natively supports that, but PDF does support JavaScript and I can imagine JavaScript querying the viewport and then hiding/showing pages.
A useful feature of a PDF reader on these devices could be to display only this area with a smaller, perhaps user defined, margin.
This at least would fix the 'huge margin' problem.
As long as I have two screens, I've learned to like digital textbooks. It's hard to ctrl+f on a piece of paper.
Just a third (or fourth) tier in the scheme they already use.
The typesetting and layout of textbooks is also very important. It's not just free flowing text. This means they are simple not compatible with the screen sizes of computers or ebook readers.
The ability to flip through a book is surely one of the best features of the format. When I'm familiar with a textbook I can find the page I want in seconds based on some vague intuition about where it will be (for some textbooks I can do this with my eyes closed). I'm not aware of any electronic formats that offers such fast random access.
You could also pay a relatively small amount to get the PDFs printed and spiral-bound at a print shop. That solves your problem of free-flowing text, plus they lay flat, which is a godsend.
I've found that I really like buying digital books, but using my iPad or Kindle computer app to read coding books because the formatting is better. Even if you don't copy/paste, it's easier to type in code if it's in the window next to you instead of looking down (yeah, I can type without looking at the keyboard, but I have more trouble in an IDE when I have to match parens and quotes, deal with autocomplete and other syntax things like semicolons).
But I am with you on the print books. I've spend many thousands of dollars building up my home CS library. Nothing helps me learn better than stepping away from the screens and reading paper in a comfortable chair!
Screen is adequately large and the DPI is so high that they look great, it turned out to be a useful device.