

Ask HN: I am forced to write awful code What do I do? - RutZap

So the story is: I got a new job as a PHP web developer at a rather respectable digital studio in UK. I was very enthusiastic about this one but now, after 3 months, I am building and maintaining websites using  a CMS built in 2005. The code is far from being up to standards, MVC being something that I can only dream about.<p>To have an idea about the code I am going to tell you that we have a PHP class called dbConfig which stores database credentials. This class is then extended by a DB class which has query functions and other functions that deal with data retrieval from the db. This class (DB) is then extended by a class called front-end which has some functions that generate forms and escape inputs. this class (front-end) is then extended by a Cart class that deals with user logic and creates a sort of a fully functioning shopping cart (amazing i know!).<p>All this badly implemented inheritance is topped up by the fact that global variables are used throughout the site/app. It is an awful procedural codebase (at one point it even uses magic quotes). Everything single job becomes a bodge job. If I have to change something it then creates an error in a totally different part of the website.<p>I have suggested that we move towards a MVC framework or something that will help with this horrible dependencies and will probably decouple most of the things, so that code maintenance would be easier. Sadly, it seems that the company is more interested in spewing up websites and cashing in the money (from some rather big international clients) than in creating better quality software.<p>Now that you know my situation.. here comes the question: What should I do? How can I speed up the transition to a development process that is up to the industry standards? (did I tell you that we still deploy apps with FTP and have htaccess authentication for the web administration area??) I am sure that there are some developers among you that have been in my situation at one point in their career... so what did you do? How can I say this to my boss? I don't want to be responsible for crappy websites that pollute the internet, I don't want to keep on bodging code.... but at the same time, I don't really want to change my workplace (it's well paid, close to home, etc. etc.)<p>Thank you all, and... I'm sorry for the rubbish I upload every day!
======
MattBearman
I was in a remarkably similar situation, PHP dev who worked for a large UK
agency. I held it out for a year, slowly refactoring the code, but the biggest
problem was it was a home made CMS they used, which was badly fragmented
across 50 ish sites. So when I improved the code on one site, I couldn't just
drop that code into all the other sites...

Long story short, I quit and went freelance, best decision I ever made :)

My advice to you would be that (unfortunately) it will never be considered
cost effective in management's eyes to rebuild the existing sites in a decent
CMS / Framework. But you should at least be able to get them to use modern dev
standards on any new builds, or complete rebuild of existing sites.

------
crpatino
Others have offered good advise so far, so let me try to not sound too
redundant.

The first and foremost thing you must understand is that each code base has a
personality of its own. Code bases, like people, change over time but they
rarely deviate from their core character. And the more money that code is
making for someone, the more resistant it grows to change.

So, the thing is, don't put so much of your identity on it. It is not personal
and it is not about you. In due time you will have chances to try your hand
and build something from scratch, but until that day comes you need to work
with other people's code in order to learn what approaches are feasible and
where the pitfalls are.

In this particular case, only you can decide if the current code base is so
much of a mess for you to bother. But you owe to yourself to give it another
try, this time from the perspective that you want to make it not perfect, but
as good as it can be. Once you have given an honest try to assess the
potential of this system, and can explain in detail why that is negligible or
non existent, you may look for the next job with a clean conscience.

~~~
RutZap
Hmm.. you make a very good point. I haven't looked at things from this
perspective before. Thank you!

------
osivertsson
I've been in a similar situation.

I found another developer with the same feelings about our (lack of) code
quality as me and we put our desks opposite each other. This allowed us to
exchange ideas and implement them in a positive environment. This worked well,
and we quickly improved our deployment and testing practices.

But refactoring the code was harder. Developers that had been there 10 years
knew that starting to pull at X would break Y, so they were afraid of change.
Of course they had experience to avoid most problems, and this knowledge was
what made them valuable to the company. At any other company they would have
been sub-par developers.

Our improvement progress slowed down. We got tired of having to work really
hard to get to the most basic industry standard, which were still far from
stuff we could be really proud of.

In the end we both quit, and it was the right decision.

If you feel you have the support from other developers to make a change to
your situation, then stay and do it with energy. But otherwise just move on,
there is no point in wasting your time when there are so many better places.

------
verganileonardo
What does this company want? Money. Why they hired you? To help them make
money.

If you can align your interests (write better code) with their interests (make
more money), you may have a chance to rewrite the code. :)

Prove them that refactoring the code can help you create more sites faster.
With more sites in less time, they will make more money.

------
brd
This situation sounds painfully familiar. Ultimately it comes down to whether
or not you can find someone with enough influence that will support you.

A few tips:

1) Build demos and/or sneak in good code when you can. There will always be a
subset of people that can appreciate good code once they see it working,
increase your chances of finding those people and converting them to the
cause.

2) Do not overwhelm them (them being veteran developers and management). You
will scare them if you talk about starting from scatch or major overhauls.
This is a battle of incremental changes, technically and culturally.

3) Don't burn yourself out. Its a daunting and draining task implementing the
kinds of changes you're talking about in an organzation that doesn't want or
appreciate it. If you can't find the right support, its ok to give up. If you
do give up keep in mind that YOU didn't fail, you're just not in the right
position or in the right organization to make it happen.

------
dave007
Make the argument that a rewrite in MVC would be an investment in a more
extendable and flexible system, i.e. a plugin system could be created to allow
the same CMS to provide more bespoke features such as a readily available blog
or shop while being much more maintainable. A solid base to build on while
saving money in the long run, this is something they could jump on board with.

Or

Make the security argument, a procedural system the size of CMS and site
running on it ->could<\- be riddled with security issues especially as
developers are acknowledging that any addition is a bodged job due to global
variable etc etc. If they are dealing with larger clients this should concern
them enough.

Also if you're lucky enough to be deploying to PHP 5.3 using closures
(<http://php.net/manual/en/functions.anonymous.php>) can save a lot of hassle
when dealing with procedural code.

------
countessa
tldr; It sounds to me like you are in the wrong job. You are obviously keen to
write good quality code in a modern-ish framework/paradigm.

\---

You have to realise that the company you are working for now might be
suffering from industrial inertia - also, unless you are privvy to their
financials, you probably have no idea of any pressure they are under to make
ends meet. In general, selling something that you've already sunk costs into
is more cost-effective than writing something new and then selling it, so if
their primary objective at the moment is to make money and they have a system
that is working, they will probably keep pushing it. It is rare, though not
unheard of, for a company to be so developer focussed that they will undertake
a wholesale refactor of their code base if it is currently bringing home the
bacon. Also in something very old and convoluted (as you've said pull on one
part and another part will collapse), refactoring is often less time-efficient
than simply writing it from scratch. Ask yourself "if I manage to refactor
this into a best-of-breed code, will I be enjoy putting the time and effort
into it on a daily basis? or will it just be an albatross around my neck every
morning until it is done?"

Consider your goals - you could consider staying at the company and learning
about the business end (i.e. actually selling the software), maybe make some
valuable contacts - that's a good goal but it might not be the right goal for
you. If you currently just want to write beautiful code, then find somewhere
else to work - perhaps a more greenfield development without legacy code.

edit to add a potentially relevant link on stackexchange that i've just come
across: [http://programmers.stackexchange.com/questions/155488/ive-
in...](http://programmers.stackexchange.com/questions/155488/ive-
inherited-200k-lines-of-spaghetti-code-what-now)

~~~
RutZap
I agree with you, the business oriented solution is to push the old CMS as
long as they can, to squeeze as much cash out of it as possible.

Anyway... that stackexchange question has some very good answers. Thanks for
sharing!

------
mattm
Do you have any tests in place? (I'm assuming not). You've got to get tests in
place before doing such a major refactoring. Read the book "Working
Effectively With Legacy Code" for a good starting point.

~~~
RutZap
Good point. I'll start with this tonight
[http://www.objectmentor.com/resources/articles/WorkingEffect...](http://www.objectmentor.com/resources/articles/WorkingEffectivelyWithLegacyCode.pdf)
and if it goes well I'll give the book a try. Thanks!

------
antidoh
Write code, get paid, learn, teach the willing, move on when it's right.

Learn? Of course. There's a huge difference in asking people here what to do
in your situation, compared to living your situation with open eyes and mind.
You'll be much better able to avoid _creating_ situations like you're in now,
after having lived it. Hint: it wasn't caused by poor coding practices.

------
RutZap
Just an update:

It seems that in 3 weeks or something like that we will be moving in new
offices.... And there is a promise for improvement... probably this is the
best time to kill off the old home-made CMS and replace it with a framework!
...

PS: Thank you all for your responses... it's a good feeling that I am not the
only one that was in this situation.

------
se85
Like many others here, i've been in the same situation, and like many others
here, I also made the decision to quit.

In my opinion, when things have reached this point its time to go, there is
nothing more you can do there.

Even staying there for the money is ultimately a self defeating decision.

------
johnny22
refactor, refactor, refactor. one bit at a time.

