Variable names are cheap. Conditional checks are cheap. So why limit yourself when there's a cleaner way to express your code and increase its maintainability.
Use long variable names
var flag = true; // :(
flag isn't very descriptive of what the boolean should be controlling. Don't be afraid to expand the name of variables to express what they really do. JavaScript minifiers' jobs are to shorten the variable names down - it isn't your job to do that - it's your job to write readable and maintainable code.
var shouldSaveDocument = true; // :)
Don't be afraid to break down conditionals
// :( if (doc.hasChanged && !doc.isReadOnly && fs.hasFreeSpace && user.wantsToSave) { doc.save(); }
When doing some sort of precondition checking, don't throw them all into one if. The logical operators become a jumbled mess. It's hard to change or debug them especially when they are all on one line. Don't be afraid to break down conditionals and retest.
var shouldSaveDocument = true; // retest the flag here so that operations can be added/removed easily and can see them clearly in a diff. if (shouldSaveDocument && !doc.hasChanged) { trace('not saving unchanged document'); shouldSaveDocument = false; } if (shouldSaveDocument && doc.isReadOnly) { trace('not saving read-only document'); shouldSaveDocument = false; } if (shouldSaveDocument && !fs.hasFreeSpace) { trace('not saving document when there is not any freespace'); shouldSaveDocument = false; } if (shouldSaveDocument && !user.wantsToSave) { trace('not saving document as the user does not want to'); shouldSaveDocument = false; } if (shouldSaveDocument) { doc.save(); }