Skip to main content
We maintain a compact image-optimizer demo that illustrates a full-stack flow and how to safely use AI-assisted code completion (like Tabnine) to generate inline documentation. Overview
  • Backend: a Python Flask service that accepts an uploaded image, processes it with OpenCV, and returns an optimized image.
  • Frontend: a React UI that uploads images, sets a quality parameter, and displays original vs optimized images with size comparisons.
  • Purpose: show practical, safe patterns for generating docstrings and inline comments with Tabnine and similar tooling, and how to avoid common pitfalls when documenting large functions.
Why use Tabnine (or similar)?
  • Speeds up writing docstrings, JSDoc, and inline comments.
  • Helps standardize documentation style.
  • Best used on small, focused selections (single functions or utility blocks) rather than whole files.
Below is a representative snippet from the Flask backend showing imports and the start of the upload endpoint. Note the correct standard-import name imghdr and robust early request checks:
Screenshot of a desktop browser showing an image-optimizer demo with "Original Image" and "Optimized Image" versions of a book cover titled "Coding with AI" and their file sizes. A gradient page background is shown inside the browser while a code editor and project files are visible behind the window.
Best practice note for large functions and AI tools
AI completion tools are most effective when you provide focused context. For long files or functions (80+ lines), generate documentation for a small selection (for example, one function or a helper). Then manually review and paste the docstring or comments into your code to avoid unintended edits to imports, logging, or scaffolding.
Common problem patterns and how to avoid them
  • Tools can produce broad diffs that remove critical imports, logging setup, or configuration.
  • Very large functions reduce the assistant’s ability to reason about context, leading to incorrect or incomplete suggestions.
  • Refactor large functions into logically smaller units (validation, decoding, processing, response). Smaller functions are easier to document and test.
Problem / Symptom / Recommended Fix Example: safe generated docstring for the Flask upload endpoint
  • Rather than accepting a large automatic edit, generate a concise docstring for the function and apply it manually:
A corrected, concrete fragment for image processing
  • This is an example fragment you can use inside the upload endpoint after you validate the request and the image file:
Practical workflow when using Tabnine (or other assistants) on large files
  1. Select only the smaller code region you want documented (a single function or helper).
  2. Ask Tabnine to “Document” or “Explain” that selection.
  3. Review the generated docstring/comments. Copy only the docstrings/comments you want and paste them manually into the file — this prevents removal of imports and scaffolding.
  4. Run tests or a manual smoke test after accepting any generated change.
  5. Prefer refactoring large functions into smaller units before relying on automated documentation.
React frontend: JSDoc examples
  • Rather than rewriting an entire React file, generate short JSDoc comments for components or utilities and insert them manually.
Important warning
Never accept a generated change that removes imports, logging setup, or other scaffolding without a careful review. Those deletions will usually break your application.
Summary and final tips
  • Tabnine and similar tools can accelerate documentation work, especially for small, well-defined code blocks.
  • For functions longer than ~80 lines, prefer:
    • Breaking the function into smaller units (validation, decoding, processing, response).
    • Generating docstrings for selected regions instead of entire files.
    • Manually pasting and reviewing generated docstrings/comments.
  • Always run tests (or a quick manual smoke test) after applying generated changes.
References and further reading

Watch Video