views:

456

answers:

13

Do you refactor when you see things like this? Or do you just plug your nose and move on?

    public Collection<DataValidationRuleBase> GetFieldValidationRules(String key)
    {
        Collection<DataValidationRuleBase> found = null;
        try
        {
            this.mRules.TryGetValue(key, out found);
        }
        catch (ArgumentException ex)
        {
            //log the error
            Log.Error(ExceptionHandling.BuildExceptionMessage(ex));
            return null;
        }
        return found;
    }
+30  A: 

If you're the new guy, you move on.

It is unlikely to be looked on favorably if you start being a cowboy and refactoring stuff all over the place, especially if it's unrelated to what you're working on. Even if you "know" it will improve the codebase, and you might feel you're "taking initiative", the first time you screw it up and introduce a bug in code that was working before, it will look very bad for you.

I would make note of all the things that you see that need refactoring, and when you've built your reputation and trust at the company, you'll have significantly more leeway to go back and improve things.

womp
That's good advice anytime you're not prepared to ask why it was done a specific way. So often new managers, new programmers, come in and want to change things they see as inefficient, not bothering to learn the reasons that things have come to be as they are.
overslacked
@overslacked - One of the most insightful statements I have seen here at Stackoverflow. +1
JasCav
Definately write notes or implement some kind of "Improvements to Implement" wiki if one doesnt exist, this can then be put into production at some stage and not step on any ones toes. (not to mention give you more work to do if you think you can "fix" everything, if its not broken...)
Mark Redman
@Mark - I like your idea, because it turns the entire "must step lightly because I'm too new" thing around into "being productive and proactive without any risk".
zombat
+10  A: 

if it's working then leave it like this, you could never know what might happen if you will change stuff

Omu
+5  A: 

Strip out anything that identifies your company (or the previous coder) and send them to The Daily WTF :)

GAThrawn
+2  A: 

ya, if your the new guy, then you don't refactor stuff, you just do what the team leader is telling you

Omu
You must be his boss?? ;-)
Austin Salonen
A: 

what does TryGetValue do?

do you need to always track error? (the logging thing)

there is many thing that could be valid with this thing

Fredou
+2  A: 

When you are new, watch out who you tell about "crap" code. It is highly possible that they wrote it, or that they write code the same way. This is a good way to get off on the wrong foot with a new job.

KM
+1  A: 

Unless you work in Dilbert-world, you have a talk with your manager. "Hey, I think I found a problem and this is how I'd like to fix it." Discussion ensues. You might move on to talk to the original developer and some human contact will take place.

How is this a complicated problem?

Bob Cross
@Bob It's complicated because you only have one chance to make a first impression and you don't want to piss off half the development team in the first week because you think their code is inefficient. Though it might pain some people to admit, it remains true: there is a social aspect to software development. We're not coding in vacuums!
Kyle Walsh
@Kyle Walsh, yes, that was my exact point. The question was posted from the point of view of "this code is stupid and I am not, ergo, I should fix it." There was no implication of discussion. I'm saying that the OP should have at least one explicit conversation. Personally, if someone were to come onto my team, find a problem and not tell somebody, I would peg them as either too passive or passive-aggressive. Neither are welcome on the team. We have too much work to do.
Bob Cross
+8  A: 

Don't change working code without good reason.

If you think there is something wrong with some code, then point it out to your team leader, and let him decide what to do with it.

Reasons:

  • You don't know why the original programmer did something. It might be idiocy, or they might have a sound reason for what they did. It's just possible that you are the idiot and have not grasped some subtlety of the code (although the other programmer should have commented it clearly if that is the case!)

  • Any change to the code is something that needs re-testing, and potentially could introduce new bugs in otherwise working code. That shouldn't stop us refactoring to improve code quality, but we shouldn't just change everything we think is wrong without considering it carefully.

  • If you "correct" other peoples' code, you stand a high chance of generating resentment and coding "battles" with your team mates.

  • Your team leader can deal with it approriately (take the original programmer to task, lecture the team, or get it quietly fixed, etc) without anyone knowing that you "pointed the finger" at them. And you'll get brownie points with your boss for pointing out the flaw... Unless it's his code :-)

(You can also check in Source Control to see who wrote the code)

Jason Williams
A: 

You are a little vague on your definition of idiocy in the code.

If it's because TryGetValue doesn't throw an ArgumentException but instead throws an ArgumentNullException, you should be safe in fixing it.

MSDN Dictionary.TryGetValue Method

Austin Salonen
+2  A: 

Rule of thumb: If it ain't broke, don't fix it!

If you're the new guy, and you just happen to come across some smelly code, I wouldn't recommend changing the code unless doing so is part of whatever enhancement you are currently working on.

If it appears to be a major issue, a more acceptable alternative would be to try to write a unit test for the case that fails, and then, depending on how your company normally deals with these issues, either go back and fix it, or let the testers come across it and assign it to the appropriate developer.

This approach is likely to earn you some brownie points, as it shows that you're paying attention and being proactive, without introducing potential side effects in the code.

ph0enix
+1  A: 

I would usually write some unit tests (hopefully this isn't a foreign idea in your company) based on my assumptions and leave it at that.

Occasionally there are (valid?) reasons why the code is strangely written that can only be seen from a failed unit test or when a change is made by a new hire.

Later, when I'm not the new guy and have a better understanding of the code base I would perform the refactoring.

This also has the added benefit of learning how the code works without being a cowboy -No matter how tempting it can be :)

Craigerm
+1  A: 

(Almost) Every company that has a smaller headcount than its workload warrants will have technical debt or messy code. Unfortunately, (almost) every company is like that.

Unless you're in an agile organization or in a project that started out with low technical debt, and kept it that way, fighting this is almost hopeless. Under time pressure, we all write code like that. In fact, even Uncle Bob writes ugly code before he has the time to refactor the bejesus out of it prior to posting it in a book.

If you start refactoring things, you're running the risk of breaking something. If your organization does not have comprehensive unit tests, that's not a risk you should be taking.

Cleaning up technical debt is an organization-wide decision or at least project-side. It doesn't start with the new guy, unfortunately.

Uri
+11  A: 

Bad Programmers:

Refactor it, check it in, and move on without saying a word.

Good Programmers:

"Hey Neil, I came across this method and was wondering why it was written this way.. this return null here seems redundant, mind if I drop it to clean up the code a little bit? Or is there a specific reason you wrote it like that?"

Neil N
Communication ability is rare they say...
Alex Baranosky
exactly, ask if there was a reason, and check. I've foudn many times that code like this is just a side effect of older/legacy code, Of course many times is just ignorance, and just asking the question helps both the code base and the programmer.
webclimber