CODE SNIPPET TEMPLATE

Share your code with proper documentation and context. This template helps you format your code snippets in a way that makes them more valuable to others.

Perfect for:

  • Sharing solutions on Stack Overflow
  • Documenting code for team members
  • Creating code examples for documentation
  • Saving reusable code patterns
  • Teaching programming concepts

TEMPLATE PREVIEW

# Code Snippet: [Title of Snippet]

## Description
[Provide a brief description of what this code does and why it's useful]

## Language/Environment
- **Language**: [e.g., JavaScript, Python, Java]
- **Version**: [e.g., Python 3.9, Node.js 16.x]
- **Framework/Library**: [e.g., React, Django] (if applicable)

## Dependencies
```
[List any libraries, packages, or dependencies required]
# For example, for Python:
# requests==2.26.0
# pandas==1.3.3
```

## Code
```[language]
[Your code here]
```

## Usage Example
```[language]
[Example of how to use this code]
```

## Expected Output
```
[What the code is expected to output when run]
```

## Explanation
[Provide a step-by-step explanation of how the code works]

1. [Step 1]
2. [Step 2]
3. [Step 3]

## Edge Cases & Limitations
- [List any known limitations]
- [Describe edge cases or scenarios where this might not work]
- [Performance considerations]

## Alternative Approaches
[If there are other ways to solve the same problem, briefly mention them]

## Additional Notes
[Any other relevant information]

## References
- [Link to relevant documentation]
- [Link to related resources]
- [Credit original sources if applicable]

---
Author: [Your Name]
Last Updated: [YYYY-MM-DD]

EXAMPLE SNIPPET

Code Snippet: Simple Node.js API Rate Limiter

A middleware function for Express.js that implements a basic rate limiter using in-memory storage.

// Simple rate limiter middleware for Express.js
const rateLimit = (options = {}) => {
  const {
    windowMs = 60 * 1000, // 1 minute by default
    max = 100, // max 100 requests per windowMs
    message = 'Too many requests, please try again later.'
  } = options;
  
  // Store for tracking requests
  const requestStore = new Map();
  
  // Cleanup old entries every windowMs
  setInterval(() => {
    const now = Date.now();
    requestStore.forEach((timestamp, key) => {
      if (now - timestamp > windowMs) {
        requestStore.delete(key);
      }
    });
  }, windowMs);
  
  return (req, res, next) => {
    const key = req.ip || req.headers['x-forwarded-for'] || 'unknown';
    const now = Date.now();
    
    // Initialize counter for this IP if it doesn't exist
    if (!requestStore.has(key)) {
      requestStore.set(key, { count: 1, resetAt: now + windowMs });
      return next();
    }
    
    const client = requestStore.get(key);
    
    // Reset count if window has passed
    if (now > client.resetAt) {
      client.count = 1;
      client.resetAt = now + windowMs;
      return next();
    }
    
    // Increment count and check against max
    client.count++;
    if (client.count > max) {
      return res.status(429).send(message);
    }
    
    return next();
  };
};

USAGE TIPS

  • Be thorough with dependencies: List all dependencies and version requirements to make the code more reproducible.
  • Include context: Explain not just what the code does but why it's useful and when to use it.
  • Use syntax highlighting: Specify the language in your code blocks for proper syntax highlighting.
  • Real examples: Provide working usage examples that others can try immediately.
  • Document edge cases: Mention limitations and edge cases to prevent misuse.