I'm working on an installation script, and especially there I have many things that need to be checked and actions that only need to be performed in case of related outcomes. For example, a folder only needs to be created if it doesn't exist:
MyDesktopFolderPath := AddBackslash(ExpandConstant('{commondesktop}')) +
'My Folder';
if not DirExists(MyDesktopFolderPath) then
begin
ForceDirectories(MyDesktopFolderPath); //Create full path
end;
if DirExists(MyDesktopFolderPath) then
begin
//Perform other actions
Instead of this, I could use (exploit?) short-circuit evaluation to make it more compact:
MyDesktopFolderPath := AddBackslash(ExpandConstant('{commondesktop}')) +
'My Folder';
IsMyDesktopFolderExisting := DirExists(MyDesktopFolderPath) or
ForceDirectories(MyDesktopFolderPath);
if IsMyDesktopFolderExisting then
begin
//Perform other actions
This would work too, but I wonder if it is bad practice, mostly because it is less obvious that actions might be performed. Any opinions on that?
Edit: As pointed out by Secure, the code is not completely equivalent, but the discussion topic still stands for other cases.
if (PrintPage()) { // do something }
-- This eliminates the need for theIsPagePrinted
variable. – Robert Harvey Sep 18 '11 at 20:00if PrintPage() { IsPagePrinted = True; }
. Maybe it's just Pascal's (optional) BEGIN/END that makes it look bulky. – thoiz_vd Sep 18 '11 at 20:05DesktopFolderExists
, instead ofIsDesktopFolderExisting
? – kevin cline Sep 19 '11 at 00:39or
operator) is true, the second isn't evaluated. This means thatx or y
is not the same asy or x
, even though in simple boolean logic, they are the same. – kevin cline Sep 24 '11 at 02:28DirectoryExists("x") or MakeDirectory("x")
– kevin cline Sep 24 '11 at 16:36x != null and x.length > 3
, where the second expression will raise an exception when x is null. – kevin cline Sep 25 '11 at 06:04?.
– kevin cline Sep 25 '11 at 15:55value = userValue || defaultValue
– kevin cline Sep 25 '11 at 19:39