Really newbie question on style
Moderator: General Moderators
Really newbie question on style
I have noticed that many people here and in the php book I am using to learn php with write there code using curly
braces as so.
if (something happens) {
then do some stuff;
}
My question is what is the logic of having the curly brace right after the if statment (or loop or whatever you are doing)
For example, in terms of reading code, this seems more readable to me.
if (something happens)
{
then do some stuff;
}
Obviously I am a new to programming so I know that there is a sound logical reasoning to why,readability wise, the first way of
writing the code is better then the way I have below. But I don't understand why. Some insight would be nice. After
all I would like to develop good habits since I am just in the process of developing my style.
braces as so.
if (something happens) {
then do some stuff;
}
My question is what is the logic of having the curly brace right after the if statment (or loop or whatever you are doing)
For example, in terms of reading code, this seems more readable to me.
if (something happens)
{
then do some stuff;
}
Obviously I am a new to programming so I know that there is a sound logical reasoning to why,readability wise, the first way of
writing the code is better then the way I have below. But I don't understand why. Some insight would be nice. After
all I would like to develop good habits since I am just in the process of developing my style.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
It's only a personal preference. I personally use the latter, as I, like you, feel it lends itself better to readability.
The historical derivation of the former stems from text-only displays having very finite numbers of lines available on screen. If one wanted to see more lines, it was a simple way to increase the number of actual lines that are shown on screen without making it much more difficult to read.
The latter, I believe, stems from a later time when displays supported far more lines and we had higher level functionality (like functions, classes and such) to deal with.
The historical derivation of the former stems from text-only displays having very finite numbers of lines available on screen. If one wanted to see more lines, it was a simple way to increase the number of actual lines that are shown on screen without making it much more difficult to read.
The latter, I believe, stems from a later time when displays supported far more lines and we had higher level functionality (like functions, classes and such) to deal with.
-
Superman859
- Forum Commoner
- Posts: 47
- Joined: Sun Oct 29, 2006 10:22 am
*shrugs* It does not affect your code, but I use the second style you mentioned, except that I put a space after each curly bracket for extra clarification:
It might take up more space, but it really sets your function declarations apart from the rest of the script if you have more than twenty or thirty lines of code in your function.
-wyrmmage
Code: Select all
function whateverFunction()
{
echo('whatever');
}-wyrmmage
I do that too... although I don't remember the last time I wrote a standard function... I write methods way more often...
That, to me, looks perfect.
Code: Select all
public function doSomething() {
$this->foo = 'bar';
return true;
}Follow up question
Another question regarding style. One of the things that I have noticed very difficult for me is keeping my programs well organized. It seems that when they get big they are really hard to edit and follow. One of the things I have done is begun to write functions and that has definitely helped. But even some of the functions get a bit big and hard to follow.
I have been reading a lot of code written here and that has definitely helped in terms of organizing my own code but if people have some tips that helps keep them organized that would really be helpful. As an example of my organizational problems, yesterday I wrote a function that had 13 curly braces (both {}). It took me forever to find the missing curly brace. Now I do use white space and indent but I still notice that when I nest control loops withing other control loops the program quickly becomes unmanageable.
Any advice?
I have been reading a lot of code written here and that has definitely helped in terms of organizing my own code but if people have some tips that helps keep them organized that would really be helpful. As an example of my organizational problems, yesterday I wrote a function that had 13 curly braces (both {}). It took me forever to find the missing curly brace. Now I do use white space and indent but I still notice that when I nest control loops withing other control loops the program quickly becomes unmanageable.
Any advice?
yes; you could use an editor that supports matching brace high-lighting(there is a list of them on this website somewhere, although I can't provide the link) or split your program into seperate files. Even if you are using an editor, if you're code is more than three or four hundred lines of code, I would recommend splitting it into seperate files; when I first started coding in php, my first actual project got up to 30,000 lines of code, then I had to split it because notepad ran out of memory to display it at that point
Anyway, just split it into different files (perhaps one for each function, if they are fairly long) and use an include() statement to get them when you need them.
-wyrmmage
Anyway, just split it into different files (perhaps one for each function, if they are fairly long) and use an include() statement to get them when you need them.
-wyrmmage
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Here is your 'What's your favourite PHP editor' mega thread. I would look at ways that you might possibly be able to optimize your code. After that, look at using an editor that can help with matching braces and the what not.
PS | I am of the 'new line for braces' variety:
With no new line after the opening brace.
PS | I am of the 'new line for braces' variety:
Code: Select all
<?php
public function dosomething()
{
if ($this->there_is_something_to_do)
{
$this->better_do_it();
}
else
{
$this->better_do_something_else();
}
}
?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
class fooBar
{
public function __construct()
{
while($jcart == 'cool') {
}
}
}The problem is of course, nothing at this time is comfortable (I am sure it will be at some point). I really like the idea of creating separate files for each function. I had thought of that before but thought it might be come a bit unwieldy keeping track of all those files (currently my program is 5 seperate files and I always have trouble remeber which file each is called - though I have started in my beginning comment section making a list of all files used by a particular program.
I do use a syntax highlighter editor (xcode in the developers tools by mac) and have dabbled a bit it vim. I just cannot imagine trying to maintain a program that is over a hundred lines of code let alone 30k.
I do use a syntax highlighter editor (xcode in the developers tools by mac) and have dabbled a bit it vim. I just cannot imagine trying to maintain a program that is over a hundred lines of code let alone 30k.
-
Superman859
- Forum Commoner
- Posts: 47
- Joined: Sun Oct 29, 2006 10:22 am
A hundred lines is nothing
I just had a short Java assignment due that took 319 lines in one file.
Yes, you will find something that is comfortable. I've been programming Java for a while and just recently started PHP. Java is almost always written on separate lines, which is why I am used to that. I wasn't used to it at first. Heck, I wasn't used to putting any square brackets
For maintaining longer sections of code, you MUST be organized - and use white space. It's free, why not take it? With longer sections of Java, I create many methods (very similar to functions) and call them, as opposed to throwing it all together. Using methods keeps a very nice and organized structure. I imagine PHP works the same way, although I recently read user-made functions can eat up memory in PHP...I'm not certain about this though - I'd love to create a function for nearly everything to simply keep the program organized.
And again, as the code gets bigger and bigger, introduce classes. I'm unsure of classes in PHP as I haven't studied them yet, but for Java, I see a class as a HUGE method (or a huge function). If it takes up too many lines and completes a nice little task, I stick it in it's own class.
Yes, you will find something that is comfortable. I've been programming Java for a while and just recently started PHP. Java is almost always written on separate lines, which is why I am used to that. I wasn't used to it at first. Heck, I wasn't used to putting any square brackets
For maintaining longer sections of code, you MUST be organized - and use white space. It's free, why not take it? With longer sections of Java, I create many methods (very similar to functions) and call them, as opposed to throwing it all together. Using methods keeps a very nice and organized structure. I imagine PHP works the same way, although I recently read user-made functions can eat up memory in PHP...I'm not certain about this though - I'd love to create a function for nearly everything to simply keep the program organized.
And again, as the code gets bigger and bigger, introduce classes. I'm unsure of classes in PHP as I haven't studied them yet, but for Java, I see a class as a HUGE method (or a huge function). If it takes up too many lines and completes a nice little task, I stick it in it's own class.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
http://php.net/php5Superman859 wrote:For maintaining longer sections of code, you MUST be organized - and use white space. It's free, why not take it? With longer sections of Java, I create many methods (very similar to functions) and call them, as opposed to throwing it all together. Using methods keeps a very nice and organized structure. I imagine PHP works the same way, although I recently read user-made functions can eat up memory in PHP...I'm not certain about this though - I'd love to create a function for nearly everything to simply keep the program organized.
And again, as the code gets bigger and bigger, introduce classes. I'm unsure of classes in PHP as I haven't studied them yet, but for Java, I see a class as a HUGE method (or a huge function). If it takes up too many lines and completes a nice little task, I stick it in it's own class.