Learn techniques and best practices to maximize the integrated AI assistant in Pocket Code.
The AI assistant in Pocket Code is a powerful tool that can significantly accelerate your development. Here's how to get the most out of it.
β 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
The AI works better when you give it context about your project:
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."
Don't expect perfect code on the first try:
// 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;
}
}
Paste existing code and ask for improvements:
"Refactor this code to make it more readable and efficient"
When you encounter difficult code:
"Explain what this function does step by step"
# 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()
Save effective prompts as snippets for reuse.
AI doesn't just generate code, it teaches. Read the comments and explanations.
"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);
}
"Convert this Python function to JavaScript"
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! π€β¨