Skip to content
Back to home
Back to blog
Tips

How to Use AI to Generate Code Faster

Learn techniques and best practices to maximize the integrated AI assistant in PocketCode.

3 min
By Pelayo Naredo
How to Use AI to Generate Code Faster

Maximize Your Productivity with AI

The AI assistant in PocketCode is a powerful tool that can significantly accelerate your development. Here's how to get the most out of it.

Best Practices

1. Be Specific in Your Prompts

Bad: "create a function"

Good: "create a Python function that takes a list of numbers and returns the median"

def calculate_median(numbers):
    """Calculates the median of a list of numbers."""
    sorted_nums = sorted(numbers)
    n = len(sorted_nums)

    if n % 2 == 0:
        return (sorted_nums[n//2 - 1] + sorted_nums[n//2]) / 2
    else:
        return sorted_nums[n//2]

# Usage
print(calculate_median([1, 3, 5, 7, 9]))  # 5

2. Use Context

The AI works better when you give it context about your project:

  • Mention the framework you're using
  • Specify versions if relevant
  • Describe the code's purpose

Example prompt with context:

"Create a React component with hooks that displays a todo list. It should allow adding, deleting, and marking tasks as completed. Use TypeScript and Tailwind CSS for styling."

3. Iterate and Refine

Don't expect perfect code on the first try:

  1. Generate initial code
  2. Test and verify
  3. Request specific modifications
  4. Optimize

Common Use Cases

Generate Boilerplate

// Prompt: "Create a TypeScript class for a user with validation"

interface UserData {
  id: string;
  email: string;
  name: string;
  createdAt: Date;
}

class User {
  private data: UserData;

  constructor(data: UserData) {
    this.validateEmail(data.email);
    this.data = data;
  }

  private validateEmail(email: string): void {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailRegex.test(email)) {
      throw new Error('Invalid email format');
    }
  }

  get email(): string {
    return this.data.email;
  }

  get name(): string {
    return this.data.name;
  }
}

Refactor Code

Paste existing code and ask for improvements:

"Refactor this code to make it more readable and efficient"

Explain Complex Code

When you encounter difficult code:

"Explain what this function does step by step"

Generate Tests

# Prompt: "Generate unit tests for the calculate_median function"

import unittest

class TestCalculateMedian(unittest.TestCase):
    def test_odd_list(self):
        self.assertEqual(calculate_median([1, 3, 5]), 3)

    def test_even_list(self):
        self.assertEqual(calculate_median([1, 2, 3, 4]), 2.5)

    def test_single_element(self):
        self.assertEqual(calculate_median([5]), 5)

    def test_unsorted_list(self):
        self.assertEqual(calculate_median([5, 1, 3]), 3)

if __name__ == '__main__':
    unittest.main()

Advanced Tips

1. Combine with Snippets

Save effective prompts as snippets for reuse.

2. Learn from Responses

AI doesn't just generate code, it teaches. Read the comments and explanations.

3. Use for Documentation

"Generate JSDoc documentation for this function"

/**
 * Calculates the factorial of a number
 * @param {number} n - The number to calculate factorial for
 * @returns {number} The factorial of n
 * @throws {Error} If n is negative
 */
function factorial(n) {
	if (n < 0) throw new Error("Cannot calculate factorial of negative");
	if (n === 0 || n === 1) return 1;
	return n * factorial(n - 1);
}

4. Language Conversion

"Convert this Python function to JavaScript"

Limitations to Consider

  • Not always perfect: Always review generated code
  • Privacy: Only your code is sent when you make an explicit query
  • API costs: Use your own key to control expenses

Productivity Shortcuts

  1. Cmd/Ctrl + K: Open the AI assistant
  2. Select code + Cmd/Ctrl + K: Ask about selected code
  3. Double tap on error: Ask for help fixing it

Conclusion

The AI assistant is like having an expert programming partner available 24/7. Use it wisely and watch your productivity soar.

Next tutorial: GitHub integration from mobile

Happy coding! 🤖✨

The tool behind this article

AI Assistant

Ready to try PocketCode?

Download the app and start coding from your mobile.