Jump to content

Change
HostCell.net Shared Hosting plan

Important Announcement!

Welcome to Hostcell 2.0! If you are an existing member and have trouble logging in please reset your password! If you are still having trouble, contact us on admin@hostcell.net and we can reset the password for you!


One True Brace

brace kamp;amp;r php

13 replies to this topic

#1
OFFLINE   Saint

    Exceedingly Senior Member

  • Members
  • 1,954 posts
K&R style — as in Kernighan and Ritchie, designers of C and authors of the definitive references on the language — is also known as the “One True Brace” style:

<?php
$level = 10;
$triangle = array();
$triangle[0] = array(1);
for($i = 1; $i < $level; $i++) {
	$triangle[$i] = array();
	$prev = 0;
	foreach($triangle[$i-1] as $key => $value) {
        	$triangle[$i][$key] = $prev + $value;
    		$prev = $value;
	}
	$triangle[$i][$i] = 1;
	foreach($triangle[$i] as $key => $value) {
        	print "$value ";
	}
	print '</br>';
}
?>


(Pascal's Triangle: solution in PHP by loremar, and re-styled in K&R.)
The Tree of Internet Liberty must be illuminated from time to time with the photons of dark websites. Thomas Jefferson (nearly)
Live fast. Die young. Leave a beautiful body of code. Nick Romano in Knock on any Door 1949 (nearly)
No taxation without recrimination. Samuel Adams (nearly)

HostCell.net Shared Hosting plan

#2
OFFLINE   the_reaper

    Advanced Member

  • Members
  • 481 posts
Ahhhh, K&R...I've been using that style since I first started learning Javascript (before I started university).
I wouldn't necessarily call it the "one true brace", since other styles do have their merits (eg. loremar's version you can match up opening and closing braces a bit more easily). But I tend to like K&R better, since it results in a smaller line count and looks cleaner.
Half evil, half nerd, I'm the best of both worlds.
Don't fear The Reaper.
Need help with the JS SDK? Try here - App Page
Like reading? Try my blog on for size
Hostcell Review
Posted Image

#3
OFFLINE   loremar

    Advanced Member

  • Members
  • 373 posts
K&R??? What's that? It's new for me.

Yeah, I think it looks more cleaner to indent properly, I do that all the time but I can't seem to indent in the code while posting here.
I also prefer if braces are aligned, like reaper said, it's easier to pair braces. Also good spacing in pieces of code looks more organized coupled with helpful comments. I don't know anything about line counts having any effect on programming.

#4
OFFLINE   the_reaper

    Advanced Member

  • Members
  • 481 posts

View Postloremar, on 17 January 2012 - 10:12 AM, said:

... Also good spacing in pieces of code looks more organized coupled with helpful comments.

Agree with you there. Which reminds me...I should probably comment my code more... :unsure:

View Postloremar, on 17 January 2012 - 10:12 AM, said:

I don't know anything about line counts having any effect on programming.

It doesn't really effect the program/script, in the end...but large programs/scripts are usually said to be thousands to millions of lines, which can be different based on the style of brace used. Although...I believe they're meant to discount empty lines, lines with comments, and lines with only braces.
Half evil, half nerd, I'm the best of both worlds.
Don't fear The Reaper.
Need help with the JS SDK? Try here - App Page
Like reading? Try my blog on for size
Hostcell Review
Posted Image

#5
OFFLINE   Saint

    Exceedingly Senior Member

  • Members
  • 1,954 posts

View Postthe_reaper, on 17 January 2012 - 11:20 AM, said:

...but large programs/scripts are usually said to be thousands to millions of lines, which can be different based on the style of brace used. Although...I believe they're meant to discount empty lines, lines with comments, and lines with only braces.

Yes. Usually a "7,500 line" program means that the compiler reported processing 7,500 lines of actual working code. Otherwise a programmer could cheat on his productivity, just by adding lots more white space, or waffling on his comments!

On the other hand, a physical line such as expression1; expression2; expression3; would be reported by the compiler as 3 lines of code.

In college, a teacher will probably see a line of code such as "expression1; expression2; expression3;" and squeal in horror, "But there's no white space! It is unreadable." But on rare occasions, I have done exactly that, and to make the code more comprehensible at a later date too. If a section of logic can all fit onto the computer screen at one time, it is much easier to read and comprehend. (Or at least, that is what experience has taught me.)
The Tree of Internet Liberty must be illuminated from time to time with the photons of dark websites. Thomas Jefferson (nearly)
Live fast. Die young. Leave a beautiful body of code. Nick Romano in Knock on any Door 1949 (nearly)
No taxation without recrimination. Samuel Adams (nearly)

#6
OFFLINE   the_reaper

    Advanced Member

  • Members
  • 481 posts

View Postsours, on 17 January 2012 - 03:40 PM, said:

In college, a teacher will probably see a line of code such as "expression1; expression2; expression3;" and squeal in horror, "But there's no white space! It is unreadable." But on rare occasions, I have done exactly that, and to make the code more comprehensible at a later date too. If a section of logic can all fit onto the computer screen at one time, it is much easier to read and comprehend. (Or at least, that is what experience has taught me.)

I've seen quite a few programs and scripts that put many expressions onto one line...I've done it myself :)
I don't remember if lecturers or tutors were that pedantic about it...I think I remember one even did that in a lecture, just to show us it worked.
Half evil, half nerd, I'm the best of both worlds.
Don't fear The Reaper.
Need help with the JS SDK? Try here - App Page
Like reading? Try my blog on for size
Hostcell Review
Posted Image

#7
OFFLINE   HCN-willyhsv

    Advanced Member

  • Support Team
  • 1,858 posts

View Postloremar, on 17 January 2012 - 10:12 AM, said:

K&R??? What's that? It's new for me.

Loremar, you're killing me dude. Anyone who codes C/C++ well knows who K&R are.

But then again, I guess I am a little sensitive, since your non-knowledge of K&R only reflects that you are much younger than most folks who used K&R small but super-usable book as our only reference for years. In other words.... You making me feel old, dude! LOL

Of course, you probably dont know what DOS 3.3 is either.. ;)

http://www.amazon.co...28715420&sr=8-1 <= for those who are interested in what we are talking about.

As for braces, I am one who puts starts the braces on the next line at same level indent. I just find it is soo much easier finding your blocks when they are above each other. But then again, it all comes down to how you learned in the first place. Kind of like how some people put spaces around the equal sign or math symbols and others but everything up with no space.

#8
OFFLINE   loremar

    Advanced Member

  • Members
  • 373 posts
Oh! So it's a book. I don't think I've read that. I learned C in college while it was demonstrated in class and took some few notes. But haven't read a single book about C. I don't think it was taught in class.

However, I'm reading Programming in Lua and it's using K&R style for examples in using C API for Lua. Getting used to it.

#9
OFFLINE   HCN-Northern

    Advanced Member

  • Support Team
  • 2,107 posts

View PostHCN-willyhsv, on 08 February 2012 - 03:40 PM, said:

http://www.amazon.co...28715420&sr=8-1 <= for those who are interested in what we are talking about.

bloody hell i think i still have that book somewhere in the loft lol
Posted Image

#10
OFFLINE   HCN-willyhsv

    Advanced Member

  • Support Team
  • 1,858 posts
I've got one at work and one at home. The reference we used before google was a verb!

#11
OFFLINE   Saint

    Exceedingly Senior Member

  • Members
  • 1,954 posts

View PostHCN-willyhsv, on 08 February 2012 - 03:40 PM, said:

... folks who used K&R small but super-usable book as our only reference for years...

Yes, youngsters, at one time C was the language of the future. And there were no free C compilers. And there was no internet to pirate software on.
The Tree of Internet Liberty must be illuminated from time to time with the photons of dark websites. Thomas Jefferson (nearly)
Live fast. Die young. Leave a beautiful body of code. Nick Romano in Knock on any Door 1949 (nearly)
No taxation without recrimination. Samuel Adams (nearly)

#12
OFFLINE   HCN-Northern

    Advanced Member

  • Support Team
  • 2,107 posts

View Postsours, on 08 February 2012 - 09:27 PM, said:

And there was no internet to pirate software on.

there was BBS and there was pirate stuff.... just had to know where to look ;) ... ah the Amiga dial up days :)
Posted Image

#13
OFFLINE   Saint

    Exceedingly Senior Member

  • Members
  • 1,954 posts

View PostHCN-willyhsv, on 08 February 2012 - 03:40 PM, said:

... http://www.amazon.co...28715420&sr=8-1 <= for those who are interested in what we are talking about...

View PostHCN-Northern, on 08 February 2012 - 07:09 PM, said:

bloody hell i think i still have that book somewhere in the loft lol

My copy of K&R is still on the bookshelf. (Haven't opened it for years, though.) It's right next to another ancient book entitled, "The DIF File". Anybody remember Data-Interchange-Format? (It was a standard of the future that never happened.)
The Tree of Internet Liberty must be illuminated from time to time with the photons of dark websites. Thomas Jefferson (nearly)
Live fast. Die young. Leave a beautiful body of code. Nick Romano in Knock on any Door 1949 (nearly)
No taxation without recrimination. Samuel Adams (nearly)

#14
OFFLINE   the_reaper

    Advanced Member

  • Members
  • 481 posts
Ahhh, standards:
Posted Image
(From http://xkcd.com/927/)
Half evil, half nerd, I'm the best of both worlds.
Don't fear The Reaper.
Need help with the JS SDK? Try here - App Page
Like reading? Try my blog on for size
Hostcell Review
Posted Image