Comment On Common Functions, not Common Sense

Ben inherited a an application from a fellow employee on the fast track to retirement. How fast? Well, these are some sample methods in a thousands-of-lines-long class file called, appropriately, "Functions". public static string ReturnEmptyStringIfNullElseValue(string value) { if (value == null) { return ""; } else { return value.ToString().Trim(); } } [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Common Functions, not Common Sense

2012-08-08 08:05 • by Matteo (unregistered)
This looks more like C#...

FRIST!

Re: Common Functions, not Common Sense

2012-08-08 08:06 • by PRER (unregistered)
FLIRST! I T HINK THIS IS PHUNNEY

Re: Common Functions, not Common Sense

2012-08-08 08:06 • by snoofle
So, only integers are numeric? So when I count dollars and cents, I'm not using... numbers? Huh?

Re: Common Functions, not Common Sense

2012-08-08 08:08 • by blah (unregistered)
The uppercase method names gives a clue as to it being C# and not Java.

Re: Common Functions, not Common Sense

2012-08-08 08:09 • by Cbuttius
so the real WTF a usual is that people write far too much code to do string parsing, usually because the library doesn't directly support every case you wish to manage.

Might not be the best way to do these but they probably filled a purpose needed at the time to get up and running fast.

Putting them into a separate generic place is a good idea, perhaps StringParsingFunctins might be a better one.

Java was always poor at string parsing unless your string is exactly the "one and only" match for the type.

Re: Common Functions, not Common Sense

2012-08-08 08:15 • by Dave (unregistered)
It's C#. Main clue being that there's no such type as a "bool" in Java (in Java it's "boolean")

Re: Common Functions, not Common Sense

2012-08-08 08:16 • by Infinite Time and Space (unregistered)
There is no WTF here. These functions give you flexibility to handle cases that apply to your situation that the built-ins may not have. Putting them in one class supports easy refactoring. Also, this is a common and reasonable solution when the best available programmer is someone unfamiliar with all of the built in functions of a language. We get paid to get a good enough job done as efficiently as possible. Writing the world's greatest code is often contrary to that. The side effect is that experienced programmers often have to go in and clean up someone else's mess to add new features that the original planners could not have possibly foreseen. It is what we are paid to do. Deal with it or pick another career.

Re: Common Functions, not Common Sense

2012-08-08 08:26 • by Some Jerk (unregistered)
386692 in reply to 386689
Cbuttius:
so the real WTF a usual is that people write far too much code to do string parsing, usually because the library doesn't directly support every case you wish to manage.

Might not be the best way to do these but they probably filled a purpose needed at the time to get up and running fast.

Putting them into a separate generic place is a good idea, perhaps StringParsingFunctins might be a better one.

Java was always poor at string parsing unless your string is exactly the "one and only" match for the type.



no... TRWTF is that everything he wrote is supported directly by the string library (except that null string thing). The null string thing can be handled like this:
var someVal = stringArg ?? "";
basically... this fool needed to look busy so he set about reinventing the wheel day in and day out. I suppose it beats spending all day on wtf.com :p

Re: Common Functions, not Common Sense

2012-08-08 08:27 • by Some Jerk (unregistered)
386693 in reply to 386692
Some Jerk:
Cbuttius:
so the real WTF a usual is that people write far too much code to do string parsing, usually because the library doesn't directly support every case you wish to manage.

Might not be the best way to do these but they probably filled a purpose needed at the time to get up and running fast.

Putting them into a separate generic place is a good idea, perhaps StringParsingFunctins might be a better one.

Java was always poor at string parsing unless your string is exactly the "one and only" match for the type.



no... TRWTF is that everything he wrote is supported directly by the string library (except that null string thing). The null string thing can be handled like this:
var someVal = stringArg ?? "";
basically... this fool needed to look busy so he set about reinventing the wheel day in and day out. I suppose it beats spending all day on wtf.com :p
-- string library = bad choice of words.

double.TryParse(...) and other such interfaces make adequet substitutions as well.

Re: Common Functions, not Common Sense

2012-08-08 08:30 • by Some Jerk (unregistered)
386694 in reply to 386691
Infinite Time and Space:
There is no WTF here. These functions give you flexibility to handle cases that apply to your situation that the built-ins may not have. Putting them in one class supports easy refactoring. Also, this is a common and reasonable solution when the best available programmer is someone unfamiliar with all of the built in functions of a language. We get paid to get a good enough job done as efficiently as possible. Writing the world's greatest code is often contrary to that. The side effect is that experienced programmers often have to go in and clean up someone else's mess to add new features that the original planners could not have possibly foreseen. It is what we are paid to do. Deal with it or pick another career.


True... but implicit with the role of programmer is a subtle expectation of having at least some degree of common sense.

Captcha: transverbero - a verb that looks one way but goes another.

Re: Common Functions, not Common Sense

2012-08-08 08:34 • by Bob (unregistered)
386695 in reply to 386691
I agree with Mr. Time and Space here. Code monkeys, as a breed, are far too eager to start shooting down everybody else's thinking. I believe it's because a lot of us have insecurities about our own abilities and by shooting first all the time it deflects attention from our own weaknesses and makes us feel better about ourselves. It's the one part of this field that really irks me. It's in other fields, too, but it seems to be rampant around programming in general.

Re: Common Functions, not Common Sense

2012-08-08 08:35 • by Some Jerk (unregistered)
386696 in reply to 386690
Dave:
It's C#. Main clue being that there's no such type as a "bool" in Java (in Java it's "boolean")


c/c++ use bool ;P... though member signatures are different.

Re: Common Functions, not Common Sense

2012-08-08 08:35 • by Bob (unregistered)
386697 in reply to 386694
That's exactly what I was talking about in my last post. This person did something slightly different than I would have, therefore they are a complete idiot devoid of any common sense whatsoever. Wow - get over yourself dude.

Re: Common Functions, not Common Sense

2012-08-08 08:50 • by Esse (unregistered)
I like the fact that -12 somehow isn't numeric.

Re: Common Functions, not Common Sense

2012-08-08 08:50 • by Some Jerk (unregistered)
386700 in reply to 386697
Bob:
That's exactly what I was talking about in my last post. This person did something slightly different than I would have, therefore they are a complete idiot devoid of any common sense whatsoever. Wow - get over yourself dude.


you are taking this quite personally. You rename a handful of CLR functions in your past? :p

So this isn't a WTF like an infinate recursion loop... but there is nothing wrong with laughing at someone for basically renaming library functions or writing inferior ones to use in place of those that are built in.

Re: Common Functions, not Common Sense

2012-08-08 08:56 • by Bill Coleman (unregistered)
Someone should have taught this guy how to use temporary variables:

string trimmedValue = value.ToString().Trim();

But the level of paranoia is really high.

Re: Common Functions, not Common Sense

2012-08-08 09:03 • by Some Jerk (unregistered)
What I find truly frightening is this


public static string ReturnEmptyStringIfNullElseValue(string value)

{
// here is my null check
if (value == null)

{

return "";

}

else

{
// MY PARAMETER, which is strongly typed as a string
// IS NOT NULL, so now I just need to make sure it is
// STILL A STRING!
return value.ToString().Trim();

}

}

Re: Common Functions, not Common Sense

2012-08-08 09:23 • by Severity One
386705 in reply to 386692
Some Jerk:
no... TRWTF is that everything he wrote is supported directly by the string library (except that null string thing). The null string thing can be handled like this:
var someVal = stringArg ?? "";
basically... this fool needed to look busy so he set about reinventing the wheel day in and day out. I suppose it beats spending all day on wtf.com :p
Ah, so that's why, because I'm using an empty-string-instead-of-null method in the Java application I wrote and that I happened to be looking at a few minutes ago. Java can get pedantic with null pointer exceptions, and it doesn't have a similar construct.

Re: Common Functions, not Common Sense

2012-08-08 09:30 • by ullamcorper (unregistered)
I find it funny how close to python code the method name "ReturnEmptyStringIfNullElseValue" is.

def ReturnEmptyStringIfNullElseValue(value):

return "" if not value else value

In: ReturnEmptyStringIfNullElseValue(None)
Out: ''

In: ReturnEmptyStringIfNullElseValue("value")
Out: 'value'

Re: Common Functions, not Common Sense

2012-08-08 09:30 • by the beholder (unregistered)
386707 in reply to 386695
Bob:
I agree with Mr. Time and Space here. Code monkeys, as a breed, are far too eager to start shooting down everybody else's thinking. I believe it's because a lot of us have insecurities about our own abilities and by shooting first all the time it deflects attention from our own weaknesses and makes us feel better about ourselves. It's the one part of this field that really irks me. It's in other fields, too, but it seems to be rampant around programming in general.
You should abandon programming STAT and enter sociology. That way everyone would be happy: you would be paid to analyze people and we would be subjected to less posts where someone defends a programmer who clearly either didn't know what he was doing or didn't think things through. And also we would only get a socio-anthropological analysis of why we do what we do when we want it.

And I should become a career counselor :)

Re: Common Functions, not Common Sense

2012-08-08 09:33 • by Spivonious (unregistered)
I don't see a WTF in the first method. I have one of those myself (in VB.NET, where there is no ?? operator). Of course, you could write it with the ?? operator, but not too many people know about that one.

edit - I now know that I can use the If() method in those cases. You learn something new everyday.


As far as the others, what is the built-in function for removing $ and , from strings to get an integer value?

I'll give you a WTF for the IsNumeric function. If he didn't want to use TryParse, then he could have included the VB library and called VB's old IsNumeric function.

Re: Common Functions, not Common Sense

2012-08-08 09:44 • by Remy Porter
386710 in reply to 386708
The lack of a ternary operator is one of my biggest pet peeves in VB. That and the general lack of operator overloading.

Re: Common Functions, not Common Sense

2012-08-08 09:52 • by Marlon (unregistered)
UNICORNS!!!!!

*edit, no this is not spam...

Re: Common Functions, not Common Sense

2012-08-08 09:52 • by Spivonious (unregistered)
386712 in reply to 386710
Remy Porter:
The lack of a ternary operator is one of my biggest pet peeves in VB. That and the general lack of operator overloading.


int i = a < b ? 1 : 2;
Dim i = If(a < b, 1, 2)

Public Shared Operator +(lhs As Integer, rhs As Integer) As Integer
Return lhs + rhs
End Operator

Re: Common Functions, not Common Sense

2012-08-08 09:56 • by Remy Porter
386713 in reply to 386712
If() is computationally the same as the ternary operator, but it is not a ternary operator. The ternary operator is cleaner in terms of syntax, in my opinion.

Re: Common Functions, not Common Sense

2012-08-08 09:56 • by the beholder (unregistered)
hey, if this is a Remy post were are the HTML comments? Unicorns are not enough, you should now that Remy

Re: Common Functions, not Common Sense

2012-08-08 09:58 • by Not an enterprise code monkey (unregistered)
You can do this in one line in Haskell

Re: Common Functions, not Common Sense

2012-08-08 10:02 • by Remy Porter
386716 in reply to 386714
It's a little hard to insert good HTML comments in a CodeSOD.

Re: Common Functions, not Common Sense

2012-08-08 10:02 • by One Coder, One Cattle Prod (unregistered)
386717 in reply to 386708
Seriously? I'm a tester (OK, an SDET) and even I know that C# has a null coalescing operator. Any developer who can't even be bothered to familiarise themselves with simple features of their chosen language needs to get a new job. I hear the helpdesk are hiring.

Re: Common Functions, not Common Sense

2012-08-08 10:05 • by Matt (unregistered)
maybe string.IsNullOrEmpty() did not occur to him.

Re: Common Functions, not Common Sense

2012-08-08 10:13 • by Loren Pechtel
386720 in reply to 386708
Spivonious:
I don't see a WTF in the first method. I have one of those myself (in VB.NET, where there is no ?? operator). Of course, you could write it with the ?? operator, but not too many people know about that one.

edit - I now know that I can use the If() method in those cases. You learn something new everyday.


As far as the others, what is the built-in function for removing $ and , from strings to get an integer value?

I'll give you a WTF for the IsNumeric function. If he didn't want to use TryParse, then he could have included the VB library and called VB's old IsNumeric function.


There are redundant ToString() calls throughout the code. There's also a fear of having a local copy of the data you're working on.

The only WTF I see is the duplication of IsNumeric(). Everything else is simply inefficient but unlike what some posters have said it does *NOT* simply redo the library.

Re: Common Functions, not Common Sense

2012-08-08 10:17 • by Bob (unregistered)
386721 in reply to 386700
Some Jerk:
Bob:
That's exactly what I was talking about in my last post. This person did something slightly different than I would have, therefore they are a complete idiot devoid of any common sense whatsoever. Wow - get over yourself dude.


you are taking this quite personally. You rename a handful of CLR functions in your past? :p

So this isn't a WTF like an infinate recursion loop... but there is nothing wrong with laughing at someone for basically renaming library functions or writing inferior ones to use in place of those that are built in.


I'm just saying it would be more productive if people could point out the flaws of others without having to call them idiots or morons. I mean, I could sit here say "you're an idiot because you can't spell the word infinite" but it would be more constructive to stay focused on the correct way of doing it and not wasting time denigrating the person that you are trying to correct. Too many people act as though they have *never* made a mistake or done something in a less-than-optimal way, which is ridiculous.

Re: Common Functions, not Common Sense

2012-08-08 10:34 • by just me (unregistered)
386723 in reply to 386721
Bob:

I'm just saying it would be more productive if people could point out the flaws of others without having to call them idiots or morons. I mean, I could sit here say "you're an idiot because you can't spell the word infinite" but it would be more constructive to stay focused on the correct way of doing it and not wasting time denigrating the person that you are trying to correct. Too many people act as though they have *never* made a mistake or done something in a less-than-optimal way, which is ridiculous.


+1

btw, how's your son?

Re: Common Functions, not Common Sense

2012-08-08 10:35 • by Cbuttius
From an immediate viewpoint the only one that really looks WTF'y is the last one.

I'm a C++ programmer and don't know C# and how those functions are total rewrites. It's difficult to know exactly what they are doing anyway. What are all these '$' signs doing there? Parsing amounts of money?

I'm sure the C# library has much better string parsing options than C++ (istringstream, oh dear is that the best we can do? boost::lexical_cast wrapper of that throwing unhelpful exception when it fails..)

Now let me explain a concept: objects get printed to strings. But strings don't get loaded into empty objects, strings are parsed by factories which create objects. And it isn't a one-to-one relationship in either direction.

Incidentally the lack of a hexadecimal point is a WTF in C++ and C# although I think C99 supports it.

Re: Common Functions, not Common Sense

2012-08-08 11:04 • by Craig (unregistered)
By my recollection the TryParse() methods were not introduced until .NET 2.0. For all we know this code was written in .NET 1.0 or 1.1, in which case you pretty much had to roll your own IsNumeric (unless you leveraged the Microsoft.VisualBasic namespace, which I personally preferred not to do).

The real WTF is that developers never take into account under what circumstances the code was written. Maybe the developer was using an older version, maybe his boss was hounding him to "just get it done."

There are plenty of things in this business to make fun of, but these examples aren't as egregious as the story makes them out to be.

Re: Common Functions, not Common Sense

2012-08-08 11:10 • by default_ex (unregistered)
That first function is horrendously over complicated for what it says it does.

notNullString = ReturnEmptyStringIfNullElseValue(someString);

notNullString = someString ?? string.Empty;

Now it might just be because I actually know the language, but I find the second version much more readable.

Re: Common Functions, not Common Sense

2012-08-08 11:14 • by Rob (unregistered)
386727 in reply to 386713
Also, its a function, so both arguments will be evaluated.

In C# you can write:

(a == 1) ? b() : c();

In VB

IIf(a = 1, b(), c())

will call both b() and c(), but only return one result!

Re: Common Functions, not Common Sense

2012-08-08 11:17 • by Tuka (unregistered)
The given functions are not perfect. But there is no WTF. The writer of that code might lack experience or tuition. Anyway, the code shall work, is readable and not horrible inefficient.

Since that the real WTF is the fellow 'Ben'. His mention those examples as WTF is an proof that Ben is a arrogant, autocratic goof.

A self-opinionated mind is one of the saves ways into programers hell.

Re: Common Functions, not Common Sense

2012-08-08 11:19 • by Wonk (unregistered)
386729 in reply to 386723
just me:
Bob:

I'm just saying it would be more productive if people could point out the flaws of others without having to call them idiots or morons. I mean, I could sit here say "you're an idiot because you can't spell the word infinite" but it would be more constructive to stay focused on the correct way of doing it and not wasting time denigrating the person that you are trying to correct. Too many people act as though they have *never* made a mistake or done something in a less-than-optimal way, which is ridiculous.


+1

btw, how's your son?


I'm sure it's no laughing matter.

Re: Common Functions, not Common Sense

2012-08-08 11:27 • by Your Name (unregistered)
Java/C# With First Letter Of Function Capatalized. Twitch.

NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOoooooooooooooooooooooooooooooo.............

Re: Common Functions, not Common Sense

2012-08-08 11:49 • by Cbuttius
Ok, an obvious WTF is calling value.ToString() when value is already a string. I assume that all classes inherit from Object() and must have a ToString() method so string also has one, which probably just is implemented as "return this;"

It is quite common in C++ too to have a template to convert things to string and then provide an overload / specialization for strings. At least in C# you don't have the WTF'ery of having to deal with two types of character that might form a string.

Incidentally in C++ if you do this

template< typename T >
T parse( std::string const& str )
{
std::istringstream iss( str );
T t;
iss >> t; // yeah handle errors etc.
return t;
}

It is not just inefficient for T=std::string, it doesn't work properly..

std::string hw( "Hello world" );
std::string parsed = parse<std::string>(hw);

and parsed will be just "Hello"

Doing this for a large array of ints is bad too in that you create a stringbuf for every single one and don't reuse it.

Re: Common Functions, not Common Sense

2012-08-08 11:57 • by pantsman (unregistered)
386735 in reply to 386726
default_ex:
notNullString = ReturnEmptyStringIfNullElseValue(someString);

notNullString = someString ?? string.Empty;


What if this code was written before nullable types were added to the language?

Re: Common Functions, not Common Sense

2012-08-08 12:01 • by nisl (unregistered)
386736 in reply to 386706
ullamcorper:
I find it funny how close to python code the method name "ReturnEmptyStringIfNullElseValue" is.

def ReturnEmptyStringIfNullElseValue(value):

return "" if not value else value

In: ReturnEmptyStringIfNullElseValue(None)
Out: ''

In: ReturnEmptyStringIfNullElseValue("value")
Out: 'value'



It's better not to program than to program in that jiberish.

Re: Common Functions, not Common Sense

2012-08-08 12:03 • by Some Jerk (unregistered)
386737 in reply to 386721
Bob:
Some Jerk:
Bob:
That's exactly what I was talking about in my last post. This person did something slightly different than I would have, therefore they are a complete idiot devoid of any common sense whatsoever. Wow - get over yourself dude.


you are taking this quite personally. You rename a handful of CLR functions in your past? :p

So this isn't a WTF like an infinate recursion loop... but there is nothing wrong with laughing at someone for basically renaming library functions or writing inferior ones to use in place of those that are built in.


I'm just saying it would be more productive if people could point out the flaws of others without having to call them idiots or morons. I mean, I could sit here say "you're an idiot because you can't spell the word infinite" but it would be more constructive to stay focused on the correct way of doing it and not wasting time denigrating the person that you are trying to correct. Too many people act as though they have *never* made a mistake or done something in a less-than-optimal way, which is ridiculous.


Obviously. And I enjoy very much ... mocking my own stupidity every opportunity I get. Spelling is usually my problem however... and it has lead to some rather funny situations involving application wide updates and renames. Flatulantly for me... Microsoft added some safeguards for just such an eventuality.

I suppose I don't remember calling anyone an idiot on this thread however.

Someone wrote that the user was not substituting pre-existing functionality... and I argue that every method was a substitution of pre-existing syntax and/or built in library methods. So hurry up and (CAPTCHA: AUGUE) with me.

Re: Common Functions, not Common Sense

2012-08-08 12:04 • by nisl (unregistered)
386738 in reply to 386717
One Coder, One Cattle Prod:
Seriously? I'm a tester (OK, an SDET) and even I know that C# has a null coalescing operator. Any developer who can't even be bothered to familiarise themselves with simple features of their chosen language needs to get a new job. I hear the helpdesk are hiring.


Usually bozos like that are not qualified for ANY IT job.

Re: Common Functions, not Common Sense

2012-08-08 12:09 • by no laughing matter
386739 in reply to 386725
Craig:
By my recollection the TryParse() methods were not introduced until .NET 2.0. For all we know this code was written in .NET 1.0 or 1.1, in which case you pretty much had to roll your own IsNumeric (unless you leveraged the Microsoft.VisualBasic namespace, which I personally preferred not to do).

The real WTF is that developers never take into account under what circumstances the code was written. Maybe the developer was using an older version, maybe his boss was hounding him to "just get it done."

There are plenty of things in this business to make fun of, but these examples aren't as egregious as the story makes them out to be.

Please use the established meme:
I don't see what's wrong with this code! Does anyone of you bitches know the specs?

Re: Common Functions, not Common Sense

2012-08-08 12:10 • by C-Derb (unregistered)
386740 in reply to 386732
Cbuttius:
Ok, an obvious WTF is calling value.ToString() when value is already a string.

+100
I get that sometimes you may write code only to realize later that there is something similar built into the language. And I agree that all we are presented with is the code and no context to when it was written and what was built into the framework at the time.

But calling string.ToString() is indefensible. Whiskey. Tango. Foxtrot.

Re: Common Functions, not Common Sense

2012-08-08 12:10 • by nisl (unregistered)
386741 in reply to 386725
Craig:
By my recollection the TryParse() methods were not introduced until .NET 2.0. For all we know this code was written in .NET 1.0 or 1.1, in which case you pretty much had to roll your own IsNumeric (unless you leveraged the Microsoft.VisualBasic namespace, which I personally preferred not to do).

The real WTF is that developers never take into account under what circumstances the code was written. Maybe the developer was using an older version, maybe his boss was hounding him to "just get it done."

There are plenty of things in this business to make fun of, but these examples aren't as egregious as the story makes them out to be.


He was using TryParse methods, therefore he was using at least 2.0.

I can see entering .NET using VB, but sticking with it after 10 fucking years of development is just plain stupid - even if the company can't or doesn't allow the switch (move the fuck on). If you're still with VB.NET, you are a moron and incompetent programmer by definition.

"Just get it done" attitude would need to result in inline "if null set "" else set value" pattern - versus writing some confusingly named and what-the-fuck-that-this-function-really-do-let-me-look-it-up-every-time-cause-it-is-impossible-to-remember-ever functions.

What other excuses can you think of?

Re: Common Functions, not Common Sense

2012-08-08 12:10 • by Dave2 (seriously) (unregistered)
386742 in reply to 386696
Also, the "out" keyword is only used in C# I believe. Not java or c/c++.

Re: Common Functions, not Common Sense

2012-08-08 12:13 • by no laughing matter
386743 in reply to 386729
Wonk:
just me:
Bob:

...


+1

btw, how's your son?


I'm sure it's no laughing matter.

Wrong!

Stated it before and will state it again: Bob and i are not related!

Neither am i his son nor is he my uncle!
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment