I would like to share with you some tips to deal/prevent NPE in your code: The BIG IMPORTANT rule: do NOT assign / pass / return null value and keep your code as cohesive as possible. Programming tips / techniques: 1. Return an empty collections instead of null. For example: List: Set: Map: 2. Return … Continue reading Avoid NullPointerException in Java
Tag: OO Design Principles
Write Private Functions Not Private Methods
From http://pivotallabs.com/write-private-functions-not-private-methods/ Jared Carroll Sunday, June 30, 2013 Khi một class có quá nhiều private method (được gọi là "Lớp tảng băng trôi" - Iceberg class) thì đó là dấu hiệu cho biết class đó có thể đang làm hơn 1 nhiệm vụ (responsibility). Vì vậy việc tách (extract) những private method đó ra class khác giúp lớp … Continue reading Write Private Functions Not Private Methods
From Primitive Obsession to Domain Modelling
From http://blog.ploeh.dk/2015/01/19/from-primitive-obsession-to-domain-modelling/ A string is sometimes not a string. Model it accordingly. Recently, I was reviewing some code that looked like this: There was a few things with this that struck me as a bit odd; most notably the use of IsNullOrWhiteSpace. When I review code, IsNullOrWhiteSpace is one of the many things I look for, … Continue reading From Primitive Obsession to Domain Modelling
Factory Pattern – Nhà máy của những đối tượng
Factory Pattern Chào mọi người, hôm nay mình sẽ tiếp tục loạt bài về Design Pattern với một pattern mới: Factory Pattern. Pattern này sẽ giúp chúng ta có một hướng giải quyết đúng đắn khi đối mặt với việc phải dùng những lệnh if/else trong khối xử lý để tạo ra những đối tượng … Continue reading Factory Pattern – Nhà máy của những đối tượng
Why extends is evil
Improve your code by replacing concrete base classes with interfaces The extends keyword is evil; maybe not at the Charles Manson level, but bad enough that it should be shunned whenever possible. The Gang of Four Design Patterns book discusses at length replacing implementation inheritance (extends) with interface inheritance (implements). Good designers write most of their code in terms … Continue reading Why extends is evil
The Law of Demeter Principle
The Law of Demeter Principle (Tạm dịch là luật cho người thứ 3 - đùa đó) Một đoạn hội thoại "Giờ anh tính giải quyết thế nào về chuyện này?""Ah... anh...""Tôi không biết, chuyện của anh và cô ấy, tốt nhất là anh nên tự giải quyết riêng với cô ấy. Đừng để đến lúc … Continue reading The Law of Demeter Principle
The Dependency Inversion Principle
The Dependency Inversion Principle (DIP) (Tạm dịch là nguyên lý đảo ngược sự phụ thuộc) Đây là nguyên lý cuối cùng trong 5 nguyên lý cơ bản trong thiết kế hướng đối tượng SOLID. Nguyên lý này chỉ ra rằng các lớp high-level không được phụ thuộc vào các lớp low-level. Thay vì để các … Continue reading The Dependency Inversion Principle
The Interface Sergregation Principle
The Interface Segregation Principle (Nguyên lý phân tách interface) Đây là nguyên lý thứ 4 trong 5 nguyên lý của thiết kế hướng đối tượng SOLID. Nó giúp giảm sự cồng kềnh, dư thừa không cần thiết cho phần mềm và quan trọng hơn là giảm sự kết dính làm hạn chế tính linh động (flexibility) … Continue reading The Interface Sergregation Principle
Liskov Substitution Principle
Liskov Substitution (LSP) Đây là nguyên lý thứ 3 trong SOLID. Nguyên lý này nói rằng các lớp dẫn xuất phải có thể được thay thế bởi lớp cha. Nguyên lý này được Barbara Liskov đề cập lần đầu tiên trong quyển “Data Abstraction and Hiearchy” xuất bản năm 1988. Xin nói thêm rằng Barbara … Continue reading Liskov Substitution Principle
The Open Closed Principle
Open / Closed Principle: Đây là nguyên lý thứ 2 trong SOLID (5 nguyên lý cơ bản của thiết kế hướng đối tượng). Nguyên lý này đề cập đến việc một đối tượng phải luôn MỞ cho việc mở rộng, nhưng ĐÓNG trong việc thay đổi. Nghĩa là bạn phải làm thế nào để tránh … Continue reading The Open Closed Principle