| « There's a Rat in my Computer, Naughty NAS, and other Support Stories | Representative Line - jQuery Search n' Replace » |
public static string ReturnEmptyStringIfNullElseValue(string value)
{
if (value == null)
{
return "";
}
else
{
return value.ToString().Trim();
}
}
That isn't the worst, most useless block of code possible. Neither was his method to turn strings into ints.
public static int ReturnIntValueOfString(string value)
{
if (value != null)
{
if (value.ToString().Trim().Length == 0)
return 0;
else
{
int tmpValue = 0;
// This will fail if a decimal number is passed in
if (int.TryParse(value.ToString().Trim().Replace("$", "").Replace(",", ""), out tmpValue))
return tmpValue;
// Let's handle decimals now
decimal tmpDecimal = 0;
if (decimal.TryParse(value.ToString().Trim().Replace("$", "").Replace(",", ""), out tmpDecimal))
tmpValue = Convert.ToInt32(Math.Round(tmpDecimal, 0));
return tmpValue;
}
}
else
return 0;
}
Or these twin functions:
public static decimal ReturnDecimalValueOfString(string value)
{
if (value != null)
{
if (value.ToString().Trim().Length == 0)
{
return 0M;
}
else
{
decimal tmpValue = 0M;
decimal.TryParse(value.ToString().Trim().Replace("$", "").Replace(",", ""), out tmpValue);
return tmpValue;
}
}
else
{
return 0M;
}
}
|
public static byte ReturnByteValueOfString(string value)
{
if (value != null)
{
if (value.ToString().Trim().Length == 0)
{
return 0;
}
else
{
byte tmpValue = 0;
byte.TryParse(value.ToString().Trim().Replace("$", "").Replace(",", ""), out tmpValue);
return tmpValue;
}
}
else
{
return 0;
}
}
|
Now, Java doesn't have a built-in "isNumeric " function. Ben's codebase wasn't actually in Java, but Ben's predecessor had to implement his own anyway. He found one of the more… clever approaches to the problem.
public static bool isNumeric(string value)
{
bool isNum = true;
for (int i = 0; i < value.Length; i++)
{
if (!"1234567890".Contains(value.Substring(i, 1)))
{
isNum = false;
}
}
return isNum;
}
Hey, you. Yeah you. The one reader on Google+. I don't think you've followed us yet.|
To all of you defending this code: Learn to program proper!
This codebase is a huge W-T-clusterFuck. 1.) Violation of separation-of-concerns / single-responsibility-principle. Implementing too many functionality in each method. 2.) Misleading method names: "ReturnEmptyStringIfNullElseValue" does not return the value, it returns the trimmed value. 3.) Violation of Don't-Repeat-Yourself all over the place. Why the animosity against temporary / local variables? And that replace-code is repeated all over again instead of put into a separate method. 4.) What's the point of first parsing as Int and if that fails, parsing as Decimal? Why not parse it as decimal from the beginning? 5.) Other WTfs already pointed out: Calling ToString() on a string, inefficient coding: using Substring instead of an indexer to retrieve a single Char from a String. 6.) What's the idea of accepting input data and convert it into a number even if it is not a number? One might accept the idea that a money input may be in the form "$123.45", but this code will also accept ",7$56$,18.$9". This is worse than PHP! |
| « There's a Rat in my Computer, Naughty NAS, and other Support Stories | Representative Line - jQuery Search n' Replace » |