Yii Framework Forum: Coding Standards - Yii Framework Forum

Jump to content

  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

Coding Standards Will the coding standard change for 2.0?

#41 User is offline   ekerazha 

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

Posted 02 October 2011 - 12:52 AM

View Postcreocoder, on 01 October 2011 - 08:01 PM, said:

May be this is a good idea too. But only in comparsions (and maybe 'key' => 'value' delimeters, expressions). What i mean:
if($x === true)


Yes, tolerantly. But:

if ($x === true)


And things going bad.


In my opinion the space after "if" is much better.

It could be my math background, but I never use a space after a function call: f(x) not f (x).

But "if" isn't a function, it's a condition... so space.
Yii user #37
1

#42 User is offline   creocoder 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 198
  • Joined: 09-March 09
  • Location:*.php

Posted 02 October 2011 - 05:45 PM

Quote

But "if" isn't a function, it's a condition...

`If` is control structure, condition inside it. In php a lot of control structures (foreach, for, while, ...). Spaces are needed after all? It is much easier to not put a space after them. Why complicate style? It's pointless and there is no advantage. Especially considering that from Yii 1.0.0 there is no spaces after control structures. And this is great, it is practical, for which many thanks Qiang. I love Yii style as it is, i learned this style, i write all my extensions using this style, all applications. And I do not think that I alone :) Moreover, I am sure that most of us. So why should we change it?

Some of the changes in style are justified (for code folding, for debugging, for readability), but spaces after control structures never. There is no rational arguments why they should be.

P.S. Style should be such that it was possible to justify every detail and aspect. Because it leads to discipline. Discipline leads to order. Order leads to perfective code. Perfect code leads to immortality. This is of course a joke, but you get the idea.
No good, no bad, only consequence.
1

#43 User is offline   Psih 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 114
  • Joined: 30-June 10

Posted 03 October 2011 - 06:31 AM

View Postcreocoder, on 02 October 2011 - 05:45 PM, said:

`If` is control structure, condition inside it. In php a lot of control structures (foreach, for, while, ...). Spaces are needed after all? It is much easier to not put a space after them. Why complicate style? It's pointless and there is no advantage. Especially considering that from Yii 1.0.0 there is no spaces after control structures. And this is great, it is practical, for which many thanks Qiang. I love Yii style as it is, i learned this style, i write all my extensions using this style, all applications. And I do not think that I alone :) Moreover, I am sure that most of us. So why should we change it?

Some of the changes in style are justified (for code folding, for debugging, for readability), but spaces after control structures never. There is no rational arguments why they should be.

P.S. Style should be such that it was possible to justify every detail and aspect. Because it leads to discipline. Discipline leads to order. Order leads to perfective code. Perfect code leads to immortality. This is of course a joke, but you get the idea.

Readthatunspacedcodeyourselfplease!
2

#44 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,353
  • Joined: 12-October 09
  • Location:Croatia

Posted 03 October 2011 - 06:46 AM

View PostPsih, on 03 October 2011 - 06:31 AM, said:

Readthatunspacedcodeyourselfplease!


Please refrain yourself from comments like this...

The example you gave is not the same as if to use one space after if or not...
Find more about me.... btw. Do you know your WAN IP?
0

#45 User is offline   Psih 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 114
  • Joined: 30-June 10

Posted 03 October 2011 - 07:21 AM

View Postmdomba, on 03 October 2011 - 06:46 AM, said:

Please refrain yourself from comments like this...

The example you gave is not the same as if to use one space after if or not...


But that's exactly how code looks when a it's done. Yes, maybe simple code like get data from db and pass it to the views will look fine without spaces, but framework code isn't so simple. And I tend to do projects where simple code is not in abundance and most business logic is not trivial leading to tons of if's, foreach'es, function/method call and so on. And believe me, when you open that code even after a few weeks - you want it be perfectly easy readable.
Reading something like
if($some_var>10||$something=='somethingelse'){

is a major headache even reading your own code after a while. Reading someone's code in that style is just ridiculous - I have to reformat it all over to make sense of it.

Code readability is built on a ton of tiny things that become what we call readability. And we, humans, are all built the same way and we read the same way. You can train and make yourself to read anything - some can read hexadecimal dumps as it was plain text, but that doesn't mean that it's a readable form, don't you agree?

Separating items in code is one of those things that make reading code easier. You don't have books witch do not have spaces between some words of phrases. Here is the same thing - it is easier to process separated by space elements that a string with no spaces and trying to identify where there is what.

And for example about the braces.
I use them like this:

class MyClass
{
    public function test()
    {
        if ($expression) {
            // code
        }
        
        foreach ($item as $key => $value) {
            // code
        }
    }
}

function ()
{
    // code
}


Why? Because when I skim the code, I can identify what the block of code is by the the layout of the braces. If braces are on their own lines - that's method/function/class, in other case is a conditional block or a loop. That really helps a lot.
1

#46 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,353
  • Joined: 12-October 09
  • Location:Croatia

Posted 03 October 2011 - 07:27 AM

View PostPsih, on 03 October 2011 - 07:21 AM, said:

Reading something like
if($some_var>10||$something=='somethingelse'){



Read again the above posts... it's not about spaces inside the condition... it's just about the space after the if...

if($some_var > 10 || $something == 'somethingelse')

or
if ($some_var > 10 || $something == 'somethingelse')

Find more about me.... btw. Do you know your WAN IP?
0

#47 User is offline   ekerazha 

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

Posted 06 October 2011 - 04:07 AM

View Postcreocoder, on 02 October 2011 - 05:45 PM, said:

Some of the changes in style are justified (for code folding, for debugging, for readability), but spaces after control structures never. There is no rational arguments why they should be.

P.S. Style should be such that it was possible to justify every detail and aspect. Because it leads to discipline. Discipline leads to order. Order leads to perfective code. Perfect code leads to immortality. This is of course a joke, but you get the idea.

You could write everything without spaces (and newlines too... just write everything inline, semicolon separated), the argument is readability. Like I've already said, I just avoid spaces for function calls because in math I never used them... f(x) and not f (x)... but "if" is not a function.

If you want to avoid useless things, maybe you can avoid to use useless braces for single line statements just to workaround a debugger issue.

This post has been edited by ekerazha: 06 October 2011 - 04:15 AM

Yii user #37
0

#48 User is offline   samdark 

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

Posted 06 October 2011 - 04:14 PM

No spaces at all or spaces everywhere are extreme cases. We'll try to avoid these.
Yii 1.1 Application Development Cookbook

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

#49 User is offline   ololo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 23-January 11
  • Location:Minsk, Belarus

Posted 22 October 2011 - 07:31 PM

do you plan to use static code analysis tools to enforce your coding standarts? maybe it worth to develop (or customize some existing) rules for e.g. PHP_CodeSniffer, use it for core development and share it to the community
1

#50 User is offline   rinat.io 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 20-January 11

Posted 05 May 2013 - 12:40 PM

Did you consider following PSR-2 coding style?
0

#51 User is offline   samdark 

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

Posted 05 May 2013 - 04:09 PM

Yes, we've considered it and decided to go with our own. It's not that different from PSR-2. Tabs instead of spaces mainly.
Yii 1.1 Application Development Cookbook

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

#52 User is offline   egorpromo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 66
  • Joined: 02-December 12

Posted 12 July 2013 - 12:22 PM

Use curly brackets even if there is one code line inside brackets
if ($a == $B)
{
	$var=1; //only one line
}
else
{
	$var=2;
	//many lines of code here
}

This way decreases number of errors.
I use it continually and it is good and convinient practice.
0

#53 User is offline   Abhishek Shah 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 20-July 12
  • Location:New Jersey,USA

Posted 13 July 2013 - 12:47 AM

According to me,when we put tabs,and than ,start code after tabs,than code is not readable.
If out line is big,for example findByAttributes() qurey,after 2 tabs,you have to scroll left-right to see the whole code,so I am not preferring tabs.But let yii core team decide,what to follow.Please make a good document for it,so anybody can easily follow the standards,& code easily.
Feel free to ask for help,
Abhishek.
Freelancer.
Yii Basic Gii With Export Pdf Excel
0

#54 User is offline   samdark 

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

Posted 14 July 2013 - 02:06 PM

You can configure your IDE for any width of the tab. At least in NetBeans, PhpStorm you can do so. Document is already there as well as codesniffer style:

https://github.com/y...work-code-style
Yii 1.1 Application Development Cookbook

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

#55 User is offline   Abhishek Shah 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 20-July 12
  • Location:New Jersey,USA

Posted 20 July 2013 - 02:30 AM

View Postsamdark, on 14 July 2013 - 02:06 PM, said:

You can configure your IDE for any width of the tab. At least in NetBeans, PhpStorm you can do so. Document is already there as well as codesniffer style:

https://github.com/y...work-code-style

Thanx samdark.
Feel free to ask for help,
Abhishek.
Freelancer.
Yii Basic Gii With Export Pdf Excel
0

Share this topic:


  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

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