Return First, Early Returning?
Return first / Early return: là phong cách viết hàm/khối lệnh mà trả về ngay (return) khi biết kết quả — đặc biệt là khi gặp điều kiện bất thường hoặc lỗi — thay vì bọc toàn bộ logic chính trong các khối if/else lớn.
==> Làm cho luồng chính (happy path) phẳng và dễ đọc, tách rõ guard clauses (kiểm tra điều kiện) ở đầu.
Ví dụ:
// kém:
if condA {
if condB {
// logic chính
} else {
return errorB
}
} else {
return errorA
}
// tốt: early return
if !condA { return errorA }
if !condB { return errorB }
// logic chính
Lợi ích khi sử dụng Early Returning
Cải thiện khả năng đọc (readability) : bỏ nhiều if/else lồng nhau. Người đọc nhìn vào hàm sẽ thấy nhanh các guard clauses, rồi thấy luồng chính liên tục.
Giảm cognitive load: ít nhánh cần theo dõi song song. mỗi đoạn xử lý riêng biệt, dễ dàng debug và tracking lỗi.
Dễ mở rộng: thêm guard mới đơn giản (if + return) mà không phá cấu trúc.
Dễ viết test: Các điều kiện lỗi là những trường hợp tách biệt, dễ viết test đơn lẻ.
Rủi ro khi dùng Early Returning
Nhiều điểm return có thể gây khó khăn khi cần cleanup: nếu mở nhiều tài nguyên (db, file, lock) rồi return sớm, có thể quên đóng connection, unlock,… Giải pháp: có thể dùng defer (go)/ finally (Java).
Kiểm soát luồng khó khăn: nếu nghiệp vụ yêu cầu thực hiện bước B ngay cả khi bước A lỗi, early return sẽ vi phạm yêu cầu. Cần cân nhắc thứ tự và yêu cầu nghiệp vụ.
Có thể khiến hàm có nhiều exit point: khó debug.
Bài viết khác
Build for global scale: AFK scale cube and basic rule to build an application for global scale
REF https://akfpartners.com/growth-blog/scale-cube
Clean code in writing Go program
When writing code in Go, it is important to follow good coding practices to ensure that your code is clean, maintainable, and scalable. Here are some clean code and clean architecture practices to follow: Go coding style: Follow the official Go coding style guide, which includes recommendations for naming conventions, formatting, and documentation. Consistent coding […]
Interfaces in Go and best practice
Interfaces in Go are a set of methods that defines a behavior. A type can implement an interface by defining methods with the same signatures as the methods defined in the interface. This allows for a form of polymorphism in Go, where a single function or method can operate on values of different types, as […]
Basic concepts of Protocol-Oriented Programming (POP) in swift and ios developer
Protocol-Oriented Programming (POP) is a programming paradigm introduced in Swift that emphasizes the use of protocols as a way to define and enforce common behavior for multiple types. POP is a powerful tool for designing and organizing code, and can be used to achieve many of the same goals as object-oriented programming, but with greater […]
Functional Reactive Programming (FRP) and Imperative Programming :which one to use?
the big idea about Functional Reactive Programming (FRP) and Imperative Programming : which one to use? The choice between using Functional Reactive Programming (FRP) and Imperative Programming often depends on the particular problem being solved and the specific requirements of the project. Here are some general guidelines for when to use FRP and when to […]
Functional Reactive Programming (FRP) for ios developer
Functional Reactive Programming (FRP) is a programming paradigm that has gained popularity among iOS developers due to its ability to handle asynchronous events and its emphasis on immutability, composability, and purity. FRP allows iOS developers to write clean, maintainable code that is easy to understand and to scale. In traditional iOS development, handling asynchronous events […]