Friday, September 03, 2010


Sometimes you find a code fragment that’s so great you only wish it was your idea …
  fm: array[Boolean] of Word = (fmCreate, fmOpenWrite);
fs := TFileStream.Create(fileName, fm[FileExists(fileName)]);

Obvious – in retrospective.


  1. Anonymous21:21

    Primoz this is really ingenius, but maybe this line

    fs := TFileStream.Create(fileName, fm[FileExists(fn)]);

    should be

    fs := TFileStream.Create(fileName, fm[FileExists(fileName)]);

  2. Actually not. I believe this a good way to write bad code.
    It should be easy to read, maintain, fix and document.
    An unexperienced developer can easily got trapped in this cool looking snippet and start using it whenever he needs readonly access for example.

    Frankly - I like the idea but just as an idea. I will never use it on my own.

  3. @thereoadtodelphi: Of course. I was pretty-formatting the code and missed this part.

    @Gad D Lord: You have to know when to use it and when not. That's a typical code fragment that I would use in debugging code. Logging, for example. (And that's exactly where the fragment came from.)

  4. Victor22:05

    I think this is actually a case of not being sure what argument to pass when you don't care whether the file already exists or not. Testing with FileExists is not necessary at all! The flags may simply be combined like this:

    fs := TFileStream.Create(fileName, fmCreate or fmOpenWrite);

  5. @Victor: I think if the file does not exists then fmOpenWrite will fail.

  6. Olaf10:44

    If one includes "Math.pas" one can use "IfThen" to make it easier readable:
    fs := TFileStream.Create(fileName, IfThen (FileExists (fileName), fmOpenWrite, fmCreate));

    1. this would be a preffered manner for those reading the code for clarity.

  7. If I run into such code I rewrite it on the spot. It is unreadable and thus likely to create bugs.

    Well written code should look like an English novel, not like a bunch of tricks to save a line.

  8. @Victor: It won't work. Your code will always truncate the file to 0 size even if the file exists.

  9. Anonymous12:42


    function ForceFileStream(const FN: String): TFileStream;
    if FileExists(FN) then
    Result := TFileStream.Create(FN,fmOpenWrite)
    Result := TFileStream.Create(FN,fmCreate);

    fs := ForceFileStream(FileName);

    Not tested, of course ;-)

  10. That's just because Delphi still doesn't support the OPEN_ALWAYS flag of CreateFile()?
    One of the issues of the VCL is it never keeps current with the Windows API...

  11. @LDS: With that I *completely* agree!

  12. Anonymous10:14

    I agree with Jan Derk, this code is not easy to read and should be avoided.

  13. there are several reasons to avoid complex one liners:
    1. harder to understand what is happening.
    2. harder to debug. no local variables to inspect.
    3. it is not obvious how this will fail, making it harder to fix.

    If such a pattern was needed in many places, I would think a CreateOrOpenStream local function would be a much better one-liner. I love clever code, just not in my production applications.


  14. I love posts like this. Goes to show there is always something to learn. @Victor: "fs := TFileStream.Create(fileName, fmCreate or fmOpenWrite);" .. I tried that a few times. It strikes me that my bitmask may have failed because i added "ShareDenyNone" to the mix, which broke the word boundary.

    I added this to my article @

    Credit goes where credit's due ;)