Friday, August 04, 2006

esnips.com: We ruin Google search results!

First, visit here:

http://www.esnips.com/web/MyCPrograms

And view the source code to the two programs that were uploaded by the creator Parul Goyal - or is it Goyal Parul? Whoever it is, they clearly don't know how to write code. Everyone who doesn't know how to write code should not be allowed to post their source code to any place but private, unsearchable forums until they get halfway decent. That way, people like me aren't annoyed. I suspect that websites such as these are the reason MSDN Library search results are dropping off the map.

The first program (reversing a string) simply doesn't work. Well, it displays a string in reverse, but it doesn't actually reverse it. If he gets hired by Cisco, be afraid...very afraid. Here's how I would reverse a string:

1) Not use C. C is such a weak language. I would use C++ instead.
2) Not use C++'s basic stuff. ANSI Standard C++ is such a weak language. I would use Safe C++ instead.
3) To reverse a string, I would use BString*:

#include "Base.h"

int main()
{
printf("%s\n", *BString("parulgoyal").Reverse());

return 0;
}


Does the same thing except it actually reverses the string and THEN prints it. Done and done. I used fewer lines of code, the code is readable and more maintainable, and I know it works without even testing it.

* BString is, in my professional opinion, a cleaner design than STL string and it offers a ton of functionality that is simply missing from STL string. The class is distributed with the "Safe C++ Design Principles" book. 50% of all homework problems given to students are easily solved with this class. By "easily solved", I mean little programs like the above.

It is interesting to note that the same comment lines are used in both programs. The person is clearly copying and pasting the code from a sample named 'laernCode.c' (they really spelled it 'laern' instead of 'learn').

For the second program, it simply won't even work properly. The for-loop conditional is wrong such that the if-statement after the loop will never fire. The person also didn't bother skipping even numbers (2, 4, 6, 8, etc.) and the n/2 limit is wrong (the actual limit is (integer)sqrt(num)). I won't even bother going into the whole mess of carelessly mixing integer data types.

What is sad is that I found the code in question from a quick search on the keyword 'programming' on the esnips website (esnips.com was recommended by someone on a mailing list I moderate) and that a half-dozen people had viewed the source before I did...most likely they assumed that the answers were good and thus committed them to memory. Disseminating information while still being a beginning programmer should get you shot by a firing squad.

The only thing good about esnips is that illegal e-books of some of the better C++ books to learn from are at the top of the list of the search results. Not that I'm advocating the use of illegal materials, I'm just saying that if esnips is going to have publicly available source code that sucks, that source code should come after materials that are actually good sources for learning the language. Koenig & Moo "Accelerated C++" is an infinitely better starting point followed by the e-book "Safe C++ Design Principles" to bring you up to speed on modern programming practices. Between the two, the most useful parts of C++ are covered.

On a more interesting note, my brake line cracked yesterday and dumped brake fluid all over the road. I limped the car home, patched up one part of the crack that I could see, dumped a container of brake fluid into the resevoir, and took it to the shop today. There was a trail of brake fluid the whole way - which was entertaining. I also probably annoyed every driver behind me (sorry about that) because I limited my speed to 30 mph in a 45 mph zone. That, and I was in 2nd gear the whole way to utilize engine drag to add some traction. I'll find out later today if the crack was intentional or if the lines were just worn out from old age (the vehicle does have 56,000 miles on it). Braking the vehicle consisted of alternating between pumping the parking brake and pumping the power brakes and using the engine drag (and having LOTS of space to stop the vehicle). I thought about putting on my hazard lights, but I think most people figured out I had something wrong with my vehicle what with all the squeaky sounds from the parking brakes and the fact that I was going painfully slow down the road. Thusly I was given a fairly wide latitude to drive in. The hazard lights would have just been embarrassing. (I had my hand on it just in case I actually lost control of the vehicle).

No comments:

Post a Comment