Click here to Skip to main content
15,886,864 members

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.

 
GeneralRe: Code should be 5 or anything above 7 long... Pin
Sander Rossel28-Jan-12 12:05
professionalSander Rossel28-Jan-12 12:05 
GeneralRe: Code should be 5 or anything above 7 long... Pin
MarvinMartian28-Jan-12 12:35
MarvinMartian28-Jan-12 12:35 
GeneralRe: Code should be 5 or anything above 7 long... Pin
Sander Rossel28-Jan-12 13:05
professionalSander Rossel28-Jan-12 13:05 
GeneralRe: Code should be 5 or anything above 7 long... Pin
MarvinMartian28-Jan-12 13:41
MarvinMartian28-Jan-12 13:41 
GeneralRe: Code should be 5 or anything above 7 long... Pin
Sander Rossel28-Jan-12 23:56
professionalSander Rossel28-Jan-12 23:56 
GeneralRe: Code should be 5 or anything above 7 long... Pin
dawmail33330-Jan-12 0:44
dawmail33330-Jan-12 0:44 
GeneralRe: Code should be 5 or anything above 7 long... Pin
Jyothikarthik_N31-Jan-12 0:23
Jyothikarthik_N31-Jan-12 0:23 
GeneralLinq2Horror PinPopular
Robert Rohde24-Jan-12 21:01
Robert Rohde24-Jan-12 21:01 
Hi all,

I think the following could also qualify as a coding horror but I intentionally wrote it this way (one giant linq statement) because of a discussion with a collegue:
Directory.EnumerateFiles(args[0], "*.cs", SearchOption.AllDirectories).AsParallel().Select(f => new { File = f, Bytes = File.ReadAllBytes(f) }).Select(f => new { File = f.File, Bytes = f.Bytes, Encoding = f.Bytes.Take(3).SequenceEqual(new byte[] { 239, 187, 191 }) ? Encoding.UTF8 : Encoding.Default }).ForAll(f => File.WriteAllText(f.File, f.Encoding.GetString(f.Bytes).Split(new string[] { Environment.NewLine }, StringSplitOptions.None).Select(l => new { Line = l, Index = l.Select((c, i) => new { Char = c, Index = i }).FirstOrDefault(c => c.Char != ' ' && c.Char != '\t') }).Select(l => l.Index == null ? l.Line : l.Line.Select((c, i) => (i < l.Index.Index && c == '\t') ? "    " : c.ToString()).Aggregate((s1, s2) => s1 + s2)).Aggregate((s1, s2) => s1 + Environment.NewLine + s2), f.Encoding));


Here another version where I tried to get a proper formatting:
C#
static void Main(string[] args)
{
    Directory.EnumerateFiles(args[0], "*.cs", SearchOption.AllDirectories).
        AsParallel().
        Select(f => new {
            File = f,
            Bytes = File.ReadAllBytes(f)
        }).
        Select(f => new {
            File = f.File,
            Bytes = f.Bytes,
            Encoding = f.Bytes.Take(3).SequenceEqual(new byte[] { 239, 187, 191 })
                ? Encoding.UTF8 : Encoding.Default
        }).
        ForAll(f => File.WriteAllText(f.File,
            f.Encoding.GetString(f.Bytes).Split(
                new string[] { Environment.NewLine }, StringSplitOptions.None).
            Select(l => new { Line = l, Index = l.
                Select((c, i) => new { Char = c, Index = i }).
                FirstOrDefault(c => c.Char != ' ' && c.Char != '\t') }).
            Select(l => l.Index == null ? l.Line : l.Line.
                Select((c, i) => (i < l.Index.Index && c == '\t')
                    ? "    " : c.ToString()).Aggregate((s1, s2) => s1 + s2)).
                Aggregate((s1, s2) => s1 + Environment.NewLine + s2), f.Encoding));
}


So what does this do?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
args[0] should be a directory where C#-Files are located (*.cs). It opens all files, does a bit of Encoding-analysis (this might not work in every case but its fine for me), replaces Tabs at the beginning of each line with whitespaces (4 for each tab), removes alle whitespaces and tabs in otherwise totally empty lines and writes the file back with the proper encoding.

Interestingly this runs faster than I would have expected. For a codebase with about 8800 files it runs just a few seconds. I would even think (haven't measured it) that searching, reading and writing the files is more time consuming than the rest of the linq in between.


Robert
GeneralRe: Linq2Horror PinPopular
Nagy Vilmos24-Jan-12 21:33
professionalNagy Vilmos24-Jan-12 21:33 
GeneralRe: Linq2Horror Pin
V.24-Jan-12 22:59
professionalV.24-Jan-12 22:59 
GeneralRe: Linq2Horror Pin
Robert Rohde25-Jan-12 1:31
Robert Rohde25-Jan-12 1:31 
GeneralRe: Linq2Horror Pin
BobJanova25-Jan-12 8:16
BobJanova25-Jan-12 8:16 
GeneralRe: Linq2Horror Pin
riced25-Jan-12 10:54
riced25-Jan-12 10:54 
GeneralRe: Linq2Horror Pin
PIEBALDconsult26-Jan-12 2:36
mvePIEBALDconsult26-Jan-12 2:36 
GeneralRe: Linq2Horror Pin
BobJanova27-Jan-12 13:02
BobJanova27-Jan-12 13:02 
GeneralRe: Linq2Horror Pin
PIEBALDconsult27-Jan-12 16:03
mvePIEBALDconsult27-Jan-12 16:03 
GeneralRe: Linq2Horror Pin
riced28-Jan-12 6:27
riced28-Jan-12 6:27 
GeneralRe: Linq2Horror Pin
icestatue23-Feb-12 3:14
icestatue23-Feb-12 3:14 
GeneralRe: Linq2Horror Pin
gavindon1-Feb-12 10:51
gavindon1-Feb-12 10:51 
GeneralRe: Linq2Horror Pin
killabyte25-Jan-12 14:29
killabyte25-Jan-12 14:29 
GeneralRe: Linq2Horror Pin
Robert Rohde25-Jan-12 17:52
Robert Rohde25-Jan-12 17:52 
GeneralRe: Linq2Horror Pin
killabyte27-Jan-12 9:46
killabyte27-Jan-12 9:46 
GeneralRe: Linq2Horror Pin
Sander Rossel19-Mar-12 21:50
professionalSander Rossel19-Mar-12 21:50 
GeneralRe: Linq2Horror Pin
Robert Rohde22-Mar-12 2:07
Robert Rohde22-Mar-12 2:07 
GeneralGoodbye version control Pin
RobCroll20-Mar-12 12:07
RobCroll20-Mar-12 12:07 

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

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