wyc
wyc

Reputation: 55283

Does the position of the brackets really matter in PHP (for instance, for the sake of readability)?

I started with CSS so I always used brackets this way:

#content {
    selector: value;
}

While using CodeIgniter I noticed that each file have the brackets like this:

function something here() 
{
    $this->$item('item');
    $data['rows'] = $this->data_model->getAll();

    $this->load->view('home', $data);
}

Is the second example better for readability or copying or pasting?

Upvotes: 4

Views: 497

Answers (8)

Matt Gibson
Matt Gibson

Reputation: 38238

That actually looks a bit wrong for CodeIgniter; according to the Style Guide, they use Allman style. See this article on Wikipedia for some background on indentation styles. [NB: indentation now fixed in question].

Really, though, it's just a matter of religion (note that Wikipedia's Indent Style article is currently linked to from its Wars of Religion article!)

The best thing to do is to be consistent.

So, for your own project, use whatever you feel is the most readable/usable. But if you wanted to contribute code to CodeIgniter, you'd use the Allman style, and if you're writing a project that just uses CodeIgniter, it might be easier to follow their style simply to stop your brain from having to swap between two styles while you're simultaneously digging through their code and writing yours.

Upvotes: 5

NikiC
NikiC

Reputation: 101936

No, it isn't simply a matter of preference. The style used by CodeIgniter simply is wrong in my eyes (yeah, give me many downvotes pls). There simply is one coding style that many projects use, the Zend Coding Standards Coding Style. In my opinion this is the only valid coding style, simply because most projects use it and you therefore don't need rethinking when you start contributing to them.

Actually, this is one of the reasons, why I do not use CoedIgniter. Another reason is, that they provided the introduction videos only in QuickTime. I don't expect they provide me a webm video, but at least Flash would really be nice.

PS: Please don't think I say this only because these are the Coding Standards I use. No, this isn't the case. I do not strictly adhere to these standards. But I think I should and I will try to. Simply because they are the standard.

Upvotes: 0

Treffynnon
Treffynnon

Reputation: 21553

The Zend Framework manual has a good chapter on their coding style, which might help you with the basics such as class naming etc.

Update: Looks like the original question has been changed to include CodeIgnitor. It is best you adhere to the style in the framework or the surrounding code if you are taking over someone elses project.

Upvotes: 1

Martin Bean
Martin Bean

Reputation: 39389

It's doesn't matter, as long as it's interpreted correctly.

However, if you work in a multi-developer environment, it may be worth agreeing upon a coding style standard across your work flow.

Upvotes: 0

Emanuil Rusev
Emanuil Rusev

Reputation: 35255

You can use both styles in PHP (and in CSS) and that will not affect the way your code executes.

Choosing one style over the other is a matter of personal preference. If you are using CodeIgniter, though, I'd recommend that you stick to it's coding style and be consistent.

Upvotes: 1

Gatman
Gatman

Reputation: 594

there is no matter where you put the brackets ...... you put them where for you will be more easy to read the code

Upvotes: 0

Rob Stevenson-Leggett
Rob Stevenson-Leggett

Reputation: 35679

I would have said your way is more readable.

It's a personal preference / team standards thing at the end of the day though.

Most people tend to go for your method or the following:

function something()
{
   //more code
}

Upvotes: 0

Centurion
Centurion

Reputation: 5291

No difference at all, you can use this style just if it's more understandable for you.

Upvotes: 2

Related Questions