1. A note: PDF is a singularly poor way of delivering text for reuse. Line breaks, tabs and indents will probably be lost or mangled beyond recognition. It seems simple and obvious, but it will be a huge source of frustration for your audience. Include the code in separate files, or even in attachments (as well as the text, for the reader).
2. That said, what you describe should never happen when making a PDF from text you can see in InDesign. What did that page look like before you exported it from InDesign?
While I have done a lot of manuals where code is in place, I haven't experienced what you are showing in your screen shot.
In my screen shot, there is the code from your post. It appears on the page just fine. But as Test SN says, there will be line breaks. The second code block shown below the first is after copying from the resultant PDF and pasting back into the same frame. Note that the original doesn't have hard returns except where they should be. But in the code block pasted from the PDF, there are hard returns where they shouldn't be. And the same thing happens when the text is added inside Acrobat.
Now, for coding I use UltraEdit and I can easily fix the returns issue, but we have always included plain text files with each code snippet numbered, keyed to the block in the PDF (think figure numbers, that sort of idea). These code sample files were on the distributed CD, in the download ZIP and on the web site for download.