Possibly not the best way to generate file names



  • Continuing the discussion from Unique datetime:

    @smallshellscript said:

    Oh man, that brings back memories...

    The discussion on how to deal with :wtf: user file name requirements reminded me of some code I used to be responsible for. Slightly anonymized (but not much):

    StringBuilder stringBuilder = new StringBuilder(80);
    string text = "\\\\imgserv\\e\\Images";
    if (!Directory.Exists(text))
    {
        text = "c:\\Imgs";
    }
    if (Directory.Exists(text))
    {
        if (LogicalDrive.GetDiskFree(Directory.GetDirectoryRoot(text)) > 500000000L)
        {
            DateTime imageTimeDateStamp = img.imageStamp.imageTimeDateStamp;
            stringBuilder.Append(text);
            
            if (stringBuilder[stringBuilder.Length - 1] != '\\')
            {
                stringBuilder.Append('\\');
            }
            
            stringBuilder.Append((imageTimeDateStamp.Hour < 12) ? "0" : "1").Append(".");
            stringBuilder.Append(imageTimeDateStamp.Minute % 6).Append(".");
            stringBuilder.Append(imageTimeDateStamp.Second);
            string text2 = stringBuilder.ToString();
            
            if (!Directory.Exists(text2))
            {
                try
                {
                    Directory.CreateDirectory(text2);
                }
                catch (Exception exc)
                {
                    LogError("Unable to create directory " + text2, exc);
                }
            }
        }
    } 
    

    Note that this is .NET 1.1 code and that it had already been through one round of my early, hamfisted refactoring. Before I got to it, that StringBuilder was all String concatenation done with +.

    I later added yyyyMMdd parent directories because the not-really-a-timestamp dirs were getting a little full and the VBA macros that were used to view the images from DB listings extracted into Excel were grinding peoples' 2005 era PCs to a halt for 10s of minutes.



  • @smallshellscript said:

    <pre
    stringBuilder.Append((imageTimeDateStamp.Hour < 12) ? "0" : "1").Append(".");
    stringBuilder.Append(imageTimeDateStamp.Minute % 6).Append(".");
    stringBuilder.Append(imageTimeDateStamp.Second);

    Hmm. So at, say 13:31:22, this snippet should yield "1.1.22", but 2 hours, 2 minutes earlier at 11:29:22 we'd get "0.5.22"?



  • @smallshellscript said:

    Note that this is .NET 1.1 code

    I've noticed that many :wtf:s these days come from 1.1 code that hasn't been properly migrated. I consider 1.1 a huge improvement over everything that came before it, but it wasn't until 2.0 that you could write code that you could be proud of.


  • 🚽 Regular

    @tar said:

    Hmm. So at, say 13:31:22, this snippet should yield "1.1.22", but 2 hours, 2 minutes earlier at 11:29:22 we'd get "0.5.22"?

    And at 13:36:22 you get "1.0.22", as well as at every six minute interval from then on, until past midnight.

    Neither being ordered or unique seems to be a requirement.



  • @Zecc said:

    @tar said:
    Hmm. So at, say 13:31:22, this snippet should yield "1.1.22", but 2 hours, 2 minutes earlier at 11:29:22 we'd get "0.5.22"?

    And at 13:36:22 you get "1.0.22", as well as at every six minute interval from then on, until past midnight.

    Neither being ordered or unique seems to be a requirement.

    That's right.

    To give a little context, this code was originally for a machine that generated images every 3 - 4 seconds then passed them to a human operator for inspection. This code generated the folder names for storing the images so that they could be reinspected if there was ever any question about the inspection results later.

    It was then hooked to one that generated images every 1.25 seconds which started to cause volume problems. The image names were "unique" until we added a 2nd 1.25 second machine at which point we got collisions. Fun times.

    Also, the images were grey scale and the .NET default .jpg compression lost some of the inspection features we were interested in. Switched to .bmp to preserver the original image and filled up the storage server's HDD in about a week and a half...



  • @chubertdev said:

    I've noticed that many s these days come from 1.1 code that hasn't been properly migrated. I consider 1.1 a huge improvement over everything that came before it, but it wasn't until 2.0 that you could write code that you could be proud of.

    Not that you can't write good .NET 1.1 code but there were so many VB6'ers making the move to C# (to get away from the stigma, I guess) that you got stuff like this.



  • @smallshellscript said:

    That's right.

    To give a little context...

    It's often the story of how the code got like that is as revealing as the code itself.

    @smallshellscript said:

    a machine that generated images every 3 - 4 seconds then passed them to a human operator for inspection

    This poor human has the worst job in the world : (



  • @tar said:

    This poor human has the worst job in the world : (

    Heh. Usually two humans (from a pool of about two dozen) since I won the argument that the two ends of our product are sufficiently different that context switching between them slows you down enough that you can't meet the timing requirements (I do have to explain this again to new production managers who think they've found an efficiency improvement).

    For up to 8 hours at a time (12 if you're pregnant or on light duties) since I lost the argument, backed by research by companies such as G.E., that tasks like this showed a huge increase in error rates after about an hour to an hour and a half.



  • @smallshellscript said:

    Before I got to it, that StringBuilder was all String concatenation done with +.

    You looked at this mess and decided that changing seven + operators to a StringBuilder was the right thing to do? There's no loops in there and the file system checks will take several orders of magnitude more time than was saved doing so.

    Heck, changing the center section to ...

    text2 = text +
        (text.EndsWith("\\") ? "" : "\\") +
        ((imageTimeDateStamp.Hour < 12) ? "0" : "1") + "." +
        (imageTimeDateStamp.Minute % 6).ToString() + "." +
        imageTimeDateStamp.Second.ToString()
    

    ... would improve readability and probably compile to exactly the same code you wrote. If it didn't, it would be only microseconds slower than the code above.



  • @Jaime said:

    You looked at this mess and decided that changing seven + operators to a StringBuilder was the right thing to do?

    Possibly, I don't really remember - it was some years ago and I hadn't yet learned to profile everything before making optimizations. I do favour String.Concat over using + for string concatenation. This is a holdover from my VB6 days where + and & act slightly differently.

    Note that I wasn't allowed to change the format of the directory names themselves until it started causing problems for people who's opinions mattered.


Log in to reply