Re: [PHP] How to write code: how wrong am I?
At 11:04 AM -0700 2/22/11, Alexis wrote: On 22/02/11 09:40, Dotan Cohen wrote: On Tue, Feb 22, 2011 at 16:15, Robert Cummings wrote: If you switch to vertically aligned braces you will have an easier time matching up open and close braces... if (something) { // Code here } It's trivial to match using this style since you only need to scan one axis. That bracing style drives me nuts! But I have no problem scanning a single axis for the close brace and any statement, be it an if, while, switch, or something else. Well the way you do it, drives me nuts :) Morale of the story, do whatever *you* are most comfortable with..personally I indent as well, has it's downsides I admit, but it's what I've done for the last 20 years and I'm comfortable with it. Alexis I agree with Alexis. Furthermore, I place braces on their own lines AND I do it in all languages I program in, such as CSS, PHP, JavaScript, and others. Cheers, tedd -- --- http://sperling.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
At 9:13 AM -0500 2/22/11, Robert Cummings wrote: On 11-02-22 08:58 AM, Dotan Cohen wrote: On Tue, Feb 22, 2011 at 14:11, Jay Blanchard wrote: It is an interesting concept Dotan, what is the goal? Who is the page targeted to? The goal is to have every open and close bracket matched, and not have to worry about what is still open. This is why I use the bracing style I use in PHP. A cursory glance of the code will show you where you've forgotten a brace. Syntax highlighting is also nice. HTML can trip you up with missing or superfluous tags, but a quick run through the W3C validator will catch those errors. Cheers, Rob. Rob is exactly right on both counts. Place braces on their own lines and use the W3C Validator to check both html and css. Cheers, tedd -- --- http://sperling.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Tue, Feb 22, 2011 at 19:03, Paul M Foster wrote: > Not a bad idea for HTML, not so great for PHP. Some of what you're > trying to solve/avoid can be handled by a decent editor, one that does > syntax highlighting and/or does brace and parenthesis checking. It > doesn't require an expensive IDE. I use Vim, and it works fine for this. > And honestly, if you're serious about programming, you really shouldn't > be using something like Notepad on Windows. Seriously. We had a coder > working for a company where I was who coded in Word of all things. We > just looked at him like he was crazy. > When I'm writing code in a language I know, I use either Vim, Kate, or Eclipse. But when I'm learning a language I use an environment with no syntax highlighting or code completion. It is in these cases that the close-then-fill coding style helps. Also, I've many times had to SSH into a server and change HTML or PHP code in the half-VIM that ships with CentOS and Debian, which does not support syntax highlighting. So the style has real-world advantages. > Annotating end-braces is also very helpful for long conditionals. Vim > can track start and stop braces, but when they span several screen > pages, it can be a problem. > Yup! > Likewise commenting code as you go is an excellent idea. 2:1 comments > are a bit much; Linus Torvalds would kick your butt. But I often do this > if I'm not exactly sure how I'm going to code something, but I know the > rough logic of it. Lay out the conditionals, comment on what should > happen within each code block, and then go back and fill in the blocks > with actual code. > 2:1 is excessive. I only comment where the code is not explicitly clear, or at the beginning of each code block of a long if / elseif / elseif chain. And I comment the close parenthesis to know what it closed. > It's also worth noting that sometimes code is hard to follow. And even > if you're going to be the only one updating it, you may well forget the > logic six months from now. Commenting allows you to recapture that logic > more easily. > Yup! > And then there's the PHP interpreter. If you make a syntax error, like > failing to add an closing brace, the interpreter will tell you so. If > you spend *enough* time coding, you can usually track down where your > mistakes are relatively easily. If you've ever coded in C, the same is > true for the C compiler. Its behavior is very very similar to that of > the PHP interpreter. It's something you get used to over time as you > code more and more. Also, what could be called "incremental coding" or > "stepwise development" helps: Code a little. Run/compile. Code a little, > run/compile. That way, errors are easier to catch than after you've > written 5000 lines in a file. > Sometimes I have to touch a live site, in fact _most_ of the work I do is for small accounts that don't have a test environment. I can often write a test page with a security-by-obscurity filename, but relying on the interpreter is a habit that I cannot afford. -- Dotan Cohen http://gibberish.co.il http://what-is-what.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Feb 22, 2011, at 5:29 AM, Dotan Cohen wrote: I wrote a short page on how to actually type of code that one writes, it can be found here: http://dotancohen.com/howto/write_code.html The point that I stress on the page is that first you close an element, then you fill it's contents. The page has examples in HTML and PHP. I would like to know what the experienced programmers here think about this. Am I going about it wrong? Thanks! I don't think this is a bad idea at all, in general. This is pretty much what TextMate does when you use snippet insertion. For editors that don't have this particular feature, I'll still generally put in the structure of what i'm doing before filling in all the details. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On 2/22/11 12:04 PM, Alexis wrote: On 22/02/11 09:40, Dotan Cohen wrote: On Tue, Feb 22, 2011 at 16:15, Robert Cummings wrote: If you switch to vertically aligned braces you will have an easier time matching up open and close braces... if (something) { // Code here } It's trivial to match using this style since you only need to scan one axis. That bracing style drives me nuts! But I have no problem scanning a single axis for the close brace and any statement, be it an if, while, switch, or something else. Well the way you do it, drives me nuts :) Morale of the story, do whatever *you* are most comfortable with..personally I indent as well, has it's downsides I admit, but it's what I've done for the last 20 years and I'm comfortable with it. Alexis The most important thing is that you are consistent within a project and within a team. Your entire team should be consistently using one format standard, whatever it is. Failure to do so results in unreadable code, because your brain has to shift gears depending on who wrote a given block of code. That's bad news. I'd rather see code written in brace-on-own-line style (which I despise) than code that freely switches between that and inline-brace style (my preference) every other for-loop. That's simply unreadable. --Larry Garfield -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On 22/02/11 09:40, Dotan Cohen wrote: On Tue, Feb 22, 2011 at 16:15, Robert Cummings wrote: If you switch to vertically aligned braces you will have an easier time matching up open and close braces... if (something) { // Code here } It's trivial to match using this style since you only need to scan one axis. That bracing style drives me nuts! But I have no problem scanning a single axis for the close brace and any statement, be it an if, while, switch, or something else. Well the way you do it, drives me nuts :) Morale of the story, do whatever *you* are most comfortable with..personally I indent as well, has it's downsides I admit, but it's what I've done for the last 20 years and I'm comfortable with it. Alexis -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Tue, Feb 22, 2011 at 01:29:30PM +0200, Dotan Cohen wrote: > I wrote a short page on how to actually type of code that one writes, > it can be found here: > http://dotancohen.com/howto/write_code.html > > The point that I stress on the page is that first you close an > element, then you fill it's contents. The page has examples in HTML > and PHP. I would like to know what the experienced programmers here > think about this. Am I going about it wrong? Not a bad idea for HTML, not so great for PHP. Some of what you're trying to solve/avoid can be handled by a decent editor, one that does syntax highlighting and/or does brace and parenthesis checking. It doesn't require an expensive IDE. I use Vim, and it works fine for this. And honestly, if you're serious about programming, you really shouldn't be using something like Notepad on Windows. Seriously. We had a coder working for a company where I was who coded in Word of all things. We just looked at him like he was crazy. Annotating end-braces is also very helpful for long conditionals. Vim can track start and stop braces, but when they span several screen pages, it can be a problem. Likewise commenting code as you go is an excellent idea. 2:1 comments are a bit much; Linus Torvalds would kick your butt. But I often do this if I'm not exactly sure how I'm going to code something, but I know the rough logic of it. Lay out the conditionals, comment on what should happen within each code block, and then go back and fill in the blocks with actual code. It's also worth noting that sometimes code is hard to follow. And even if you're going to be the only one updating it, you may well forget the logic six months from now. Commenting allows you to recapture that logic more easily. And then there's the PHP interpreter. If you make a syntax error, like failing to add an closing brace, the interpreter will tell you so. If you spend *enough* time coding, you can usually track down where your mistakes are relatively easily. If you've ever coded in C, the same is true for the C compiler. Its behavior is very very similar to that of the PHP interpreter. It's something you get used to over time as you code more and more. Also, what could be called "incremental coding" or "stepwise development" helps: Code a little. Run/compile. Code a little, run/compile. That way, errors are easier to catch than after you've written 5000 lines in a file. Paul -- Paul M. Foster http://noferblatz.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Tue, Feb 22, 2011 at 16:15, Robert Cummings wrote: > If you switch to vertically aligned braces you will have an easier time > matching up open and close braces... > > if (something) > { > // Code here > } > > It's trivial to match using this style since you only need to scan one axis. > That bracing style drives me nuts! But I have no problem scanning a single axis for the close brace and any statement, be it an if, while, switch, or something else. -- Dotan Cohen http://gibberish.co.il http://what-is-what.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On 11-02-22 09:06 AM, Dotan Cohen wrote: On Tue, Feb 22, 2011 at 14:58, Robert Cummings wrote: I'm more likely to do this stuff for HTML than PHP. However, I do sometimes lay out the structure of some conditionals/functions before fleshing them out so that I can do some early testing... but I always fill the conditional as I'm doing this. In case that's not clear... the code flows from first character to last character as I type all of the conditionals (no jumping around). Later I fill in the associated activity for the conditional. Thanks for the insight. I could see where that is a good compromise. Like this I suppose: if (something) if (something) { } if (something) { // Code here } If you switch to vertically aligned braces you will have an easier time matching up open and close braces... if (something) { // Code here } It's trivial to match using this style since you only need to scan one axis. Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On 11-02-22 08:58 AM, Dotan Cohen wrote: On Tue, Feb 22, 2011 at 14:11, Jay Blanchard wrote: It is an interesting concept Dotan, what is the goal? Who is the page targeted to? The goal is to have every open and close bracket matched, and not have to worry about what is still open. This is why I use the bracing style I use in PHP. A cursory glance of the code will show you where you've forgotten a brace. Syntax highlighting is also nice. HTML can trip you up with missing or superfluous tags, but a quick run through the W3C validator will catch those errors. Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Tue, Feb 22, 2011 at 15:00, Andy McKenzie wrote: > I'll be the first to tell you that I'm not a great programmer, so my > take may not be worth a lot. However, this is pretty close to how I > write. Not exactly, but close. I also always label open and closing > brackets on everything, unless the brackets are less than three lines > apart. For instance, I might find myself writing: > > if($foo == 'bar') > { # Begin foo=bar test > # If foo=bar, do a lot of things. > > } # End foo=bar test > > Then I go back and fill in the conditional stuff. I also do something similar, I debated writing about that as well: class someClass{ function someFunction() { if (true) { // Some Code } } // end someFunction } // end someClass I don't strictly subscribe to a three-line limit, but whatever feels reasonable at the time. Often that depends on the complexity of surrounding code. -- Dotan Cohen http://gibberish.co.il http://what-is-what.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Tue, Feb 22, 2011 at 14:58, Robert Cummings wrote: > I'm more likely to do this stuff for HTML than PHP. However, I do sometimes > lay out the structure of some conditionals/functions before fleshing them > out so that I can do some early testing... but I always fill the conditional > as I'm doing this. In case that's not clear... the code flows from first > character to last character as I type all of the conditionals (no jumping > around). Later I fill in the associated activity for the conditional. > Thanks for the insight. I could see where that is a good compromise. Like this I suppose: if (something) if (something) { } if (something) { // Code here } -- Dotan Cohen http://gibberish.co.il http://what-is-what.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Tue, Feb 22, 2011 at 14:15, Marc Guay wrote: > It's an interesting idea (a different take on coding best practices) > but I find the PHP example to be laborious and time consuming with > little benefit. If I'm typing an IF statement, I hope to god I know > what the condition is before I start typing it. Creating the if/else > structure first and then filling in the conditions after is a lot of > cursor-shuffling that could be lived without. > > If the if statement is simple, then I do agree with you. But once there are nestled functions or conditionals, I like to have the layout already ready. Consider: if ( someFunction( someCheck( srrtolower( $_POST["variable"] ) ) ) ){ // Some Code } Now, after the $_POST variable, how many close parentheses does the code need? This does trip up beginners, especially those who rightfully learn outside an IDE. Similar situations occur for something like this (unindented to not overflow email line character limit): function someFunction(){ if($something==true){ while($someCondition){ switch($someVariable){ // Some Code } } } } Writing that is fine, but if the user goes in to add another while or if, he will get lost in the sea of parenthesis. Therefore, I always close elements before filling them in. It keeps the mind's state machine saner! -- Dotan Cohen http://gibberish.co.il http://what-is-what.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Tue, Feb 22, 2011 at 14:11, Jay Blanchard wrote: > It is an interesting concept Dotan, what is the goal? Who is the page > targeted to? > The goal is to have every open and close bracket matched, and not have to worry about what is still open. The page was specifically written for a friend who is learning HTML, but I plan on presenting it to my C# and Java classmates who I think would benefit from it as well. I choose to display the concepts with PHP instead of C# or Java as I feel that there would be more PHP learners online to benefit from the concept than C# or Java beginners. -- Dotan Cohen http://gibberish.co.il http://what-is-what.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Tue, Feb 22, 2011 at 6:29 AM, Dotan Cohen wrote: > I wrote a short page on how to actually type of code that one writes, > it can be found here: > http://dotancohen.com/howto/write_code.html > > The point that I stress on the page is that first you close an > element, then you fill it's contents. The page has examples in HTML > and PHP. I would like to know what the experienced programmers here > think about this. Am I going about it wrong? I'll be the first to tell you that I'm not a great programmer, so my take may not be worth a lot. However, this is pretty close to how I write. Not exactly, but close. I also always label open and closing brackets on everything, unless the brackets are less than three lines apart. For instance, I might find myself writing: if($foo == 'bar') { # Begin foo=bar test # If foo=bar, do a lot of things. } # End foo=bar test Then I go back and fill in the conditional stuff. I've found two sets of benefits to doing things this way. 1) I don't forget to close the brackets later, and I know they're indented properly. 2) The comments mean that if I get to the beginning of a section and want to skip to the end, I can just search for whatever comes after "Begin", and know that the next instance will be the end of that section. I also tend to document my code in comments before I start writing actual code. I frequently find that that helps keep me on track, rather than getting distracted by random new features that occur to me after I start. It also means that when the time comes to document what I did, most of it is already there... that's saved me a few times. (I tend to wind up with about a 2:1 ratio of comments to code, which at least means I can figure out later what I was thinking when I wrote that horrible mess.) So I can't say whether it's worthwhile practice for a good programmer who writes on a regular basis: for someone like me, who throws together an occasional script to save time later, or a personal web-site that needs a database backend, it can save a lot of aggravation. -Alex -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On 11-02-22 06:29 AM, Dotan Cohen wrote: I wrote a short page on how to actually type of code that one writes, it can be found here: http://dotancohen.com/howto/write_code.html The point that I stress on the page is that first you close an element, then you fill it's contents. The page has examples in HTML and PHP. I would like to know what the experienced programmers here think about this. Am I going about it wrong? I'm more likely to do this stuff for HTML than PHP. However, I do sometimes lay out the structure of some conditionals/functions before fleshing them out so that I can do some early testing... but I always fill the conditional as I'm doing this. In case that's not clear... the code flows from first character to last character as I type all of the conditionals (no jumping around). Later I fill in the associated activity for the conditional. Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
It's an interesting idea (a different take on coding best practices) but I find the PHP example to be laborious and time consuming with little benefit. If I'm typing an IF statement, I hope to god I know what the condition is before I start typing it. Creating the if/else structure first and then filling in the conditions after is a lot of cursor-shuffling that could be lived without. 2 cents Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] How to write code: how wrong am I?
[snip] The point that I stress on the page is that first you close an element, then you fill it's contents. The page has examples in HTML and PHP. I would like to know what the experienced programmers here think about this. Am I going about it wrong? [/snip] It is an interesting concept Dotan, what is the goal? Who is the page targeted to? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php