bundyxc Posted August 5, 2009 Share Posted August 5, 2009 I'm curious, how to you guys put your brackets? I'm torn between if (x) { //code } else { //code } and if (x) { //code } else { //code } The top is easier to read (in my opinion), but it looks like it drags out the code, and gets annoying. What do you guys use, and why? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted August 5, 2009 Share Posted August 5, 2009 Fewest lines taken up by structure and is readable when using proper indentation - if (x) { //code } else { //code } Quote Link to comment Share on other sites More sharing options...
Maq Posted August 5, 2009 Share Posted August 5, 2009 The manual uses the second style but I prefer the first style due to the Java formatting standards I have at work. I'm not really sure what the actual PHP standard is. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted August 5, 2009 Share Posted August 5, 2009 Fewest lines taken up by structure and is readable when using proper indentation - if (x) { //code } else { //code } this, but its just personal preference, not based on anything else really. Quote Link to comment Share on other sites More sharing options...
bundyxc Posted August 5, 2009 Author Share Posted August 5, 2009 Fewest lines taken up by structure and is readable when using proper indentation - if (x) { //code } else { //code } See, that just looks confusing to me, especially when you start overlapping loops and such. if (w) { //code } else { if (y) { //code } elseif (z) { //code } else { //code } } elseif (x) { //code } if (w) { //code } else { if (y) { //code } elseif (z) { //code } else { //code } } elseif (x) { //code } Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted August 5, 2009 Share Posted August 5, 2009 if (w) { //code } else { if (y) { //code } elseif (z) { //code } else { //code } } elseif (x) { //code } there much better. Looks fine to me. BTW this code is invalid =P. cant have an else before an elseif Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 5, 2009 Share Posted August 5, 2009 See, that just looks confusing to me, especially when you start overlapping loops and such. So why are you asking? Unless your code has to follow a particular format for business reasons, go with whatever logical format works for you. According to the zend framework standards, which some subscribe to, functions and classes have the brackets on thier own line, but loops and other constructs include them in-line with other code. Personally I prefer to use the more verbose method (your first example) as it makes readability easier - at least for me - especially when going back to code I've written months/years earlier. Quote Link to comment Share on other sites More sharing options...
bundyxc Posted August 5, 2009 Author Share Posted August 5, 2009 I'm solely asking because code that is 'easier' to read, looks very empty, and I see more experienced coders using different methods. I was just curious if the 'easier' code was syntactically incorrect, etc. Quote Link to comment Share on other sites More sharing options...
bilalaslam777 Posted August 5, 2009 Share Posted August 5, 2009 the first one increases readability and is easy to understand but memory and space consuming. Quote Link to comment Share on other sites More sharing options...
Maq Posted August 5, 2009 Share Posted August 5, 2009 I'm solely asking because code that is 'easier' to read, looks very empty, and I see more experienced coders using different methods. What you are asking is completely relative to the person coding. I was just curious if the 'easier' code was syntactically incorrect, etc. This has nothing to do with syntax. You can put all of your code on a single line if you want to and not receive syntax errors. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.