Agile Technical Practices Distilled by Pedro M. Santos

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



Copyright Disclaimer:
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.