Yii Framework Forum: Coding Standard - Yii Framework Forum

Jump to content

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »
  • You cannot start a new topic
  • This topic is locked

Coding Standard Rate Topic: ***** 5 Votes

#1 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 608
  • Joined: 08-May 09

Posted 07 August 2009 - 07:49 PM

Is there a coding standard yii is following?
Its definitly not Zend's

I would like to see yii is following the coding standard of zend framework
0

#2 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 08 August 2009 - 01:51 AM

There was an approach: http://www.yiiframew...oc/cookbook/40/
0

#3 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 608
  • Joined: 08-May 09

Posted 08 August 2009 - 04:35 AM

View Postpestaa, on 08 August 2009 - 01:51 AM, said:



Somebody knows PHP_CodeSniffer?
http://pear.php.net/...iffer.usage.php
0

#4 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 08 August 2009 - 04:52 AM

As the cookbook page is only a recommended set of standards, using codesniffer is a bit overwhelming, isn't it?

What is the point of enforcing coding standards in this environment?
0

#5 User is offline   François Gannaz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 24-November 09

Posted 27 November 2009 - 09:18 AM

IMHO, the lack of a properly defined coding standard is a big flaw of Yii, especially if Yii intends to be a pro framework.

Yii's style is not always coherent, and quite different from the usual. For instance, all the standards I know of write
if ($a == 1)
instead of
if($a==1)


The main PHP coding standards are PEAR and Zend (which derives from PEAR and is quite similar). You might add Drupal, Symfony or ezComponents', but I don't think they're as widely used. And all of them are very different from Yii's. So I believe experienced developers have a hard time trying to adopt Yii's conventions. I had. And I finally chose to write my own code with Zend style in Yii.

The coding style is really important for adoption. I will not recommend Yii to my software company because I know the others developers will be much disturbed by this. And it even more important if you use the scaffolding tools!

I know beautifying Yii's code would be a huge task, even if some automatic treatment could help, but I believe it's worth the pain.
0

#6 User is offline   Sander 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 169
  • Joined: 03-November 09
  • Location:Amsterdam, Netherlands

Posted 27 November 2009 - 09:38 AM

Seriously? For a couple of spaces and brackets? I couldn't care less. Especially if it means the core developers can do what works best for them and don't have to worry about silly details like these.
0

#7 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 27 November 2009 - 09:41 AM

Absolutely agree to Sander. Moreover Yii's source code is one of the best formatted i've seen in PHP at all. So i don't understand the complaint at all. In cases like this there's a saying in german like "Some people, if they don't have problems, they try to create some...". ;)
0

#8 User is offline   François Gannaz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 24-November 09

Posted 28 November 2009 - 05:29 AM

I was talking about professional development. Have you ever worked on a medium-sized or large project, with collaborative development? If so, you must have heard about coding standard, or at least coding style. If each developer writes code in a format different from his neighbor, you'll end up with a big mess.

As mbi pointed in an earlier message, this is so important that there are tools to checks the quality of a PHP code. CodeSniffer is a PEAR project that helps enforcing the coding style. It's now part of most continuous integration tools for PHP.

And sorry, Mike, but Yii's source code isn't that well formatted. Please read Zend or Dupal's code standards to understand why some of its practices are considered harmful. For instance too long lines or omitting braces. I can even point you some parts of the code that are plain ugly, like the installer or CHttpRequest::getPreferredLanguage().
3

#9 User is offline   Sander 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 169
  • Joined: 03-November 09
  • Location:Amsterdam, Netherlands

Posted 28 November 2009 - 11:11 AM

I know about coding standards in collaborative environments, but I'm not really convinced of their use. Many programmers go very anal over this, but my experience usually tells me this:

If you need to work your way through some code, it can be either 1) your own code, in which case it'll be formatted in a convenient way and by recognizing your own style or maybe remembering writing it previously you might scan it a little bit faster, or 2) someone else's code, in which case you're going to have to consciously read the relevant parts of it line by line anyway, so code formatting doesn't really make a difference.

Far more important for me are sane data structures and coding practices. Small methods, thoroughly documented code, descriptive variable names, stuff like that.

Also, if you build a project on top of Yii, how many times are you going to have to modify the original source? I think Yii is set up pretty nicely, you can extend pretty much any class you like to add functionality, and you can do it completely separate from the original Yii framework code, so you can still keep your coding standards for your project if you really really want to.

One thing I do agree with you about is omitting the curly braces for a one-line if/for clause. This can lead to some logical problems when that clause is extended without adding those braces, so they should preferably be there anyway, even if they enclose only one line of code.
0

#10 User is offline   Backslider 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 363
  • Joined: 23-July 09

Posted 28 November 2009 - 11:37 PM

Well, it's just nice to call people "anal" if they happen to have a different viewpoint from your own <_<

Missing spaces where there should be spaces makes reading the code difficult, since it upsets color coding in code editors.

Skipping brackets is a very poor habit and makes reading the code a pain.

Far more important however is steering clear of "sexy code" for sexy code's sake. Yii's jQuery Ajax for example falls down all over the place due to this and is totally unreliable. Reading these forums, most experienced developers here write their own Ajax... and for good reason. So, we have a whole bunch of code already in Yii that is essentially redundant.
We were all once expert at....... nothing.

yii-language-behavior

My Blog
0

#11 User is offline   Sander 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 169
  • Joined: 03-November 09
  • Location:Amsterdam, Netherlands

Posted 29 November 2009 - 04:34 AM

Yeah I should clarify that. I'm not talking about everyone who disagrees with me, I'm also not directly talking about anyone here in this thread, but in general some people take it to the extreme. I've seen cases where code was rejected because there was a loose space after a semicolon at the end of a line. I can think of no other word than anal for those kinds of cases, and I honestly think some of these people suffer from OCD.

Again, I agree on the brackets, because that's as much a best practice issue (as I said above) as a style issue.

I've never seen a code editor freak out over a space when highlighting PHP, Java or any other space-ignoring language, and believe me, I tried a LOT of editors. Do you have an example of this? For languages like Python it obviously makes sense because they rely on indentation for logic, but apart from that I think any editor that can't be flexible with whitespace is worth its salt. I don't see the need for me to work around bugs in other people's editors, actually (nor for them to work around bugs in mine, of course).

I'm not really an Ajax guy (I focus more on the backend logic of our company's apps) so I can't comment on your last point.
0

#12 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,895
  • Joined: 04-October 08
  • Location:DC, USA

Posted 29 November 2009 - 08:14 AM

Thank you guys for pointing out things that are not so good in Yii and thank you guys for defending for me (:lol:).

Yii (previously Prado) was developed over several years. It reflects the gradual change of my programming philosophy. Therefore, part of the code may not be very consistent with the others and may not be perfect by PHP standards. Please let me know if you find these code. I am more than happy to correct or improve them.

Regarding coding standards, I tend to be very frugal in writing code: no spacing around brackets and operators, no extra brackets enclosing single if-statements, and using tabs instead of spaces for indentation. Forgive me if these "bad" habits make you read the Yii code difficultly.

But trust me, I'm a professional programmer with a lot of experience working in big (with hundreds of people) and small teams. The so-called coding standards are always a set of loose standards in these circumstances. Things like the above three aspects never appear in any of these standards that I have experience with. I guess that's why my habit like this.

We also had a discussion about indentation in this forum before. Many people, including me, don't like the Zend standards at all in this aspect. Using four spaces instead of a tab not only makes erasing code typing more, but also increases the file size (and potentially lower the performance? ;)) Also, the class naming convention used in Zend standards is simply ugly (it only appears in PHP, and never in any other languages even they didn't support namespace in the old days). The introduction of PHP 5.3 namespace makes this naming convention totally redundant.

I know many people like to add spaces around brackets and operators to increase readability. I didn't follow this convention for two reasons: it requires more typing (about 20% more); it makes code less consistent because people sometimes omit spaces in some places, which makes global searching a pain.

In summary, I think coding standards such as naming convention, comments and indentation, are far more important than details like spacing. The former is also much easier to (and should) be enforced in team work.
0

#13 User is offline   ekerazha 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 526
  • Joined: 10-October 08
  • Location:European Union

Posted 29 November 2009 - 09:45 AM

View Postqiang, on 29 November 2009 - 08:14 AM, said:

no spacing around brackets and operators

I'd like spacing around operators :)

Quote

no extra brackets enclosing single if-statements

This is GOOD programming.
Yii user #37
0

#14 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 29 November 2009 - 11:27 AM

Actually in the teams i've been working similar rules applied as Qiang mentioned. I really don't see a problem here. The more rules, the harder (and less motivating) for new team members to join. A set of loose rules is enough to ensure readability. I think it will always depend on the "coding spirit" (in lack of a better word) that leads the team. Some teams prefer strict rules, others don't and have no problem in bending their mind a little.

I can't agree, that these statements are not easily readable (even for a programmer with only basic experience):

        if($scenario==='')
            $scenario=$this->getScenario();

One can argue about the following example but i even do like this one for it's shortness:

            if(($path=Yii::getPathOfAlias($alias))!==false)
                Yii::setPathOfAlias($name,$path);
            else
                Yii::setPathOfAlias($name,$alias);

versus

            $path=Yii::getPathOfAlias($alias);
            if($path!==false) {
                Yii::setPathOfAlias($name,$path);
            } else {
                Yii::setPathOfAlias($name,$alias);
            }



That saves 2 lines and makes the code more compact. Everyone who knows about operator precedence and understands what a PHP expression is (and you should know your language, right?) should easily understand the middle example.
0

#15 User is offline   François Gannaz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 24-November 09

Posted 30 November 2009 - 04:57 AM

I know qiang gave its reason, so arguing won't lead anywhere, but just for the fun of it:

View PostMike, on 29 November 2009 - 11:27 AM, said:

            if(($path=Yii::getPathOfAlias($alias))!==false)
                Yii::setPathOfAlias($name,$path);
            else
                Yii::setPathOfAlias($name,$alias);

versus

            $path=Yii::getPathOfAlias($alias);
            if($path!==false) {
                Yii::setPathOfAlias($name,$path);
            } else {
                Yii::setPathOfAlias($name,$alias);
            }


That saves 2 lines and makes the code more compact. Everyone who knows about operator precedence and understands what a PHP expression is (and you should know your language, right?) should easily understand the middle example.

You mean:
            $path = Yii::getPathOfAlias($alias);
            if ($path !== false) {
                Yii::setPathOfAlias($name, $path);
            } else {
                Yii::setPathOfAlias($name, $alias);
            }


Honestly, which code is clearer?

I've known about operator precedence for more than twenty years, but I can't grasp the first line of your code in one glance. My eyes needs some time to part the line in blocks (they go left, right, left, right), my brain needs some time to sort them and apply operator precedence. Maybe it's because I'm used to this syntax, but reading the last code is much faster for me.

Now suppose you want to add an error flag inside your "if/else". Once in a while, one will write:
            if(($path=Yii::getPathOfAlias($alias))!==false)
                Yii::setPathOfAlias($name,$path);
            else
                Yii::setPathOfAlias($name,$alias);
                $error = true;
I know that's a stupid mistake, but such things can happen to anyone.

The problem is not that people won't understand the code, it's how fast they'll read it, and how to reduce errors when reading and modifying.

BTW, in Yii it's usually
}
else {
Why those 2 useless spaces? Anyone sacrifying fast coding to readability? ;)
0

#16 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 30 November 2009 - 05:28 AM

View PostFrançois Gannaz, on 30 November 2009 - 04:57 AM, said:

I've known about operator precedence for more than twenty years, but I can't grasp the first line of your code in one glance. My eyes needs some time to part the line in blocks (they go left, right, left, right), my brain needs some time to sort them and apply operator precedence. Maybe it's because I'm used to this syntax, but reading the last code is much faster for me.


Hehe, well as you said: not much sense in arguing, because yes, i personally find the shorter code easier to read. Like if my brain adapted that pattern.

So can we agree that it's all a matter of getting used to?
0

#17 User is offline   ekerazha 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 526
  • Joined: 10-October 08
  • Location:European Union

Posted 30 November 2009 - 08:16 AM

My university professor in a course of the first year (computer engineering faculty), did cut points at the exam if you used brackets for single if-statements: they are useless so they don't have to be there. If you add other lines, THEN you also add brackets.
Yii user #37
0

#18 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,727
  • Joined: 17-January 09
  • Location:Russia

Posted 30 November 2009 - 09:44 AM

ekerazha
It is just a personal preference like $underscored_variables vs $camelCasedVariables. Your professor was not right about cutting points for it.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#19 User is offline   ekerazha 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 526
  • Joined: 10-October 08
  • Location:European Union

Posted 30 November 2009 - 10:08 AM

View Postsamdark, on 30 November 2009 - 09:44 AM, said:

ekerazha
It is just a personal preference like $underscored_variables vs $camelCasedVariables. Your professor was not right about cutting points for it.


Maybe, but she's a university professor who wrote dozens of books about computer engineering and software engineering, so she can do this.
Yii user #37
0

#20 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,727
  • Joined: 17-January 09
  • Location:Russia

Posted 30 November 2009 - 11:06 AM

She can since it's her class and her rules. But this does not mean everything is the same in a real world.

I've got your point. But there is another (very reasonably described by François Gannaz).
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

Share this topic:


  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users