tag:blogger.com,1999:blog-29331675.post6760177903235199064..comments2024-03-05T17:37:00.995+01:00Comments on The Delphi Geek: Ingenious!gabr42http://www.blogger.com/profile/06903558857617342477noreply@blogger.comBlogger15125tag:blogger.com,1999:blog-29331675.post-7298200972978525602016-02-17T15:54:39.378+01:002016-02-17T15:54:39.378+01:00this would be a preffered manner for those reading...this would be a preffered manner for those reading the code for clarity.Unknownhttps://www.blogger.com/profile/08316030693504570847noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-24558112776372548552011-02-02T19:40:43.887+01:002011-02-02T19:40:43.887+01:00I love posts like this. Goes to show there is alwa...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.<br /><br />I added this to my article @ http://delphimax.wordpress.com/2011/02/02/smarting-up-your-classes-lookup-tables-and-how-to-use-them/<br /><br />Credit goes where credit's due ;)$ABBAhttps://www.blogger.com/profile/01849703745444988905noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-9339418525914478262010-09-15T15:47:51.035+02:002010-09-15T15:47:51.035+02:00there are several reasons to avoid complex one lin...there are several reasons to avoid complex one liners:<br />1. harder to understand what is happening.<br />2. harder to debug. no local variables to inspect.<br />3. it is not obvious how this will fail, making it harder to fix.<br /><br />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.<br /><br />WWarrenhttps://www.blogger.com/profile/04053407632823479165noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-70245874094986981992010-09-06T10:14:09.758+02:002010-09-06T10:14:09.758+02:00I agree with Jan Derk, this code is not easy to re...I agree with Jan Derk, this code is not easy to read and should be avoided.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-29331675.post-75627645802989399132010-09-04T15:12:59.315+02:002010-09-04T15:12:59.315+02:00@LDS: With that I *completely* agree!@LDS: With that I *completely* agree!gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-65356163567052616192010-09-04T15:07:12.319+02:002010-09-04T15:07:12.319+02:00That's just because Delphi still doesn't s...That's just because Delphi still doesn't support the OPEN_ALWAYS flag of CreateFile()?<br />One of the issues of the VCL is it never keeps current with the Windows API...LDShttps://www.blogger.com/profile/04633789460476801953noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-37451724007572755702010-09-04T12:42:18.009+02:002010-09-04T12:42:18.009+02:00...or
function ForceFileStream(const FN: String)......or<br /><br /><br />function ForceFileStream(const FN: String): TFileStream;<br />begin<br /> if FileExists(FN) then<br /> Result := TFileStream.Create(FN,fmOpenWrite)<br /> else <br /> Result := TFileStream.Create(FN,fmCreate);<br />end;<br /><br /><br />fs := ForceFileStream(FileName);<br /><br /><br />Not tested, of course ;-)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-29331675.post-78552455395541090442010-09-04T12:24:09.350+02:002010-09-04T12:24:09.350+02:00@Victor: It won't work. Your code will always ...@Victor: It won't work. Your code will always truncate the file to 0 size even if the file exists.gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-75447120462747069482010-09-04T12:08:15.547+02:002010-09-04T12:08:15.547+02:00If I run into such code I rewrite it on the spot. ...If I run into such code I rewrite it on the spot. It is unreadable and thus likely to create bugs.<br /><br />Well written code should look like an English novel, not like a bunch of tricks to save a line.Jan Derkhttps://www.blogger.com/profile/02482104835570444722noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-77171598413956374342010-09-04T10:44:17.752+02:002010-09-04T10:44:17.752+02:00If one includes "Math.pas" one can use &...If one includes "Math.pas" one can use "IfThen" to make it easier readable:<br />fs := TFileStream.Create(fileName, IfThen (FileExists (fileName), fmOpenWrite, fmCreate));Olafnoreply@blogger.comtag:blogger.com,1999:blog-29331675.post-8343664066664256782010-09-04T07:59:39.747+02:002010-09-04T07:59:39.747+02:00@Victor: I think if the file does not exists then ...@Victor: I think if the file does not exists then fmOpenWrite will fail.runnerhttps://www.blogger.com/profile/10898827626049604220noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-16536304321399974822010-09-03T22:05:07.281+02:002010-09-03T22:05:07.281+02:00I think this is actually a case of not being sure ...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:<br /><br />fs := TFileStream.Create(fileName, fmCreate or fmOpenWrite);Victornoreply@blogger.comtag:blogger.com,1999:blog-29331675.post-16147777702249524282010-09-03T21:41:25.480+02:002010-09-03T21:41:25.480+02:00@thereoadtodelphi: Of course. I was pretty-formatt...@thereoadtodelphi: Of course. I was pretty-formatting the code and missed this part.<br /><br />@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.)gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-86874201205602626792010-09-03T21:37:09.059+02:002010-09-03T21:37:09.059+02:00Actually not. I believe this a good way to write b...Actually not. I believe this a good way to write bad code.<br />It should be easy to read, maintain, fix and document.<br />An unexperienced developer can easily got trapped in this cool looking snippet and start using it whenever he needs readonly access for example.<br /><br />Frankly - I like the idea but just as an idea. I will never use it on my own.Gad D Lordhttp://www.mtgstudio.comnoreply@blogger.comtag:blogger.com,1999:blog-29331675.post-7202180265233838062010-09-03T21:21:57.946+02:002010-09-03T21:21:57.946+02:00Primoz this is really ingenius, but maybe this lin...Primoz this is really ingenius, but maybe this line<br /><br />fs := TFileStream.Create(fileName, fm[FileExists(fn)]);<br /><br />should be<br /><br />fs := TFileStream.Create(fileName, fm[FileExists(fileName)]);Anonymousnoreply@blogger.com