Have Confidence in Your Skills By Ruling Out External Resources
If something goes wrong in our code base the default position for most developers is "what did I code wrong?". Instead, check your dependencies first and trust your code and tests more.
Join the DZone community and get the full member experience.Join For Free
a while ago, i wrote a customized dashboard for users of an ecommerce system. it was something that i was proud of it when it was released to the users of the system.
before deploying the code to a development server, i ran the dashboard on my local machine. i ran the unit tests and integration tests. i finally connected to the database to confirm everything worked. everything worked great. fifteen minutes later, i deployed my code to the dev server. once completed, i started loading the web page to test it.
it failed. why did it fail?
i started looking through the code to check and double-check where it was failing.
the key to this was that it was connecting to the same database on my local machine as it was on the development server. for you microsoft devs out there, the web.config's connection string was the same. this was fine because i wanted to test against the same database.
so why wasn't it connecting to the database?
"i don't get it. everything works fine on my machine."
then i would hear one of my ex-bosses's voice saying:
"we're not shipping your machine to all our customers!"
we've all been there when we've tested code. everything works on your machine, but when you deploy it, everything blows up and you don't know why. before i tell you the solution, let me explain another situation that occurred.
two days ago, i overhead some team members trying to figure out a particular issue. the qa team submitted it as a bug. the developer modified a piece of code and ran his unit tests and integration tests. the integration tests were failing.
he modified 2-3 lines of code, but didn't touch a section of code where the error occurred. the developer kept saying, "how could it be breaking when i didn't touch that code?"
i mentioned to one of the devs, "if i was a betting man, i would place my money on data."
after researching the issue, it turns out the data did in fact change. the record for this particular integration test changed. once the data was updated, everything started working again. his code wasn't the issue, it was the environment around it.
we are taught to write code to solve solutions, but our code needs to "expect the unexpected." the environment can change at any time and sometimes, we need to code for worst case scenarios. not all the time, but most of the time.
back to my original problem: why couldn't i connect to the database and it was giving me a weird error message? the database was fine and the data didn't change.
the problem was a hardware issue. the database server ran out of disk space and couldn't open the files to function. hence, the error. the code was fine. the environment around the code caused issues. it wasn't code.
as you write and deploy code in your career, trust the code you write. create unit tests and integration tests to validate everything happening in the system. i'm not saying from this point on, it's everyone else's fault and not your precious code you wrote. ;-) if your unit tests and integration tests pass and your code fails on another machine, don't rule out the external resources.
don't immediately go to code. it may be external resources creating the errors.
has anyone else had code "work on their machine?" post your comments below with your experiences.
Published at DZone with permission of Jonathan Danylko, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.