Skip to main content

Posts

Showing posts from February, 2015

Abstractions

This is the first part of my series on abstractions.Part 2 - Reused Abstraction PrinciplePart 3 - Dependency Elimination PrincipleCoupling is one of the enemies of a healthy code base. One way to combat high coupling is to introduce abstractions. Too few abstractions is bad. Your code can become coupled. Some of the worst code I've worked with was highly coupled to either the database, UI or both. Working with such code is difficult.Too many abstractions is equally as bad. Abstraction behind abstraction can become so difficult to work with the benefit of abstracting in the first place is lost. Some of the worst code I've worked with was so convoluted with needless abstractions it made any development a tricky process.Most abstractions are not really abstractions at all, but nothing more than simple indirection. Indirection is sometimes required, though it is wrong to confuse with abstraction. IFileWriter is not an abstraction. IReciept that happens to write to the file system …

Guard Clauses and Assertions

Two simple techniques to increase code quality, resilience, and ease debugging scenarios is to use guard clauses effectively and ensure that assertions are used liberally.Guard ClausesAny public method should perform guard clauses to ensure pre conditions are met.Ensures the code's invariants are not broken.Throw exceptions, because these are exceptional issues.Developer and user assistance as it is possible for these clauses to fail at runtime.Here we enforce that any PersonalDetails instance has a forename and surname. A forename must also be at least one character long. As long as these conditions are met, we finally assign the values internally. Guard clauses should also be used on dependencies that are services, checking that a service is not a null instance for example.AssertionsUsed within private methods/functions where required.Should be used for situations that should never happen, e.g the presence of a bug or invalid scenario.Developer only assistance, the user should n…

Consistency

Striving for consistency within a codebase is a good thing. I'm very much someone who believes in applying a consistent formatting style, patterns and practices. However there are two sides to this view.One colleague used to hate different apps that used different frameworks, styles and conventions. This is a fair point, it made switching between them harder. In their eyes, a change to the development process should cascade across all applications.Another colleague used to state that without breaking consistency then improvements and progress would never happen. An equally fair point. However this lead to scenarios where some of the code would be in differing states of consistency, or improvements were avoided because they were too large to implement safely.Like most things in software development, there is rarely a true answer. The best of both worlds is to apply both concepts at varying levels.Applying consistency at package/assembly/module/namespace level works well from my exp…

Value Object Refactoring

After extract method or extract class introducing a value object is one of the most powerful refactorings available. A value object encapsulates a value or concept within your domain. While the term is more formally known from Domain Driven Design, DDD is not a pre-requresite for use. Introducing a value object can be applied to any code base.Some excellent examples of value objects would include CustomerId, Money, OrderId and PhoneNumber. These could all be identified as integers, strings or decimal numbers, but doing so would lead to a series of downsides.Making use of primitive data types to express concepts within an application is a code smell known as primitive obsession. Replacing primitives with value objects is the solution to this smell.Primitive ObsessionDuplication will be thrown throughout the codebase. Both in the form of simple guard clauses, or core domain logic.More tests are required. This ties into the duplication above.Your domain lends itself towards an anaemic mo…