Programming is not just about creating functional code. It’s also about ensuring code quality and maintainability. Cursor AI is a tool that can assist in writing better code, provided you follow a few key principles. Below, you’ll find tips on how to prompt effectively to write code that is understandable, readable, and resilient to future changes, based on Cursor AI’s core concepts.
1. Write code understandable to humans, not just machines.
Readable code is the foundation of good programming. Use intuitive names for variables, functions, and classes so that anyone can understand what your code does without extensive analysis. Avoid abbreviations that might confuse other developers.
Example:
I added a section with variables that need to be filled in before use:
OCTOPARSE_USERNAME – username from the configuration
OCTOPARSE_PASSWORD – password from the configuration
OCTOPARSE_TOKEN – token received after authorization
TASK_ID – ID of the task to check the status
TS_AUCTIONS_TASK_ID – ID of the task for the auction list
TS_CAR_DETAILS_TASK_ID – ID of the task for car details
Extending the Client Interface ✅
@FeignClient(
name = "octoparse-client",
url = "\${octoparse.api.url}",
configuration = [OctoparseHttpConfiguration::class],
contextId = "octoparseClient"
)
interface OctoparseClient {
@GetMapping("/data/notexported")
fun getNotExportedData(
@RequestParam taskId: String,
@RequestHeader("Authorization") authorization: String,
@RequestParam type: Class
): TSResponse
// Helper methods for Trading Solutions
fun getTSAuctions(authorization: String): TSResponse =
getNotExportedData(
taskId = TSTaskType.AUCTIONS_LIST.taskId,
authorization = authorization,
type = TSAuction::class.java
)
fun getTSCarDetails(authorization: String): TSResponse =
getNotExportedData(
taskId = TSTaskType.CAR_DETAILS.taskId,
authorization = authorization,
type = TSCarDetails::class.java
)
}
2. Define the goal of the task and break it into smaller parts
Before starting the implementation, clearly define what you aim to achieve. Break larger tasks into smaller, more manageable components. This will simplify both the implementation process and future code maintenance.
Cursor AI can assist in planning your work by suggesting divisions into functions and modules.
Let me know if you’d like me to continue!
3. Choose the right algorithms and data structures
Selecting the appropriate algorithms and data structures can significantly improve the performance and readability of your code. For instance, if you’re working with hierarchical data, use a tree instead of a list.
Example:
// Poor Choice of Data Structure
val users = mutableListOf()
// Better Choice of Data Structure
val users = mutableMapOf<String, MutableList>()
4. Avoid Repetition
The DRY (Don’t Repeat Yourself) principle is one of the fundamental rules of good coding. Repetition increases the risk of errors and makes code maintenance more difficult.
Cursor AI helps identify sections of code that can be refactored into shared functions or classes.
Example:
// Repetitive Code
println("Hello, John")
println("Hello, Jane")
// DRY-Compliant Code
fun greetUser(name: String) {
println(„Hello, $name”)
}
greetUser(„John”)
greetUser(„Jane”)
5. Clear input and output data format
Defining a standard format for input and output data greatly simplifies testing and application integration. A good practice is to use data types and annotations.
Example:
fun calculateSum(numbers: List): Int {
return numbers.sum()
}
6. Anticipate difficulties and edge cases
While writing code, think about potential issues that may arise. Test your code with data that is unusual or extreme to ensure robustness.
7. Comment only what’s not obvious
Good comments explain why the code does something, not what it does. Avoid excessive comments that can distract or mislead. Whenever possible, use docstrings to document the purpose and behavior of your functions.
Example:
// Redundant Comment
var x = x + 1 // Adding 1 to x
// Valuable Comment
x = x + 1 // Compensating for rounding error
// Class Documentation Example
/**
* A class handling the data extraction process from the Octoparse system.
*
* @param username Username for authentication
* @param password Password for authentication
* @param taskId Task identifier for data extraction
*/
class DataExtractor(
val username: String,
val password: String,
val taskId: String
) {
var token: String? = null
var data: List<Map<String, Any>> = emptyList()
}
8. You Don’t Need to Remember Coding Standards and Best Practices
The Cursor agent reads linter errors to automatically fix them, so you can spend your time playing foosball instead. 😊
9. Optimize Performance Wisely
Performance is important, but not at the expense of readability. Avoid premature optimization, as it can make debugging and application development more challenging.
10. Write Tests
Automated tests ensure the stability and reliability of your code. Well-written test cases make it easier to implement changes and catch errors. Cursor AI is particularly helpful as it can come up with the most unpredictable edge case scenarios.
Example of a test in Python:
def test_calculate_sum():
# Test case for calculating the sum of a list of numbers
result = calculate_sum([1, 2, 3])
expected = 6
assert result == expected, f"Expected {expected}, but got {result}"
11. AND MOST IMPORTANTLY: Cursor AI Will Write the Documentation for You 😎
Based on the code it generated itself. 🚀
Summary
Cursor AI is a powerful tool that can assist in writing clean, readable, and efficient code. The key to success lies in following programming best practices, such as the DRY principle, proper code formatting, and writing tests.
Remember, coding is an art that combines creativity with technical discipline – good tools like Cursor AI can make this task significantly easier. 🚀