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?

#21 User is offline   kernel 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 91
  • Joined: 22-November 10

Posted 27 August 2011 - 07:18 AM

:mellow: I see coding standar is not so important because you could format it by the IDE easily when you are reading codes and let people to write codes in ways make them comfortable.
0

#22 User is offline   Psih 

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

Posted 29 August 2011 - 03:26 AM

View Postkernel, on 27 August 2011 - 07:18 AM, said:

:mellow: I see coding standar is not so important because you could format it by the IDE easily when you are reading codes and let people to write codes in ways make them comfortable.

;D
Should I say that IDE auto-formatting usually breaks on constructs having more than 2-3 formatting elements.

Besides, you checkout the framework from SVN and what - format the whole project? o.O
0

#23 User is offline   Say_Ten 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 109
  • Joined: 17-September 10

Posted 30 August 2011 - 11:13 AM

A project should be written to meet one set of coding standards, if you work on a project you follow that projects coding standards, even if you don't like it. Personally:

Tabs vs Spaces
Tabs for indentation and spaces for aligning, the two are separate. I prefer tabs as everyone can have it flow how they want it. The trouble with treating spaces as tabs is that if one person commits two spaces and another three it falls down. Personally my tab stop is 3!

Yoda Style
I'm all for this, given a little time you start doing it without thinking and it's safer. Once you're programmed to do it you find it doesn't take longer to read the code but there is a little re-training involved. Saying you'll never make that assignment mistake is a little naive also ;p

Single Line Conditions
In favour of these, they make it absolutely clear that line's scope. It may seem overkill but again it's a little bit safer. Especially if there's lots of lines together sometimes that indent is not entirely obvious and you skip over it. On that note, I always put the opening brace on it's own line too.

Finally
The core Yii! team set the coding standards and as it stands they are the ones that are going to be coding using it. However if they do decide to grow the team then the decisions of coding standards that are easy to pick up and use should be considered and surely people using Yii! day in day out are a good source on opinions on this matter.
2

#24 User is offline   SystemicPlural 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 132
  • Joined: 12-April 10

Posted 01 September 2011 - 04:42 AM

View PostGustavo, on 26 August 2011 - 01:36 PM, said:

I think you should leave this discussion.

The core team should be able to code the way they know better and are more productive, focusing on what really matters.

I would agree with this discussion if the currect code was not well written or readable. But it is.

You wouldn't like someone coming to your work and telling you how you should write your code, would you ?

I agree, it is up to the core team to agree on coding standards.

This is despite the fact that I would do things differently.

It would however be great if they could publish them.
0

#25 User is offline   mindplay 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 397
  • Joined: 03-September 09
  • Location:New York

Posted 02 September 2011 - 09:28 AM

View Postsamdark, on 18 August 2011 - 09:44 AM, said:

if (true === $dummy)

This one is called Yoda Condition because of spelling. The goal is to prevent accidental assignment in conditions:

if ($dummy = true)


I don't like it because of Yoda-style.


So you're willing to write your expressions backwards, making the code less legible, prevent yourself from making typos.

I don't get it.

If you're worried about making this mistake, the if ($dummy = true) statement can be easily detected and highlighted by an IDE. This problem should not be solved by garbling your code, but by using proper tooling.
0

#26 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 02 September 2011 - 10:33 AM

If you are as l33t as you appear to be, why are you letting your tools take the responsibility of making sure that your code is correct?
My advice would be to trust experienced programmers.

Oh, and another thing you've got wrong: Samdark is actually against Yoda conditions as is apparent from the quote you posted.

As a C++ programmer as well as a PHP programmer, I embrace all coding conventions which helps to ensure that my code is correct.
But that's probably just me. ;)
"Less noise - more signal"
0

#27 User is offline   samdark 

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

Posted 02 September 2011 - 11:25 AM

Yep, I'm not a star wars fan :)
Yii 1.1 Application Development Cookbook

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

#28 User is offline   lubosdz 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 115
  • Joined: 25-July 10
  • Location:Slovakia, Bratislava

Posted 08 September 2011 - 10:42 AM

View Postmdomba, on 25 August 2011 - 01:34 AM, said:

There is one "great" +1 point in favor of braces for oneliners... debugging!

I don't know for other debuggers but in Netbeans with xdebug you cannot debug the oneliner if there are no braces around it... you cannot even set a brakpoint there...


YES, YES, YES!!!
Same happens in NuSphere PHPEd.
PLEASE, PLEASE, PLEASE - add brackets to a single line and stop doing this:

if(is_file($viewFile.$extension))
	return Yii::app()->findLocalizedFile($viewFile.$extension);
else if($extension!=='.php' && is_file($viewFile.'.php'))
	return Yii::app()->findLocalizedFile($viewFile.'.php');
else
	return false;


because it is really hard to guess breakpoints in debug mode.

Instead, it should go:
if(is_file($viewFile.$extension)){
	return Yii::app()->findLocalizedFile($viewFile.$extension);
}else if($extension!=='.php' && is_file($viewFile.'.php')){
	return Yii::app()->findLocalizedFile($viewFile.'.php');
}else{
	return false;
}


I recommend following the Pear coding standards.

Lubos
Yii extension: Captcha Extended

Greatest discoveries in 22nd century will be about the gravitation. | http://www.synet.sk | http://ipdf.sk
1

#29 User is offline   mbi 

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

Posted 08 September 2011 - 12:16 PM

for me the frameworks code base is a black box
but the community around yii should agree to a coding standard for extensions
0

#30 User is offline   kernel 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 91
  • Joined: 22-November 10

Posted 10 September 2011 - 11:50 PM

Agree on black box
0

#31 User is offline   R.K. 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 117
  • Joined: 20-September 10

Posted 12 September 2011 - 05:49 AM

I think class methods that are declared as protected or private must begin with a single underscore.
0

#32 User is offline   ekerazha 

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

Posted 12 September 2011 - 09:08 AM

View PostR.K., on 12 September 2011 - 05:49 AM, said:

I think class methods that are declared as protected or private must begin with a single underscore.


That was a convention for languages which didn't really support visibility declaration... you used _ to remember that a method/property was "private"
Yii user #37
0

#33 User is offline   ekerazha 

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

Posted 12 September 2011 - 09:15 AM

View Postlubosdz, on 08 September 2011 - 10:42 AM, said:

Same happens in NuSphere PHPEd.

You should suggest to fix their bug
Yii user #37
0

#34 User is offline   lubosdz 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 115
  • Joined: 25-July 10
  • Location:Slovakia, Bratislava

Posted 14 September 2011 - 05:36 AM

View Postekerazha, on 12 September 2011 - 09:15 AM, said:

You should suggest to fix their bug


There's nothing to fix, since there's no bug.

Server parses executed script and sends to IDE debugger listener information about executed line. Debugger listener check if such a line has a breakpoint set by developer - if yes, it pauses script execution and waits.

The problem is that it is hard to guess which line will be a valid breakpoint line parsed by server. If you put e.g. two PHP commands into a single line or if you dont use brackets (but single line instead) then you have a chance 50% that you will set breakpoint at line that will be skipped. Then you must try more breakpoints to "find" the right breakpoint line. It's a bit of frustration sometime. That's why I recommend PEAR syntax - it encourages debugging-problem-free code standards.

It's hard to explain - if you try you would see...
Yii extension: Captcha Extended

Greatest discoveries in 22nd century will be about the gravitation. | http://www.synet.sk | http://ipdf.sk
0

#35 User is offline   ekerazha 

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

Posted 14 September 2011 - 06:14 AM

View Postlubosdz, on 14 September 2011 - 05:36 AM, said:

It's hard to explain - if you try you would see...

Yeah, indeed I don't really understand the issue... it seems a non-issue to me
Yii user #37
0

#36 User is offline   Ben 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 270
  • Joined: 15-March 09

Posted 15 September 2011 - 07:26 PM

Imagine this code:

01: if ($condition)
02:  foo();
03: else
04:  bar();


If you set a breakpoint on line 2 or 4, the debugger won't break. Instead, you had to set the breakpoint at the first line of foo() or bar().

With the following code, there are no such problems:

01: if ($condition) {
02:  foo();
03: } else {
04:  bar();
05: }


You just set the breakpoint and it stops execution. Might be a problem of the debugger, or some sort of PHP opcode generation/ optimization. Don't really know. Maybe it will get fixed some day. But till then, braces help debugging.
Don't like ads in my sig...
1

#37 User is offline   ekerazha 

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

Posted 16 September 2011 - 05:56 AM

If it is like that, then it looks to me like a debugger issue. And in my opinion that code is much more readable without useless baces.
Yii user #37
0

#38 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 16 September 2011 - 07:49 AM

Regarding braces for single line if statements here is a nice discussion - http://stackoverflow...es-bad-practice
Find more about me.... btw. Do you know your WAN IP?
2

#39 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 16 September 2011 - 08:05 AM

+1 to that. ;)
"Less noise - more signal"
0

#40 User is offline   creocoder 

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

Posted 01 October 2011 - 08:01 PM

Quote

I hope you stick with existing Yii standards - it's the only code standard that ever made any sense to me.

Completely agree. `Only` here is very important word.

Quote

Always, always and always uses spaces instead of tabs!!

Never, never and never use spaces instead of tabs for indention. Don't change fw style at this part. It's very important.

Quote

brackets for one-line else etc.

May be this is a good idea. But please don't change brackets style from this:
if
{
    ...
}
else
{
    ...
}


to:

if {
    ...
} else {
    ...
}


It's ugly, in addition to another 10 reasons (code folding for example). And one of them: open any fw file. Immediately we can say that this is Yii. Not f...g Zend, not b...t Symfony(even 2). It's Yii.

Quote

spaces in comparisons

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.
No good, no bad, only consequence.
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