4 ms·
Poll: Spaces in if statements
Do you put spaces between the "if" and parenthesis in if statements? For example:
if (test)
vs
if(test)
- kpumuk 15y agoDefinitely put spaces
- mikelikespie 15y agoYes... BSD style FTW (aside from hard tabs I suppose) http://en.wikipedia.org/wiki/Indent_style#BSD_KNF_style http://en.wikipedia.org/wiki/Indent_style#BSD_KNF_style
- sim0n 15y agoI personally prefer if( test )
- BSeward 15y agoThis is my personal preference too. I like that the condition is visually separated from the wrapping. I'm also prone to borderline-too-complex conditions, and this syntax makes those easier to focus on.
- lucisferre 15y agoDo you also write that way ( if you know what I mean ).
- BSeward 15y agoWith occasionally obtuse syntax: yes; with bonus space parentheticals: no (see?). Most non-code writing is going to be read linearly, rarely more than onc. Parentheticals in a reading context are secondary, not deserving of additional emphasis that they get in code.
- zoowar 15y agoIn C, 'if' is a keyword and a space always follows a keyword.
- zoowar 15y agoAnd one of the earlier C style guides I was exposed to. http://www.psgd.org/paul/docs/cstyle/cstyle.htm http://www.psgd.org/paul/docs/cstyle/cstyle.htm
- collint 15y agoif test
- misuse-permit 15y agoHm, I think you forgot a colon.
- mattdeboard 15y agoMost polite exception message ever.
- collint 15y agoruby or coffeescript true if true :)
- mindcrime 15y agoHell no... looking at code formatted like that makes my eyeballs bleed and leaves me huddled in a corner, with a ghastly, haunted look in my eye, as I mutter shit like "Ȋa! Ȋa! Cthȕlhu fhtagn. H'nglȕi mglw'nafh Cthȕlhu R'lyeh wgah'nagl fhtan!!" while some eldritch, unspeakable substance oozes down the wall...
- georgieporgie 15y agofunction(...) conditional (...)
- nxn 15y agoAnd do you also add the space before the paren when you declare a function? I ask because a conditional block is often closer syntactically to a function definition than it is to a function invocation, yet I hardly ever see anyone put a space before the paren when defining a function.
- michaelcampbell 15y agoI'm guessing here, but I suspect functional comes from maths notation where there typically is no space between the function name and the paren. y = f(x)
- georgieporgie 15y agoI don't see that there is any relationship between a function declaration and a conditional followed by a statement block. There is no reason to use different formatting for function declaration versus invocation.
- Sigi 15y agoYes, but only recently. It's a bad habit I'm trying to break. Like others have stated, it's a keyword.
- Webhacker 15y agoif ( spaces ) { more readable code; }
- epynonymous 15y agoi do it like this: if( test == true ) {} in python: if test:
- walrus 15y agoI'm surprised so many people like spaces. I was under the impression that most people didn't use a space.
- trbecker 15y agoYes, my team uses this style. Coding style is a matter of choice, and we should adapt to the style of our peers.
- gcr 15y agoHow much does this really matter? I suppose I usually use spaces, but to be honest, this isn't something I notice.
- tzs 15y agoI don't recall which I do, but I do know that I use less space in general nowadays than I did before syntax highlighting was basically everywhere. Anyone else change their style due to syntax highlighting?
- jamesbritt 15y agoYes. I leave out parens whenever I can because position, spacing and syntax highlighting makes code quite clear. So I really prefer, for example, if have_taste? ... or unless stuff =~ some_regex ...
- atheken 15y agoOnly n00bs use if statements. :-p
- FrancescoRizzi 15y agoI tend to worry about whitespaces in/around if statements only when the condition is complex. SO I tend to have "if(a==b)", but maybe "if( (a==b) && (c==d) )" and may have space between the 'if' and opening parenthesis when the condition is so complex to be multi-line (but then again, at that point a refactoring is needed/natural)
- lucisferre 15y agoUgh... This is the type of thing I love about languages like ruby and python, this kind of crap debate doesn't matter, there is really pretty much one way to write it. Braced languages annoy me because every developer seems to have their own "special" brand of stupid formatting. Some perfect examples of this thinking are right here: http://en.wikipedia.org/wiki/Indent_style#Horstmann_style http://en.wikipedia.org/wiki/Indent_style#Horstmann_style http://en.wikipedia.org/wiki/Indent_style#Whitesmiths_style http://en.wikipedia.org/wiki/Indent_style#Whitesmiths_style Think about basic writing people. Would you decide not to put a space before a parenthetic statement(obviously not)? Or like one comment suggested, you would never put a space after the opening or before the end ( unless you are just being ironic ). It doesn't matter how you think it looks, it matters that you are trying to be different for the sake of being different. Stop it.
- jhrobert 15y agoif( test ){