- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Well, see it this way, pointless code is better than code full of errors. ;)
Admin
public static double Post(double fristPost, double sceondPost) { double comment=fristPost+sceondPost; return comment; }
Admin
Why write something passe like "(a + b) * c)" when you could write the much more descriptive "Utils.Multiply(Utils.Add(a, b), c)". Plus, with the Utils class there's no longer any need to worry about that pesky order of operations. Truly only a brillant mind could have come up with that.
Admin
wow, i bet he was an A+ student back in university
Admin
Nice how he at least checks for value under- and overflows
Admin
Meh, including same file everywhere is so lame. Real pro would compile it separately and link it as a library. Bonus points for naming library utils.dll.
Admin
Bah - he's not even reusing his own code:
public static double Add(double firstNo, double secondNo) { double total = Add(new double[] {firstNo,secondNo}); return total; }
Admin
I wonder why he didn't recursively call his ADD function to add inside the ADD function. That would be sweet.
Admin
It is a nice thing. If you ever need to use a multiply where you before added something you just have to change this class. It is perfect. Easy to use. Easy to adjust. Perfect.
Admin
I would say the sum of an array of doubles can be useful as a shorthand. But only in odd applications that happen to use it.
Admin
Wait, I see the problem, he didn't use Inversion Control / Dependency Injection in his library, along with a messaging framework. What happens if I want to change the way AddTwoNumbersTogether works? what happens if I need to use the Util function but someone hasn't written the actual 'addition' logic yet.
AdditionFirstOperandMessage x = new AdditionFirstOperandMEssage(3);
AdditionSecondOperandMessage y = new AdditionSecondOperandMEssage(9);
AdditionMessageRequest = request = NewAdditionRequestMessage(x, y);
IMathsProcessUnit maths = MathFactory.GetInstance(); IUtil utils = UtilFactory.GetInstance(); AdditionResponseMessage response = utils.AddTwoNumbersTogether(request, maths);
Console.WriteLine(response.Result)
of course, really.. I should have MessageBuilders constructing IAdditionRequest instances.
Admin
I actually find the sum of array function useful. Its much more readable if you need to do a lot of array sums. The rest... O_o
Admin
Would have been better if he named it HisName.ArrayMath and just left in the method to do manipulations on arrays. Or maybe even add things for moving averages, statistics, or matrices. Would have been more useful that way.
Admin
I think I get it, the real WTF is that he didn't use operator overloading to create his routines.
Admin
So you'd want him to just open code additions and multiplications? Think of the maintenance nightmare! If the semantics of addition or multiplication changed he'd have to go through and change everything!
Sheesh. Think ahead, people.
Admin
Fixed his implementation, he had duplicated logic..
public static double Add(double firstNo, double secondNo) { double total=firstNo+secondNo; return total; }
public static double Add(double[] valueArray) { double total = 0.00; foreach(double val in valueArray) { // total+=val total = Add(total, val) } return total; }
Admin
Well you know Communism was a good idea 'in general'
Admin
decimal sum = valueArray.Sum();
Admin
Jesus Christ my ass itches.
Admin
Or rather:
Mmmh. Code Reuse. :)
Admin
So did you select the three dumbest functions from the file to showcase? Because if you did, I'm not impressed: the Add(double[]) function seems somewhat useful if you need to sum arrays of doubles at various points in the code. I can also see why you'd like to define a similar method for individual arguments out of a sense of consistency (although it's not very useful on its own).
C++ has functions for sums, differences, counts, maximum/minimum etc. in its standard library, not because it's hard to implement these algorithms from scratch, but because by using standard functions your code becomes clearer (and less vulnerable to bugs due to e.g. typos). If C# does not provide these functions, why shouldn't this guy be allowed to add them?
Admin
I do a lot of work in numerical applications. Often there are differing logics on how to do addition. For example, if we are using NaNs to denote missing values (not an uncommon scenario), we might have differing desires for summing an array of NaNs:
Although this code is pretty useless, it should not be hard to see WHY you might want to have add functions in general. I'd say the array add would eb a good idea for general numeric programming. The two value version, not so much.
Admin
If you combine this re-write from Trondster:
public static double Add(double firstNo, double secondNo) { double total = Add(new double[] {firstNo,secondNo}); return total; }
with this rewrite from Grovsey:
public static double Add(double[] valueArray) { double total = 0.00; foreach(double val in valueArray) { total = Add(total, val) } return total; }
you would really be getting somewhere!
Admin
That is just plain stupid. Oh my...
Admin
The real WTF is that fact he didn't use the params modifier in public static double Add(double[] valueArray).
Admin
I can see the array function being usefull... in many situations though, you rarely have a 'prepared' list of numbers.. by that I mean, the chances are those numbers are already going to be in something like an OrderList, containing Order objects, which in turn will have a list of LineItems... would make more sense to add a 'Total' property to the Order, which enumerates the LineItems, and a Sum to the Customer class which would in turn call the Total of on each Order.
Or to say it another way, you would be enumerating one list, creating an array of doubles to pass into a function which then enumerates through creating a sum. Why bother with the second step? why not just create the total when you loop through the original list?
Anyway, I digress :)
Admin
Because C# DOES have these functions.
double Add(double a, double b){ return a + b; } double Add(double[] arr){ return arr.Sum(); }
Admin
You could borrow a model from Newspeak:
double plusUngood(double a, double b) { return (double) a + b; }
Admin
[quote user="Grovesy"] Or to say it another way, you would be enumerating one list, creating an array of doubles to pass into a function which then enumerates through creating a sum. Why bother with the second step? why not just create the total when you loop through the original list? quote]
That is a good idea. Would be easy to provide a generic function that uses delegates to get the value. That way you can provide a custom delegate to get different values from collection objects, and the generic class will provide a way to get the sum of items based on the delegate you provided; something like GetOrderAmountDelegate.
Admin
Last I checked, .Sum is not available without LINQ, and this code will likely have been written before .NET 3.5 (not to mention that the project might have an earlier .NET version as compile target).
Admin
in general
Admin
Can I bet double or nothing on the next deal?
Admin
I think "firstNo" and "secondNo" are bad variable names. Being used to rejection, I'd think they were booleans with default value "False".
Admin
Admin
Well it's not too bad. This at least makes it obvious that adding two "No"s makes a Nono, which perfectly describes the kind of functions he's written.
Admin
Wow, I did this make-a-utils-library too, back when I was 16 and using Turbo C++ v3.0 for DOS.
It was so handy to have a function that would center a line of text on the screen; printed ((80-line_length)/2) spaces plus the string.
I even learned how to handle variable arguments so I could make a cprintf() function that would take color arguments and print strings with colors; cprintf("%zRED%zGREEN%zBLUE", COLOR_RED, COLOR_GREEN, COLOR_BLUE);
Ahhh, those were the days.
After getting Watcom v10.5 and realizing that none of that stuff from #include <conio.h> was going to work (not to mention Windows programming was a completely different ball of wax); I abandoned the library. It sure was fun to write something so "useful" (in hindsight, completely useless) though.
Sweet.
Admin
Admin
Generally speaking I think it makes more sense to have an array_reduce function and pass "add" as a parameter. Making two kinds for each function -- one to handle two scalars, and another to handle an array of scalars -- only leads to confusion. What if somebody changes the implementation for Add(Scalar,Scalar) and forgets to make a corresponding change in Add(Array)?
Admin
e.g.:
Which, of course, prints...5.
Admin
Admin
As far as I remember, C# does not provide access to the operators as functions. So if he used functional style a lot, all these reduce/map/zip, then he had to have a function like this (and worse, there might have been Identity too) and it's not a WTF at all.
Admin
I would find this funny, except that you've just described all of the posers at codebetter.com. Taking CRUD to a whole new level of infernal machiness...
Admin
Hey Guys...there's this great new invention that i just came up with called "the wheel"! It's awesome, it's round and it rolls and stuff....It's amazing and you should totally buy it from me.
Admin
I would too, if it wasn't for the fact that this was pretty much a copy of something I saw in our code base a few days ago (computes the value of two orders)..... it's all a bit enterprisey here.
Admin
The two-parameter Add could be used as a parameter to a "map" operation.
// apply the $5 surcharge to each item charges = Map(charges, Add, 5);
Though I doubt that's why the function was written...
Admin
No, he was a Utils.AppendCharacter("A", '+') student.
Admin
Pointless code IS code full of errors--just errors waiting to be found.
Admin
Good one! Cracking me up.
Admin
Both wrong, he was a: Utils.Append(Utiles.GetCharFromString(Utils.ToUpperCase(Utils.GetFirstLetter(Utils.Alphabet()))), Utils.Solve("1 ? 1 = 2"), Utils.GetSpace(), Utils.GetCV().GetEducation().GetPosition());
Admin
The real WTF with this is that it's round. It will just roll away from you. It should be flat on the bottom so it doesn't get away.