Agile Technical Practices Distilled by Pedro M. Santos
Author:Pedro M. Santos
Language: eng
Format: epub
Publisher: Packt Publishing
Published: 2019-06-27T16:00:00+00:00
Connascence of Value (CoV)
Connascence of Value (CoV) occurs when two or more components' values are related or have an intrinsic range of validity in their input not expressed by their primitive types.
Let's consider the initial example:
public class Time {
int _hour;
int _minute;
int _second;
public Time(int hour, int minute, int second){
_hour = hour;
_minute = minute;
_second = second;
}
public string Display(){
return _hour + ":" + _minute + ":" + _second + ":";
}
}
What happens if we instantiate it like this:
var myTime = new Time (27, 76, 82);
That's obviously not a valid time. Is it wise to rely on the user's knowledge about the concept of time? The best thing we can do to improve our design is to try to eliminate it by making invalid states unrepresentable.
What if we do something like this:
public enum Hour {
Midnight = 0,
OneAm = 1,
TwoAm = 2,
ThreeAm = 3,
FourAm = 4,
FiveAm = 5,
...
TenPm = 22,
ElevenPm = 23
}
public class Time {
Hour _hour;
int _minute;
int _second;
public Time(Hour hour, int minute, int second){
_hour = hour;
_minute = minute;
_second = second;
}
public string Display(){
return _hour + ":" + _minute + ":" + _second + ":";
}
}
We can see that now our parameter takes an enumeration called Hour, and it is basically impossible to pass something incorrectly. However, the resulting enumeration becomes quite long and feels like overkill in this situation. What about minutes and seconds? Those would be even longer.
Preceding on that road clearly depends on how the class will be used in its context. If there will be a lot of manual instantiations it might be worth doing it. (Remember, typing is not the bottleneck!) Otherwise, there is a minimum viable solution for this form of connascence.
The real issue with CoV is that it is discoverable only at runtime, so we should try to minimize the response time of the negative feedback and validate the input as soon as possible. In this way, we make sure that this class participates in the information flow if – and only if – its state is valid.
public class Time {
int _hour;
int _minute;
int _second;
public Time(int hour, int minute, int second){
_hour = hour;
_minute = minute;
_second = second;
Validate()
}
public string Display(){
return _hour + ":" + _minute + ":" + _second + ":";
}
private void Validate(){
if(_hour < 0 || _hour > 23)
throw new InvalidHourException();
if(_minute < 0 || _minute > 59)
throw new InvalidMinuteException();
if(_second < 0 || _second > 59)
throw new InvalidSecondException();
}
}
Let's see another example of CoV where the need of validation is not so intuitive:
ublic class Triangle {
int SideA;
int SideB;
int SideC;
public Triangle(int sideA, int sideB, int sideC){
SideA = sideA;
SideB = sideB;
SideC = sideC;
}
}
The values of the sides need to satisfy a particular relation for it to be a triangle, as shown in the diagram. So, here we have the
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12571)
Hello! Python by Anthony Briggs(9916)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9779)
Dependency Injection in .NET by Mark Seemann(9340)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8299)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7082)
Microservices with Go by Alexander Shuiskov(6851)
Practical Design Patterns for Java Developers by Miroslav Wengner(6770)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6708)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6416)
Angular Projects - Third Edition by Aristeidis Bampakos(6115)
The Art of Crafting User Stories by The Art of Crafting User Stories(5644)
NetSuite for Consultants - Second Edition by Peter Ries(5577)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5381)
Kotlin in Action by Dmitry Jemerov(5065)
