How-to articleResearch writingComputer science

How to present computer code and algorithms in a thesis

Code is part of the evidence in many theses, but pages of source listings rarely help an examiner. This guide explains when to use pseudocode, when to show a short listing and how to archive and cite the full code, with LaTeX tools, an illustrative algorithm and a checklist of common mistakes.

What you will learn

  • Choose between pseudocode, a source listing and an archived code base for each piece of code
  • Format algorithms and listings with numbering, captions, line numbers and cross-references
  • Explain code in the text without narrating it line by line
  • Archive and cite research software so that examiners and readers can obtain the exact version used
  • Prepare code and documentation that support the reproducibility of your results

What you will unlearn

  • The more code you include, the more work the examiner will see.Long listings obscure the contribution; show algorithms and short excerpts, and archive the full code.
  • Pseudocode is just simplified Python.Pseudocode is language-neutral and shows the logic of an algorithm, with defined inputs, outputs and steps.
  • Putting code on GitHub is enough.A repository can change or disappear; archive the version used in the thesis with a persistent identifier and cite it.
  • Code is not a research output that needs citing.Software is a citable research product; cite your own code and the software you relied on, with versions.

Skills you will practise

  • Writing pseudocode
  • Formatting listings in LaTeX
  • Documenting code
  • Software citation
  • Reproducible research practice
  • Explaining algorithms in prose

Why the way you present code matters

In computational theses the code often embodies the method, so how you present it affects how well an examiner can judge your work. Examiners need to understand what your algorithm does, why it is designed that way and whether the results it produced can be trusted. They rarely need to read every line. A thesis that presents a tool or technique should make its result and its validation clear, as good software engineering research papers do (Shaw, 2003).

Presentation is also part of reproducibility. Computational results are increasingly expected to come with the code and data that produced them, so that others can check and build on them (Peng, 2011). Experience shows why this matters: when researchers tried to obtain and build the code behind hundreds of computer systems papers, much of it could not be obtained or built (Collberg & Proebsting, 2016), and in a recent sample of 100 replication packages from a leading software engineering conference only 40 ran fully, and 14 of those reproduced the reported results (Muttakin et al., 2026). Your thesis can do better.

Three forms of code in a thesis

Choosing how to present code
FormUse it forWhere it goesTypical tools
Pseudocode (algorithm)Showing the logic of a new or adapted algorithm, independent of languageMain text, numbered as Algorithm 1, 2 ...LaTeX algorithm environments (algorithm2e, algpseudocode)
Short source listingShowing an implementation detail that matters to the argument, an API use or a configurationMain text (short) or appendix, numbered as Listing 1, 2 ...LaTeX listings or minted; Word with a monospaced style
Full code baseAllowing readers and examiners to run, inspect and reuse the workArchived repository, cited in the thesisVersion control plus an archive that issues a persistent identifier

As a rule, describe your method in prose and figures, present the key algorithms as pseudocode, show a listing only when the exact code is the point, and put everything else in an archived repository. Check your university's regulations on appendices and supplementary files: some limit appendix length or allow electronic supplements to be deposited with the thesis.

Think also about where code appears in the structure of the thesis. The methodology or design chapter usually presents the algorithms and the architecture of the implementation; the evaluation or results chapter reports what the code produced, with the experimental set-up; and an appendix or data availability statement tells the reader where the archived code and data can be found. Keeping these roles separate stops implementation detail from crowding out the argument.

Writing pseudocode

Good pseudocode is precise but not tied to a programming language. State the inputs and outputs, number the lines, use standard control structures (for, while, if, return), use mathematical notation where it is clearer than code, and omit details such as input parsing or logging that do not affect the logic. Give each algorithm a number and a caption, and refer to it in the text before it appears.

Algorithm 1. An illustrative algorithm written for this guide, shown as numbered pseudocode
LineAlgorithm 1: Sliding-window z-score anomaly detection (illustrative)
1Input: series x1, ..., xn; window length w; threshold k
2Output: set A of indices flagged as anomalous
3A ← empty set
4for t ← w + 1 to n do
5  μ ← mean of xt−w, ..., xt−1
6  σ ← standard deviation of xt−w, ..., xt−1
7  if σ > 0 and |xt − μ| / σ > k then
8    add t to A
9return A

In the text, explain the design rather than the syntax: "Algorithm 1 flags a reading when it lies more than k standard deviations from the mean of the preceding w readings (lines 5 to 7). The guard on line 7 avoids division by zero in constant segments. As written, each step recomputes the window statistics, giving O(nw) time; maintaining running sums reduces this to O(n)." Referring to line numbers lets the examiner follow the argument without reading the whole algorithm.

In LaTeX, the algorithm2e package, or the algorithmicx bundle with its algpseudocode style inside an algorithm float, produces numbered, captioned algorithms with line numbers; in a word processor, a numbered table or text box with a monospaced or clearly indented style serves the same purpose. For thesis set-up in LaTeX more generally, see Writing a thesis in LaTeX.

Formatting source listings

  • Keep it short. Show the 10 to 30 lines that matter, not whole files; mark omissions with a comment.
  • Use a monospaced font set once in your template, with consistent indentation.
  • Number lines so that the text can refer to them.
  • Caption and label each listing ("Listing 3: Training loop with early stopping") and cross-reference it.
  • Highlight sparingly. Syntax highlighting helps on screen; check that it remains legible in greyscale print.
  • Follow the language's style conventions, such as PEP 8 for Python, so that the code reads as professional.
  • Comment for the reader: explain why a step is there, not what each line does.
  • Avoid screenshots of code; they cannot be searched, copied or read by screen readers.

In LaTeX, the listings package typesets code natively, with options for language, line numbers, caption and label, for example \begin{lstlisting}[language=Python, caption={Training loop}, label=lst:train]. The minted package gives richer highlighting but relies on an external highlighter and may need extra compiler settings, which can complicate building the thesis on another machine. Whichever you use, include listings from the source files rather than pasting copies, so that the thesis shows the code that actually ran.

Archiving and citing the full code

The full code base should be available in a form that will not change. Use version control throughout the project, and when the thesis is submitted, tag the exact version used and deposit it in an archive that issues a persistent identifier such as a DOI; repository platforms can be linked to archives that do this automatically. Then cite that version in the thesis. The software citation principles set out why and how: software is a legitimate research product, citations should credit its authors, identify it uniquely and persistently, give access to it, and specify the exact version used (Smith et al., 2016).

  • A README explaining what the code does, how to install it and how to reproduce each main result.
  • A record of the environment: language and library versions, ideally in a lock file or container definition.
  • Scripts that regenerate every table and figure from raw or processed data, without manual steps (Sandve et al., 2013).
  • Fixed random seeds, and a note of hardware where results depend on it.
  • A licence, so that others know whether they may reuse the code.
  • Data, or instructions for obtaining it, described so that it is findable and reusable (Wilkinson et al., 2016).

For machine learning work, also report data splits, hyperparameter searches and computing resources, as reproducibility checklists in the field require (Pineau et al., 2021), and check for data leakage between training and test sets, which can make results irreproducible even when code is shared (Kapoor & Narayanan, 2023). Where you report run times or memory use, describe how they were measured and on what hardware, because naive timing can be misleading (Beyer et al., 2019). For a fuller treatment of reproducible programming practice, see Programming for AI research in a PhD.

Acknowledging other people's code and tools

Most research code builds on libraries, frameworks and examples written by others. Cite the main software packages your results depend on, with version numbers, in the same way you cite papers (Smith et al., 2016); many projects state how they wish to be cited in their documentation or in a citation file in the repository. If you adapted code from a tutorial, a forum answer or another researcher's repository, say so in a comment in the code and in the thesis, and check that its licence allows reuse.

The same transparency applies to code produced with the help of generative AI tools. Follow your university's policy on declaring such use, keep a note of where it was used, and make sure you understand, have tested and can explain every line that appears in your thesis or affects your results. In the viva you may be asked why an algorithm was designed in a particular way; being able to answer from your own reasoning is part of showing that the work is yours.

Common mistakes

  • Appendices of hundreds of pages of source code that no examiner will read.
  • Listings with no caption, number or reference in the text.
  • Pseudocode that is really code in one language, with library calls and syntax details.
  • Narrating code line by line instead of explaining the design and its rationale.
  • Screenshots of code or of an editor window.
  • Linking to a repository that later changes, without archiving and citing the version used.
  • Results in the thesis that cannot be regenerated from the archived code and data.
  • Failing to cite the libraries, tools and other people's code the work depends on.

For presenting the outputs of your code, see How to present PhD data in tables and figures and How to write the results chapter; for citing data and code in reference lists, see How to reference an econometrics PhD thesis. Annotated computing examples are in sample work.

Key takeaways

  • Use pseudocode for algorithms, short listings for implementation details that matter, and an archive for the full code.
  • Number, caption and cross-reference every algorithm and listing, and explain design choices in prose.
  • Archive the exact version of your code with a persistent identifier and cite it (Smith et al., 2016).
  • Make results regenerable by scripts, with recorded environments and seeds (Sandve et al., 2013; Peng, 2011).
  • Shared code often fails to run or reproduce results, so test your archive before submission (Collberg & Proebsting, 2016; Muttakin et al., 2026).
Your next step

Want to apply this to your own study?

  1. 1Tell us your topic, data and deadline
  2. 2A subject mentor reviews your work with you
  3. 3You get a plan and a quote
Full dissertation mentoring

References

  1. Beyer, D., Löwe, S., & Wendler, P. (2019). Reliable benchmarking: Requirements and solutions. International Journal on Software Tools for Technology Transfer, 21(1), 1–29. https://doi.org/10.1007/s10009-017-0469-y
  2. Collberg, C., & Proebsting, T. A. (2016). Repeatability in computer systems research. Communications of the ACM, 59(3), 62–69. https://doi.org/10.1145/2812803
  3. Kapoor, S., & Narayanan, A. (2023). Leakage and the reproducibility crisis in machine-learning-based science. Patterns, 4(9), 100804. https://doi.org/10.1016/j.patter.2023.100804
  4. Muttakin, A., Mondal, S., & Roy, C. K. (2026). The state of open science in software engineering research: A case study of ICSE artifacts. In Proceedings of the IEEE/ACM 48th International Conference on Software Engineering (ICSE '26). Association for Computing Machinery. https://doi.org/10.1145/3744916.3787813
  5. Peng, R. D. (2011). Reproducible research in computational science. Science, 334(6060), 1226–1227. https://doi.org/10.1126/science.1213847
  6. Pineau, J., Vincent-Lamarre, P., Sinha, K., Larivière, V., Beygelzimer, A., d'Alché-Buc, F., Fox, E., & Larochelle, H. (2021). Improving reproducibility in machine learning research (a report from the NeurIPS 2019 reproducibility program). Journal of Machine Learning Research, 22(164), 1–20. https://www.jmlr.org/papers/v22/20-303.html
  7. Sandve, G. K., Nekrutenko, A., Taylor, J., & Hovig, E. (2013). Ten simple rules for reproducible computational research. PLoS Computational Biology, 9(10), e1003285. https://doi.org/10.1371/journal.pcbi.1003285
  8. Shaw, M. (2003). Writing good software engineering research papers. In Proceedings of the 25th International Conference on Software Engineering (pp. 726–736). IEEE. https://doi.org/10.1109/ICSE.2003.1201262
  9. Smith, A. M., Katz, D. S., & Niemeyer, K. E. (2016). Software citation principles. PeerJ Computer Science, 2, e86. https://doi.org/10.7717/peerj-cs.86
  10. Wilkinson, M. D., Dumontier, M., Aalbersberg, I. J., Appleton, G., Axton, M., Baak, A., Blomberg, N., Boiten, J.-W., da Silva Santos, L. B., Bourne, P. E., Bouwman, J., Brookes, A. J., Clark, T., Crosas, M., Dillo, I., Dumon, O., Edmunds, S., Evelo, C. T., Finkers, R., . . . Mons, B. (2016). The FAIR guiding principles for scientific data management and stewardship. Scientific Data, 3, Article 160018. https://doi.org/10.1038/sdata.2016.18

How to cite this article

Choose your referencing style, then copy. Check your university's or journal's guide for local variations.

PhD Research Lab Academy. (2023, November 24). How to present computer code and algorithms in a thesis. PhD Research Lab. https://www.phdassistance.com/study-guide/how-do-you-present-computer-code-in-a-thesis/

PhD Research Lab Academy. “How to Present Computer Code and Algorithms in a Thesis.” PhD Research Lab, 24 Nov. 2023, www.phdassistance.com/study-guide/how-do-you-present-computer-code-in-a-thesis. Accessed [access date].

PhD Research Lab Academy (2023) How to present computer code and algorithms in a thesis. PhD Research Lab. Available at: https://www.phdassistance.com/study-guide/how-do-you-present-computer-code-in-a-thesis/ (Accessed: [access date]).

PhD Research Lab Academy. “How to Present Computer Code and Algorithms in a Thesis.” PhD Research Lab. November 24, 2023. https://www.phdassistance.com/study-guide/how-do-you-present-computer-code-in-a-thesis.

PhD Research Lab Academy, ‘How to Present Computer Code and Algorithms in a Thesis’ (PhD Research Lab, 24 November 2023) <https://www.phdassistance.com/study-guide/how-do-you-present-computer-code-in-a-thesis> accessed [access date].

PhD Research Lab Academy. How to present computer code and algorithms in a thesis [Internet]. PhD Research Lab; 2023 Nov 24 [cited [access date]]. Available from: https://www.phdassistance.com/study-guide/how-do-you-present-computer-code-in-a-thesis/

PhD Research Lab Academy, “How to present computer code and algorithms in a thesis,” PhD Research Lab, Nov. 24, 2023. [Online]. Available: https://www.phdassistance.com/study-guide/how-do-you-present-computer-code-in-a-thesis. [Accessed: [access date]].

@online{how_do_you_present_computer_code_in_a_thesis_2023,
  author = {{PhD Research Lab Academy}},
  title = {{How to present computer code and algorithms in a thesis}},
  organization = {PhD Research Lab},
  date = {2023-11-24},
  url = {https://www.phdassistance.com/study-guide/how-do-you-present-computer-code-in-a-thesis},
  urldate = {[access date]}
}

This article is for learning. Use it to understand the method and cite the original sources above in your own work; do not copy or submit it as your own.

How to present computer code and algorithms in a thesisNeed help applying it to your study?
Full dissertation mentoring