내 머리 뒤쪽 어딘가에서 작은 음성이 "C# 코드가 냄새"라고 말합니다.C# : StartsWith와 두 개의 ToUpperInvariant 호출을 결합하는 것보다 더 좋은 방법
private const string STR_ConnectionString = "ConnectionString";
private readonly string upperCaseConnectionString = STR_ConnectionString.ToUpperInvariant();
// a lot further on
string keyAttributeValue = keyAttribute.Value;
if (keyAttributeValue.ToUpperInvariant().StartsWith(upperCaseConnectionString))
{
// some C# code handling a key that starts with "ConnectionString"
}
상수 STR_ConnectionString
은 코드의 다른 위치에도 사용됩니다.
냄새를 제거하는 방법은 무엇입니까?
유효성 검사가 너무 자주 수행되는 경우 확장 메서드를 작성하십시오. – Samich
냄새가 좋습니까? 또는 그것은 나쁜 냄새가나요? 나에게 좋아 보인다 : D – Shai
+1 @Shai 아마 원래의 코드베이스가 .NET 1.1이라고 언급 했어야 만하지만 곧 .NET 4로 업그레이드 될 것이다. .NET 4로 업그레이드 한 후에 과부하로 간다. –