Jump to content
Larry Ullman's Book Forums

Small Issues In Yii


Recommended Posts

The style of coding in Yii seems to be


if (etc)





as opposed to


if (etc ) {




I see in your tuturiols Larry you prefer the 2nd style which i have come to prefer myself but what should we do in Yii, use our conventional way which mean we will have mixed code or just use the new style? (Or do we change all the parentheses in Yii to make them look nice, lol)


Also another thing that bugs me is, is say saving things in the database like for instance first_name, we use the underscore between the two words. The thing is when we make properties in Yii as its OOP and we are using classe we use Camel Case for our properties, so if we declared first_name as a property it would be ugly rather than the correct way of firstName. I find i can get confused when passing over data from a controller to view and thought it might be a good idea to rename the database columns with Camel Case. So firstName, lastName, activationCode etc, what do you think of this or others here who may have views?

Link to comment
Share on other sites

The most important thing is to be consistent. Without concistency, things might get confusing. PHPs core functions is a horrible example of this. Chaos.


The next thing is conventions. You should strive to follow them, at least closely. Use camelcase for YII attributes and underscores for database tables and names. The most important reason for this is because others can easily identify when you work with YII and when you use Active Recors for DB manipulation. It's important to tell those apart quickly for several reasons.


Coding style is subjective, and you'll find a lot of strong opinions on this. I think the most important thing regarding coding style is making code readable/scannable. We are not machines, so strive for readability, define good function names, and write good documentation. If a variable will make your code easier to understand, then use it.


I actually adopted my code style on a collaboration project last year. As it was his projects, I followed his code style. The main reason was consistency, as inconsistence code is bad code in my opinion. I really liked his style, so I started using a modified style daily.


Here's some things I like to focus on when I write code:

- Write readable, simple and elegant code. I like my methods when they can almost be read like normal english.

- I think a lot before I start coding. I like to have a clear layout in my mind about the end result.

- Use time finding good method names. They should be clear and consistent

- I use longer, more describing names for private methods

- Responsibility. Try to make sure one method performs one task. This may not be optimal in all cases, but generally will. Db::connectAndQuery($query), User::authenticateThenLogin() or File::uploadIfValid($file) are examples of methods that do to much.

- Identify the exceptions. Throw exceptions early on to not waste resources.

- Don't nest if statements to deep. I try to never nest more than one if, but I will break this rule for readability. A lot of nested ifs-else can often be solved differently, and will be more readable.

- Identify code duplication and split those into new (often) private methods.

- Offer methods that will cut down on application logic. e.x hasNext(), resultsExits(), or even isValid() (that may be a terrible name in some cases)


These are my personal rules. They are breakable, as the goal is good code, not the rules themselves. My favorite type of data structures are tree structures. They can be very clear and simple, elegant and done with small amounts of code. Unfortunately, PHP code can never look as beautiful as a strongly typed language, but It can at least be readable.


Cool topic, Edward. I love such topics for several reasons.

  • Upvote 1
Link to comment
Share on other sites

Thanks for writing such a lot, yeah i guess you have a point there separating property names from active record or database names.


I Agree with what you are saying, i will do my best with naming conventions, i would like to build more to see what happens as now i don't have so much experience like yourself. CSS is somewhat similar you have to pick good clear names and know how to separate for different pages in larger projects.


Those methods that you mentioned i will be working on those soon in the user model() i know i have to move the logic into the model. I just want to see that i can get the points in Yii working first of all then i will concentrate more on tidying up. Like you i think of coding before and even while i am at it, its always in my mind this stuff such a lot of it.

Link to comment
Share on other sites


  • Create New...