Click here to Skip to main content

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrasing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page  Show 
GeneralRe: Facepalm time.memberMarcus Kramer13 Dec '12 - 6:44 
I was trying to save some time in the testing as a failure would have been really obvious to spot in test.
Figure that... You'd think I would know better after how many years of doing this.
I wasn't, now I am, then I won't be anymore.

GeneralRe: Facepalm time.memberAnna-Jayne Metcalfe13 Dec '12 - 22:34 
That's what unit tests are for, surely?
 
Debugger based testing is fundamentally limited by the lack of repeatability intrinsic in having a human poking around during the session, whereas automated tests to do the same thing should (if designed correctly) be absolutely repeatable.
 
As a bonus the tests stick around long after the debug session is ancient history. If someone breaks the code several years from now, the tests will start failing to alert you to that fact.
Anna Rose | [Rose]
 
Tech Blog | Visual Lint

"Why would anyone prefer to wield a weapon that takes both hands at once, when they could use a lighter (and obviously superior) weapon that allows you to wield multiple ones at a time, and thus supports multi-paradigm carnage?"

GeneralRe: Facepalm time.memberKP Lee15 Dec '12 - 23:58 
When I first started in a Microsoft position, I asked why they were renaming procedures that were basically doing the same thing. It was their control method, the old procedures retained old behavior, the new code expected new behavior. After the new was promoted to production, the old was removed from SQL. If the lab started squaking, they knew they missed something.
 
Sure missed that on a later project. About 80% of the existing tests were failing because the sprocs were massively changed and no-one bothered to maintain the tests when they re-wrote the sprocs.
NewsExtract from the code I'm working onmemberSuper Lloyd10 Dec '12 - 16:26 
I think that's the most stupid code I refactored today!
But barely, and there is a lot more! Sigh | :sigh:
 
public XYZ()
{
    _xyz = "0 , 0 , 0";
    string[] parts = _xyz.Split(',');
    Double.TryParse(parts[0], out x);
    Double.TryParse(parts[1], out y);
    Double.TryParse(parts[2], out z);
}
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.

GeneralRe: Extract from the code I'm working onmemberZac Greve10 Dec '12 - 17:57 
D'Oh! | :doh:
 
 
  public void WriteCode(ICodeContent ctx)
  {
    throw new BrainNotFoundException();
  }
 
 

Super Lloyd wrote:
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....

 
Where the work stops. Right?

Bob Dole
The internet is a great way to get on the net.

D'Oh! | :doh: 2.0.82.7292 SP6a

GeneralRe: Extract from the code I'm working onmemberSuper Lloyd10 Dec '12 - 18:13 
You nailed it!
Both remarks! Poke tongue | ;-P
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.

GeneralRe: Extract from the code I'm working onmemberKP Lee11 Dec '12 - 12:08 
Super Lloyd wrote:
public XYZ() { _xyz = "0 , 0 , 0";...
Looks like someone was planning on getting a string feed and forgot to feed the string or forgot to stop destroying the string.
So what did you do to refactor it? Change code to x=0;y=0;z=0;? And keep _xyz = "0 , 0 , 0";? Remove _xyz = "0 , 0 , 0"; and verify the split causes at least 3 parts? Add error checking on the tryparses? Remove XYZ and all references?
GeneralRe: Extract from the code I'm working onmemberSuper Lloyd11 Dec '12 - 22:04 
I got rid of the '_xyz' string, it was only used internally (in addition to x,y,z double) and parse every now and then, to get the value of 'x,y,z'
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.

GeneralRe: Extract from the code I'm working onmemberNemanja Trifunovic13 Dec '12 - 7:29 
There is a design pattern for that: Stringly typed code[^]

GeneralRe: Extract from the code I'm working onmemberSuper Lloyd13 Dec '12 - 13:12 
Ho... I'm learning new power pattern every day!!! Poke tongue | ;-P
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.

GeneralNo foreign keys on the first date.memberBrady Kelly9 Dec '12 - 2:21 
I wrote this little workaround earlier this year. EF complains about an insert where ParentId is set, but allows me to set it with an update.
// TODO Address this cluster-wtf.
using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
{
    if (newProject.ParentId != null)
    {
        // I don't know why I need this, but EF won't insert with a ParentId, but allows me to set it immediately after.
        int parentId = newProject.ParentId.Value;
        newProject.ParentId = null;
        _projectRepository.Insert(newProject);
        newProject.ParentId = parentId;
        _projectRepository.Update(newProject);
    }
    else
    {
        _projectRepository.Insert(newProject);
    }
    scope.Complete();
}

GeneralRe: No foreign keys on the first date.memberKP Lee11 Dec '12 - 8:17 
Assuming this is a SQL Server connection, I don't think you can lay this directly at SQL's door, but you might want to see if triggers are involved and see what they do on insert/update. Maybe the update trigger on the child will insert the parentID in the parent table if it doesn't exist?
 
No, that doesn't make sense, the update should fail with a foreign key constraint failure before it reaches the trigger.
 
So, when it complains, what is its complaint?
GeneralRe: No foreign keys on the first date.memberBrady Kelly11 Dec '12 - 8:34 
Something long winded about referential integrity.
GeneralRe: No foreign keys on the first date.memberKP Lee11 Dec '12 - 11:33 
Brady Kelly wrote:
Something long winded
What else is new with SQL? Big Grin | :-D
GeneralRe: No foreign keys on the first date.protectorPete O'Hanlon11 Dec '12 - 9:10 
Do you have an insert trigger behind this table in the database by any chance? It would seem to be consistent.

*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

"Mind bleach! Send me mind bleach!" - Nagy Vilmos

CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

GeneralRe: No foreign keys on the first date.memberBrady Kelly11 Dec '12 - 16:36 
No. The table is created by EF Code First, nothing but the table itself. Not even indexes but for the PK. I think it's something to do with EF trying to also save another child of the same parent, and clashing somewhere far away.
GeneralRe: No foreign keys on the first date.memberAndrew Rissing13 Dec '12 - 6:42 
As a suggestion, you could debug into the code using the newly open sourced code - http://entityframework.codeplex.com/[^].
GeneralRe: No foreign keys on the first date.memberryan_b200917 Dec '12 - 6:55 
with EF, it helps you out and takes care of all the FK IDs for you Smile | :) You can fill the nested object and let it handle all the FKs (don't even set the ID properties)
ie:
 
parentObject.Project = newProject;
_projectRepositroy.Insert(parentObject);
GeneralRe: No foreign keys on the first date.memberBrady Kelly17 Dec '12 - 7:04 
EF should do that, but somewhere something is amiss - probably my fluent mappings - and I had to resort to the manual update you see in the code excerpt.
GeneralHow do you like your nullsmemberRobert Hoffmann8 Dec '12 - 1:57 
In a project i had to refactor:
 
if (!string.isNullOrEmpty(User.ToString())) {
 
}
 

null.ToString() ?? sweet Big Grin | :-D
GeneralRe: How do you like your nullsmemberZac Greve8 Dec '12 - 7:25 
D'Oh! | :doh: Laugh | :laugh:

Bob Dole
The internet is a great way to get on the net.

D'Oh! | :doh: 2.0.82.7292 SP6a

GeneralRe: How do you like your nullsmemberlewax008 Dec '12 - 15:32 
I'm not sure I get what's wrong here with the given amount of context (except that ToString probably shouldn't return null...but it certainly could if it's overloaded) D'Oh! | :doh:
GeneralRe: How do you like your nullsmemberZac Greve8 Dec '12 - 16:23 
ToString is an instance method, and if the user variable is null, there will be an unhandled NullReferenceException when the ToString() is called, as you cannot call a method on a null object.

Bob Dole
The internet is a great way to get on the net.

D'Oh! | :doh: 2.0.82.7292 SP6a

GeneralRe: How do you like your nullsmemberlewax008 Dec '12 - 16:33 
Right, but there's no reason to expect it might be null at this piece of code without more context. The previous line could be something that gets that user from a function that always returns an non-null object...I just don't think there's enough information to comment on the code as-is.
AnswerRe: How do you like your nullsmemberRobert Hoffmann8 Dec '12 - 23:55 
I understand your logic, but User can be null.
 
But either way turned ToString in this situation is also completely redundant because IsNullOrEmpty already does 2 specific checks. Is the item null, or is the item an empty string.
 
So the correct code, without triggering a NullReferenceException should have been
if (!string.IsNullOrEmpty(User)) {
 
}
 
Thumbs Up | :thumbsup:

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 25 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid