-
-
Save giggio/2359657 to your computer and use it in GitHub Desktop.
Relação entre testes, design e métricas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pessoal, gostaria de saber como vocês endereçam as questões que farei após explicar o cenário | |
abaixo. Pra mim, é muito claro que todas podem ser respondidas com um belo "depende", contudo, | |
vamos focar as respostas no que atenderia a maioria dos casos (lembrando que esse é um cenário | |
muito simplório). | |
-- CENÁRIO ----------------------------------------------------------------------------------------- | |
Um desenvolvedor codificou inicialmente a classe (escrita em C#) no gist "exemplo_antes.cs". | |
Após aplicar um refactoring, ele chegou no resultado mostrado no gist "exemplo_depois.cs". | |
Algumas métricas foram coletadas antes e depois do refactoring (vide gist "métricas.txt"). | |
Obs.: Esse código foi tirado daqui -> | |
http://whileicompile.wordpress.com/2010/09/14/what-is-too-simple-and-small-to-refactor-as-clean-code | |
-- QUESTÕES ---------------------------------------------------------------------------------------- | |
1) De modo geral, podemos ter como prática que o set de testes deve, no mínimo, ter o mesmo número | |
de testes que o resultado do indicador CC (cyclomatic complexity)? Por exemplo, se um método tem | |
5 de CC, ele deve ter no mínimo 5 testes escritos para ele. | |
Notas: a) estou considerando apenas um assert por método de test. | |
b) não considerando a eficácia do teste escrito. | |
2) Membros privados podem ser ignorados nos testes? Devemos garantir os testes pelos membros | |
públicos observando a cobertura de código dos testes. Isso é suficiente? | |
Nota: sem entrar no mérito da necessidade ou não de 100% de cobertura. A intenção aqui é | |
deduzir uma relação entre métricas (total de membros privados/publicos) e testes. | |
3) Quando, através de métricas, chegamos a conclusão que o código ficou mais complexo, é uma boa | |
estratégia considerar LoC (lines of code) como indicador para comparar "antes" e "depois"? Que | |
outras métricas vocês considerariam? | |
4) Qual a melhor "unidade" para orientarmos a escrita de testes (de unidade, claro): método, classe, | |
assembly, assunto de négócio ou outra? (estou falando aqui de "Testes de Unidade": qual unidade | |
você comumente utiliza?) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Classe inicial, antes do refatoring */ | |
using System; | |
namespace CleanCode.Before | |
{ | |
public class WageCalculator | |
{ | |
public static float Calculate(float hours, float rate, bool isHourlyWorker) | |
{ | |
if (hours < 0 || hours > 80) | |
throw new ArgumentException(); | |
float wages = 0; | |
if (hours > 40) | |
{ | |
var overTimeHours = hours - 40; | |
if (isHourlyWorker) | |
wages += (overTimeHours * 1.5f) * rate; | |
else | |
wages += overTimeHours * rate; | |
hours -= overTimeHours; | |
} | |
wages += hours * rate; | |
return wages; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Classes geradas, após refactoring */ | |
using System; | |
namespace CleanCode.After | |
{ | |
public abstract class WageCalculatorBase | |
{ | |
public float HoursWorked { get; protected set; } | |
public float HourlyRate { get; protected set; } | |
public WageCalculatorBase(float hours, float hourlyRate) | |
{ | |
if (hours < 0 || hours > 80) | |
throw new ArgumentOutOfRangeException("Hours must be between 0 and 80."); | |
HoursWorked = hours; | |
HourlyRate = hourlyRate; | |
} | |
public abstract float Calculate(); | |
} | |
public class WageCalculatorForContractor : WageCalculatorBase | |
{ | |
public WageCalculatorForContractor(float hours, float hourlyRate) | |
: base(hours, hourlyRate) | |
{ | |
} | |
public override float Calculate() | |
{ | |
return HoursWorked * HourlyRate; | |
} | |
public static float Calculate(float hours, float hourlyRate) | |
{ | |
WageCalculatorForContractor payCalc = new WageCalculatorForContractor(hours, hourlyRate); | |
return payCalc.Calculate(); | |
} | |
} | |
public class WageCalculatorForEmployee : WageCalculatorBase | |
{ | |
public WageCalculatorForEmployee(float hours, float hourlyRate) | |
: base(hours, hourlyRate) | |
{ | |
} | |
public override float Calculate() | |
{ | |
if (IsOvertimeRequired) | |
return CalculateWithOvertime(); | |
return CalculateWithoutOvertime(); | |
} | |
protected bool IsOvertimeRequired | |
{ | |
get | |
{ | |
return HoursWorked > 40; | |
} | |
} | |
protected float CalculateWithoutOvertime() | |
{ | |
return HoursWorked * HourlyRate; | |
} | |
protected float CalculateWithOvertime() | |
{ | |
float overTimeHours = HoursWorked - 40; | |
return (overTimeHours * 1.5f + 40) * HourlyRate; | |
} | |
public static float Calculate(float hours, float hourlyRate) | |
{ | |
WageCalculatorForEmployee payCalc = new WageCalculatorForEmployee(hours, hourlyRate); | |
return payCalc.Calculate(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Classes geradas, após refactoring */ | |
using System; | |
namespace CleanCode.After.After | |
{ | |
public class WageCalculator | |
{ | |
public static decimal Calculate(decimal hours, decimal rate, bool isHourlyWorker) | |
{ | |
if (hours < 0 || hours > 80) | |
throw new ArgumentException(); | |
var wages = CalculateOvertime(hours, rate, isHourlyWorker) + CalculateRegularPay(hours, rate); | |
return wages; | |
} | |
private static decimal CalculateRegularPay(decimal hours, decimal rate) | |
{ | |
if (OvertimeExists(hours)) hours = 40; | |
return hours*rate; | |
} | |
private static decimal CalculateOvertime(decimal hours, decimal rate, bool isHourlyWorker) | |
{ | |
var wages = 0m; | |
if (OvertimeExists(hours)) | |
{ | |
var overTimeHours = hours - 40; | |
if (isHourlyWorker) | |
wages += (overTimeHours*1.5m)*rate; | |
else | |
wages += overTimeHours * rate; | |
} | |
return wages; | |
} | |
private static bool OvertimeExists(decimal hours) | |
{ | |
return hours > 40; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Type Member CC LoC | |
-------------------------------------------------------------------- | |
CleanCode.After........................................[18] [27] | |
WageCalculatorBase..............................(8) (9) | |
Calculate() 1 0 | |
HourlyRate.get() 1 1 | |
HourlyRate.set(float) 1 1 | |
HoursWorked.get() 1 1 | |
HoursWorked.set(float) 1 1 | |
WageCalculatorBase(float, float) 3 5 | |
WageCalculatorForContractor.....................(3) (5) | |
Calculate() 1 2 | |
Calculate(float, float) 1 2 | |
WageCalculatorForContractor(float,float) 1 1 | |
WageCalculatorForEmployee.......................(7) (13) | |
Calculate() 2 3 | |
Calculate(float, float) 1 2 | |
CalculateWithoutOvertime() 1 2 | |
CalculateWithOvertime() 1 3 | |
IsOvertimeRequired.get() 1 2 | |
WageCalculatorForEmployee(float, float) 1 1 | |
CleanCode.Before........................................[6] [14] | |
WageCalculator..................................(6) (14) | |
Calculate(float, float, bool) 5 13 | |
WageCalculator() 1 1 | |
CleanCode.After.After...................................[10] [19] | |
WageCalculator..................................(10) (19) | |
Calculate(decimal, decimal, bool) 3 5 | |
CalculateOvertime(decimal, decimal, bool)3 8 | |
CalculateRegularPay(decimal, decimal) 2 3 | |
OvertimeExists(decimal) 1 2 | |
WageCalculator() 1 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using FluentAssertions; | |
using NUnit.Framework; | |
using CleanCode.After.After; | |
namespace Metricas | |
{ | |
[TestFixture] | |
public class WageTest | |
{ | |
[Test] | |
public void SimpleWage() | |
{ | |
WageCalculator.Calculate(hours: 10, rate: 10, isHourlyWorker: true).Should().Be(100m); | |
} | |
[Test] | |
public void OvertimeHourlyWorker() | |
{ | |
WageCalculator.Calculate(hours: 41, rate: 10, isHourlyWorker: true).Should().Be(10m * 40m + 10m * 1m * 1.5m); | |
} | |
[Test] | |
public void OvertimeHourlyContractor() | |
{ | |
WageCalculator.Calculate(hours: 41, rate: 10, isHourlyWorker: false).Should().Be(10m * 41m); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment