The Pitfalls of Cut-and-Paste Coding
Join the DZone community and get the full member experience.Join For Free
we’ve all been guilty of it at one time or another. when starting out with a language or framework that you’ve never used before, you often have no choice. what i’m talking about is the act of “copy paste coding,” and it’s as common in the programming world as chewing gum under seats. when you copy and paste other developers' code into your application, it’s important to fully understand what the code does before you continue. otherwise, you risk joining the many fools who have gone before you.
when i was in primary school, there was a really smart kid in our class; let’s call him joshua. he got top marks pretty much all the time. my class also had a not-so-smart kid called david; to be fair there were a few davids in my year group in primary school. david was the kind of kid who’d do anything for a shortcut, so he used to sit horizontally behind joshua and take notes from his test sheets every time we had a maths test. primary school obviously seemed a lot harder when you were 10.
this went on for a couple of months; remember that i mentioned that joshua was a pretty switched on kid and hadn’t missed the gist of what was going on. so one day we had another test and we all sat down and got stuff in. joshua smashed the test in only 10 of the 60 minutes we had been given – to say he made us all feel inferior when he put his pencil down was an understatement.
after a slight pause, he then preceded to pull out a second test sheet and really start his test. joshua had taken two test sheets and written fake answers to all the questions in the test.
the look of shock on david’s face was priceless.
time to go on safari
recently a lot of the sites that i look after have been receiving some serious elmah spam. so much so that i've had to implement elmah filters just to get the signal-to-noise ratio of my application’s exceptions back to a manageable threshold.
the offending exception that i've continually seen:
system.web.httpexception: a potentially dangerous request.path value was detected from the client (:).
so someone is making bad requests to my web applications.
from looking at my elmah logs over time this is from a number of different sources ion different ip address ranges around the globe.
all of them have the same pattern to their exception.
request path info: ”https:/mywebsite.com/”
user agent: ”test certificate info”
they are all missing the second “/” in the https:// of my ssl site’s addresses.
so i think to myself:
“why the hell does every site that i manage that uses ssl seem to be getting the same exceptions?”
my exceptions where all for different websites on a number of different servers.
so someone out there is scanning websites with a piece of code that puts bad paths into urls, and uses the user-agent string “ test certificate info ”.
following the paper trail
so i set out on the interwebs in search of answers.
i can’t be the only one experiencing this?
and it turns out that i’m not . it seems that more than a few of us are experiencing the same thing .
one of the responses to the above linked stackoverflow post seems to nail what has happened:
my guess someone read this and didn't end up changing the example code.
the link in the quote above is to an msdn c++ article that walks through making an ssl request – but until recently the code sample on the msdn post had an error that wrote an invalid http header while making its request.
and therein lies the problem.
a copy of a copy, of a copy, of a copy
just like my not-so-intelligent primary schoolmate david mentioned earlier, when whoever wrote the piece of software that’s been visiting my sites was cranking out code in a haze of doritos and pepsi, they copied someone else’s work – and with it they copied a bug.
unlike david however when you’re a developer this is pretty normal behaviour and wouldn’t be considered unsavoury at all – hell, open source breeds on the act of copying.
however there is one thing that like david we all have to be careful of, as copying other developers code from the internet does have it’s own set risks that you need to be aware of.
if you copy someone else’s code and you don’t fully understand what it is doing , then you probably aren’t in a place to use it in a production environment.
whoever wrote the web spider that is out there indexing all my ssl website’s and causing my elmah error spam obviously didn’t note the issue in the code sample they copied from. like them if you do the same, you might end up with a piece of code in your application that simply makes you look like an amateur.
and it doesn’t have to be that way.
the above image was basically a sarcastic joke at the fact that a lot of code in world has simply not really been properly tested . and i don’t meant test-driven, test-first level tested – i mean the level of testing where it hasn’t really ever been used outside of simply knocking together a proof of concept sample for a blog post.
i’ve been guilty of this before when i’ve posted things on this very blog that have needed an update after the fact.
so if you’re out there copying code from other developers, open source projects or blog posts – make sure you read the logic you borrow thoroughly, take the time to understand it, and for your own sake make sure you test anything that spiders websites all over the internet before you hit the launch button.
my event logs will thank you.
Published at DZone with permission of Douglas Rathbone, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.