New answers tagged

5

I've factored out the logic for transforming the cell-like string to a number, for example a7 -> 1, z8 -> 26, aa12 > 27 and so on. I've also factored out the logic to replace a character if it falls within the length of the target string. Finally I've parametrised the length of the input string. This approach makes your code more readable and easier to test ...


3

DRY Code The code is very long when it doesn't need to be, and it is very repetitive. There is a programming principle called the Don't Repeat Yourself Principle sometimes referred to as DRY code. If you find yourself repeating the same code multiple times it is better to encapsulate it in a function. If it is possible to loop through the code that can ...


5

You have difficulties getting rid of the repeated code because you're trying to force two different operations into one: parsing html converting the result into OLE color Start with extracting the default value and make it a field: static readonly Color DefaultColor = Color.FromArgb(224, 224, 224); Then create the main API and give it a name that clearly ...


5

Another option, though not without caveats, is to reverse the logic. Something like this should work: private int GetOleFromHTML(string stringRep) { Color c = Color.FromArgb(224, 224, 224); if (!string.IsNullOrWhiteSpace(stringRep)) { try { c = ColorTranslator.FromHtml(stringRep); } catch { ...


5

Since a method has to be executed to get this color, you cannot declare the color as constant. A static readonly field is what comes closest to a const. private static readonly Color DefaultColor = Color.FromArgb(224, 224, 224); private int GetOleFromHTML(string stringRep) { Color c; if (string.IsNullOrWhiteSpace(stringRep)) { c = ...


4

The code in question is using the wrong tool (Regex) to achieve what you want. Using the string representation of a Color to get a string back is not a good way. If one would pass a predefined color like Color.Red into that method it would fail by returning R = , G = , B = because the Color.ToString() returns A string that is the name of this Color, ...


1

Review You don't need to calculate the difference from two temporary values GetUTCDate and GETDATE. SELECT @DifferenceInHoursBetween = DATEDIFF(HH,GetUTCDate(), GETDATE()) Proposed Solution Instead, you could apply the local time zone offset to the specified datetime. SELECT USER_ID, CreateDate AS [DateTime IN UTC], CONVERT(datetime, ...


2

Design Review I challenge the need for a huge enum like this. Even if you find a succint and maintainable way to map this huge enum from a string, what does this provide you in your code? Do you now have a method that needs a huge switch case on the enum to decide proper actions when the interaction is triggered? void InteractWith(InteractionType ...


Top 50 recent answers are included