Magic value (number, string, etc)
Magic strings or any other magic valuesΒΆ
Bad:
int responseCode = GetDeviceResponse();
if (responseCode == 188) {
...
}
Good (this can be reused elsewhere):
const int NoConnection = 188;
int responseCode = GetDeviceResponse();
if (responseCode == NoConnection) {
...
}
Bad:
public class MagicValues
{
public void AcceptCard()
{
var d = new Device();
d.SendCommand(1);
d.SendCommand(2);
d.SendCommand(9);
}
public void DispenseCard()
{
var d = new Device();
d.SendCommand(1);
d.SendCommand(3);
d.SendCommand(9);
}
}
Without magic values:
public class NoMagic
{
private const int Initialize = 1;
private const int Terminate = 9;
public void AcceptCard() {
var d = new Device();
d.SendCommand(Initialize);
d.SendCommand(2);
d.SendCommand(Terminate);
}
public void DispenseCard()
{
var d = new Device();
d.SendCommand(Initialize);
d.SendCommand(3);
d.SendCommand(Terminate);
}
}
Good:
public class NoDuplicateLogic {
private const int Initialize = 1;
private const int Terminate = 9;
public void AcceptCard() {
ExecuteCommand(2);
}
public void DispenseCard()
{
ExecuteCommand(3);
}
private void ExecuteCommand(byte command)
{
var d = new Device();
d.SendCommand(Initialize);
d.SendCommand(command);
d.SendCommand(Terminate);
}
}