12,950,272 members (68,401 online)

# 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, embarrassing 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.

 Re: Misaligned elephants Matthew Faithfull24-Jul-13 5:24 Matthew Faithfull 24-Jul-13 5:24
 Re: Misaligned elephants jschell24-Jul-13 9:26 jschell 24-Jul-13 9:26
 Message Automatically Removed Brisingr Aerowing19-Jul-13 6:46 Brisingr Aerowing 19-Jul-13 6:46
 Re: Extension Methods Andrew Rissing19-Jul-13 7:06 Andrew Rissing 19-Jul-13 7:06
 Re: Extension Methods Richard Deeming19-Jul-13 7:20 Richard Deeming 19-Jul-13 7:20
 Re: Extension Methods Gary Wheeler19-Jul-13 7:21 Gary Wheeler 19-Jul-13 7:21
 Re: Extension Methods TnTinMn19-Jul-13 8:00 TnTinMn 19-Jul-13 8:00
 Re: Extension Methods Jacek Gajek19-Jul-13 8:08 Jacek Gajek 19-Jul-13 8:08
 Posting in this particular forum triggers a find-what-is-bad thinking. Sorry. 1. It's O(n^3) which is not good. Did you consider working on sorted collections? Then it would be just O(n). (more precisely, O(max{n,m})). Why O(n^3): 1. foreach loop 2. Contains method which searches notRemove 3. Remove(itm) method which has to find index of itm (in worst case, n calls to Equals method) and, if it's an array list, it has to shift all elements by one which makes it even worse. Besides, I though that you cannot edit a collection inside foreach. 2. I method 2: Why copy all elements to a new array? Without it, it would be memory complexity of Ω(1), in situ operation. Now it's Ω(n) because it allocates a new array in the process. 3. Formatting horror, but maybe it was screwed up during pastying*. ```if (notRemove.Contains(itm)) continue; source.Remove(itm); ``` Shouldn't it be: ```if (notRemove.Contains(itm)) continue; source.Remove(itm); ``` IF it is known that all items in `notRemoves` appear in the `source` exactly once, then what about: ```public static void RemoveAllBut(this ICollection source, params T[] notRemove) { source.Clear(); foreach(var x in notRemove) source.Add(x); // because we don't have source.AddAll, unfortunately }``` which is O(m) and Ω(1). * -- "pastying" - is it a correct spelling? (derived from a verb "paste"). Greetings - Jacek
 Re: Extension Methods Richard Deeming19-Jul-13 8:55 Richard Deeming 19-Jul-13 8:55
 Re: Extension Methods Jacek Gajek19-Jul-13 9:06 Jacek Gajek 19-Jul-13 9:06
 Re: Extension Methods PIEBALDconsult19-Jul-13 8:09 PIEBALDconsult 19-Jul-13 8:09
 Re: Message Automatically Removed lewax0019-Jul-13 9:35 lewax00 19-Jul-13 9:35
 Re: Message Automatically Removed ryanb3119-Jul-13 10:22 ryanb31 19-Jul-13 10:22
 Re: Message Automatically Removed lewax0021-Jul-13 11:03 lewax00 21-Jul-13 11:03
 Re: Message Automatically Removed pkfox29-Jul-13 22:26 pkfox 29-Jul-13 22:26
 This one is for the Java experts Grimlock18-Jul-13 0:56 Grimlock 18-Jul-13 0:56
 Re: This one is for the Java experts Nagy Vilmos18-Jul-13 1:52 Nagy Vilmos 18-Jul-13 1:52
 Re: This one is for the Java experts Fredrik Bornander18-Jul-13 2:10 Fredrik Bornander 18-Jul-13 2:10
 Re: This one is for the Java experts Grimlock18-Jul-13 2:27 Grimlock 18-Jul-13 2:27
 Re: This one is for the Java experts Nagy Vilmos18-Jul-13 3:54 Nagy Vilmos 18-Jul-13 3:54
 Re: This one is for the Java experts svella23-Jul-13 3:57 svella 23-Jul-13 3:57
 Re: This one is for the Java experts Brisingr Aerowing18-Jul-13 6:40 Brisingr Aerowing 18-Jul-13 6:40
 Re: This one is for the Java experts Amitosh S.M.18-Jul-13 15:53 Amitosh S.M. 18-Jul-13 15:53
 Re: This one is for the Java experts ExcellentOrg22-Jul-13 21:30 ExcellentOrg 22-Jul-13 21:30
 Re: This one is for the Java experts englebart23-Jul-13 2:57 englebart 23-Jul-13 2:57
 Last Visit: 31-Dec-99 18:00     Last Update: 25-May-17 19:13 Refresh « Prev1...146147148149150151152153154155 Next »

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 26 May 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid