In this comprehensive blog, we delve into the core principles of coding excellence in Odoo 16 using Python. We'll cover Algorithmic Optimization, ensuring efficient performance even with complex algorithms; Single Responsibility for clean and maintainable code; the potential of Python's Inbuilt Functions for streamlined development; and the power of Caching Techniques to enhance performance."
Algorithmic Optimization:
* Solution vs. Optimal Solution: When solving a problem, your initial solution may be correct and functional, but it may not be the most efficient or optimal solution in terms of time and space complexity. It's important to recognize that there may be room for improvement.
* Analyzing Complexity: Understanding the time and space complexity of your algorithm is crucial. Analyzing the complexity helps you evaluate how the algorithm scales with input size. Aim to minimize time complexity (e.g., reducing the number of iterations or operations) and space complexity (e.g., optimizing memory usage).
* Readability and Maintainability: While optimization is crucial, it should not come at the expense of code readability and maintainability. Strive for a balance between optimization and code quality to ensure that your code remains understandable, maintainable, and modifiable.
--> O(1) - Constant Time Complexity:
Constant time complexity means that the algorithm's execution time remains constant regardless of the input size. In other words, the algorithm takes the same amount of time to execute, no matter how large the input is.
def get_first_element(lst):
return lst[0]
--> O(log n) - Logarithmic Time Complexity:
Logarithmic time complexity means that the execution time of the algorithm grows logarithmically with the input size. As the input size increases, the execution time increases but at a much slower rate compared to linear time complexity.
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
--> O(n) - Linear Time Complexity:
Linear time complexity means that the execution time of the algorithm increases linearly with the input size. As the input size grows, the execution time also grows at a proportional rate.
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
--> O(n log n) - Log-Linear Time Complexity:
Log-linear time complexity means that the execution time grows in-between linear and quadratic time complexities. It's commonly seen in sorting algorithms like Merge Sort and Quicksort.
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left_half = merge_sort(arr[:mid])
right_half = merge_sort(arr[mid:])
return merge(left_half, right_half)
def merge(left, right):
merged = []
left_idx, right_idx = 0, 0
while left_idx < len(left) and right_idx < len(right):
if left[left_idx] < right[right_idx]:
merged.append(left[left_idx])
left_idx += 1
else:
merged.append(right[right_idx])
right_idx += 1
merged.extend(left[left_idx:])
merged.extend(right[right_idx:])
return merged
In the Merge Sort example, the input array is recursively divided into smaller halves, and then the halves are merged back together. The time complexity of Merge Sort is O(n log n)
--> O(n^2) - Quadratic Time Complexity:
Quadratic time complexity means that the execution time of the algorithm grows quadratically with the input size. It's commonly seen in nested loops and can be inefficient for large input sizes.
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
In the Bubble Sort example, the algorithm compares adjacent elements and swaps them if they are in the wrong order. The time complexity of Bubble Sort is O(n^2).
These are some of the most common Big O notation complexities and their corresponding example codes. Understanding the time complexity of algorithms is essential for optimizing code and selecting the most efficient algorithm for specific tasks. These Notations are applicable to all Programming languages.
Having a basic understanding of these concepts can improve the way you code.
Single Responsibility:
* Each function should have a single responsibility or task to accomplish. This makes the function more focused, easier to understand, and less prone to errors.
* Encapsulation: You promote code reusability and modularity by encapsulating specific functionality within a single function. Functions can be reused in different parts of the codebase without duplicating the implementation.
* Maintainability and Scalability: Codebases that follow the "one function for one thing" principle tend to be more maintainable and scalable. When changes or enhancements are needed, it is easier to identify and modify a specific function without affecting other unrelated parts of the code.
#bad practice
def process_order(order):
# Step 1: Validate the order
if not order:
return "Invalid order"
# Step 2: Calculate total price
total_price = 0
for line in order.lines:
total_price += line.price
# Step 3: Apply discount
discount = 0.1
total_price -= total_price * discount
# Step 4: Update order status
order.status = "Processed"
return total_price
#best practice
def validate_order(order):
if not order:
return False
return True
def calculate_total_price(order):
total_price = 0
for line in order.lines:
total_price += line.price
return total_price
def apply_discount(total_price, discount):
return total_price - total_price * discount
def update_order_status(order, status):
order.status = status
def process_order(order):
if not validate_order(order):
return "Invalid order"
total_price = calculate_total_price(order)
total_price_with_discount = apply_discount(total_price, 0.1)
update_order_status(order, "Processed")
return total_price_with_discount
The code above demonstrates that a function is supposed to do one and only one task.
Know Your Inbuilt Functions:
* Efficiency: Frameworks are typically optimized for performance and efficiency. The built-in functions and modules are designed and implemented with performance in mind, taking advantage of underlying optimizations and best practices. Using these functions can result in more efficient code execution and improved overall performance.
* Reliability, Consistency, Compatibility, Community Support….
purchase_orders = self.env['purchase.order'].search(
[('state', '=', 'draft')])
#very bad
for purchase in purchase_orders:
if purchase.partner_id.id == self.partner_id.id:
#do stuff
#bad
purchase_orders.filtered(lambda rec: rec.partner_id.id == self.partner_id.id)
#do This
purchase_orders = self.env['purchase.order'].search(
[('state', '=', 'draft'),('partner_id','=', self.partner_id.id)])
Use filtered() only when it can’t be done with search()
def some_function(self):
partner_id = self.env['res.partner'].search([])
#bad
partner_name_list = []
for rec in partner_id:
partner_name_list.append(rec.name)
#good
partner_name_list = partner_id.mapped('name')
Caching:
* Caching can improve the performance, for example, if you have a smart tab that traverses to the corresponding record, you can choose to do a search method the get all the corresponding records, but instead, you can store the corresponding record in a Many2many field and access it every time, saves time.
partner_ids = fields.Many2many('res.partner')
def some_function(self):
#some action
self.partner_ids = self.env.['res.partner'].search(['give your condition here']).ids
The above code demonstrates how you can store values in a Many2many field in Odoo. Later on, you can directly access and use these values without ever having to perform the expensive search method again.
To read more about coding guidelines in Odoo 16 ERP, refer to our blog Coding Guidelines in Odoo ERP