views:

136

answers:

6

I am currently writing a formal research report, and I'll be including code with this report.

Question: Is there an accepted way of displaying code in research reports? I'm thinking both in terms of font, spacing, et cetera, and whether the code should be displayed inside the document, or in an appendix.

The code will be JavaScript and PHP. None of the sections of code will be more than 25 lines (so they're mere snippets). There will be approximately half a dozen snippets. Each of the snippets will have a couple of paragraphs explaining what is happening in the code, and a discussion on its pros/cons.

I have no contact with the body to whom the report will be submitted to, and they have no published guidelines on how to format code (please do not question these points).

Any help considered and appreciated.

+2  A: 

Well it depends on which style guide your paper is being written to comply to...

Usually code should be written in a monotype font so that it is easily readable (E.g. Lucida Sans Console or Courier New). This means that all letters take up the same space on the page.

When I have written bits for publishing I have indented the code 2.5cm from the side and given it a light grey background, in a Lucida Sans Console font... Following C style code indenting.

I would ask your institution if they have a style guide, but as you have a lack of this ability go with a popular style guide such as the Harvard system and make sure you follow the same format throughout.

Here is a list of journals from Google Scholar which display style: http://scholar.google.com.au/scholar?hl=en&q=PHP+SQL+programming+journal&btnG=Search&as_sdt=2000&as_ylo=&as_vis=0

JD
Thank you for answer, taken into consideration. Do you have any thoughts as to whether the code should be placed inline with the text, or in an appendix?
GlenCrawford
I've always placed it inline if it is less than a page, and in an appendix if it is a long slog of code.. If it is long it might be worth looking into line numbers too, then you can cross reference the code. All my university papers which needed code or arrays etc. were cited as Code 1.0 Code 1.1 etc.. and were inline.
JD
Excellent, thanks. Answer accepted.And thanks to the other answer-ers, too!
GlenCrawford
Hi, I work in a university - usually here (subjective!) major portions of code are added as an appendix, any examples of code added inline in the report. Code should always follow the coding conventions given (also subjective!)
Reuben Mallaby
+2  A: 

I would say Courier font with standard text spacing and standard line spacing, all black text, proper indentation.

In terms of the code itself, omit import statements, comments are okay. You may want to add foot notes like {1}, {2}, inline in the code as a comment and reference below in the text that explains the code.

This paper has an example on page 6:

http://www.eecs.berkeley.edu/Pubs/TechRpts/2006/EECS-2006-1.pdf

NW Architect
Same comment as above: Thank you for answer, taken into consideration. Do you have any thoughts as to whether the code should be placed inline with the text, or in an appendix?
GlenCrawford
I would place relevant sections inline to illustrate points.
NW Architect
A: 

If you are writing a research report, you should be using LaTeX.

I typically use the LaTeX vancyvrb package and the Verbatim.

However, another option is to use the listings package. It can input a file directly using the lstinputlisting command. It automatically numbers your lines and uses the _ character instead of the space character, but this is programmable. It's really quite nice.

vy32
Latex is fine, but there are perfectly decent alternatives.
Charles Stewart
Like what? I like being able to keep my report source in SVN, include graphics as references to other files, and produce very clean PDFs.
vy32
+2  A: 

This is my preference:

When writing inline, get rid of code that is irrelevant to the explanation (such as import statements as previously mentioned, but potentially also variable declarations that are "obvious" and the like). The goal of code placed inline should be for easy crossreference with the paragraph describing that code block.

Code placed in appendices should be complete (as in - you can put this into your compiler and press go).

Don't be scared of placing heavily cut down code in snippets, along with a reference to the appendix containing the full code - the appendix code is for someone to read/run separately. the inline code is for people to glance at and help understand the specific point of that section.

Ben Clifford
+1 All good advice. Wrt. to code in appendices: it's nice if it is all executable, but this may often be impractical. Having a separate document that is literate code conserves executability without sacrificing terseness.
Charles Stewart
+1  A: 

I realise that this is an old question, but do not forget to number the lines in your code! For one-liners, it's okay to skip numbers, but anything larger, they're almost required.

I disagree, I find it much easier to read code examples where there are sparse specific labels, such as a, b, c presented as a light-colored letter on dark circle. Unless you really need to explain every single line, only add the markers for each cross-reference.
Andy Dent
A: 

What JD and Ben said.

You should use appropriate, established syntax highlighting. Latex's listings package, mentioned by vy32, has syntax highlighting styles for both Javascript and PHP, as does the Pygments program, which outputs to, among others, Latex, HTML, and RTF.

Charles Stewart