[M3devel] BOOLEAN (Was: CVS Update: cm3)

Jay jayk123 at hotmail.com
Tue Jan 8 14:18:10 CET 2008


Henning, agreed. I said named parameters help, understood that they are up to the caller to use though.
Agreed about avoiding negative options, better to have an option that is positive, and leave the users as true and false, or..er..oops, that's what we are also saying not to use. I'm definitely a proponent of this second one. I actually haven't heard/used it for locals, but more for, uh, parameters. (I see I'm being hypocritical again -- don't use booleans...but if you do, use positive ones..), makes sense for locals too. On the other hand, I also like to default things to false/zero, globals, and locals, for easier/more efficient initialization, lately in C I write:
 
void F()
{   type1 local1 = { 0 }
   type2 local2 = { 0 }
   ..
}
 
for all my small locals. A good compiler will optimize away at least some of them (as long as they aren't "out parameters") and I've been too burned by uninitialized locals too much, and the code is usually fairly efficient otherwise (again, the initialization can often be removed by the compiler), to desire optimizing them..
 
So sometimes name things to favor false being the default or more common value.
For globals esp. that saves space in the resulting executable.
For locals, well, if you initialize all your locals to zero, the compiler can optimize that, into a small loop for zeroing...if it was REALLY smart, it'd move the locals that are zero-initialized to be adjacent, but that could trade off with other factors so I don't assume it happens.
 
 - Jay



> Date: Tue, 8 Jan 2008 12:29:30 +0100> From: lemming at henning-thielemann.de> Subject: BOOLEAN (Was: CVS Update: cm3)> To: wagner at elegosoft.com> CC: jayk123 at hotmail.com; m3devel at elegosoft.com> > > On Tue, 8 Jan 2008, Olaf Wagner wrote:> > > Quoting Jay <jayk123 at hotmail.com>:> >> > > Style tangent: Have people heard the advise that boolean parameters> > > are bad, because at the callsite esp. they don't give much meaning?> > > What is TRUE? What is FALSE? Enums or named parameters are clearer.> >> > I tend to agree, but this might get religious ;-)> > In Modula-3 you can explicitly write> f (switch := TRUE);> Problem is of course, that you only can but you need not.> > > Another advise I like to give about BOOLEANs is, that one should use> positive flag names, e.g. not> skipIntro := FALSE;> but> showIntro := TRUE;> > not> disableGUI := TRUE;> but> enableGUI := FALSE;> 
_________________________________________________________________
Make distant family not so distant with Windows Vista® + Windows Live™.
http://www.microsoft.com/windows/digitallife/keepintouch.mspx?ocid=TXT_TAGLM_CPC_VideoChat_distantfamily_012008
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20080108/914ad448/attachment-0002.html>


More information about the M3devel mailing list