Welcome Guest, Not a member yet? Register   Sign In
Good Coding Styles
#1

[eluser]bigdaddysheikh[/eluser]
Hey guys,

I am 4 weeks into CI and enjoying the development process. Coding is actually fun now, no more mundane stuff. I am also learning Ruby at the same time to be diverse.

Anyways, my question is not about specific code but rather styles. For example, for the design genre there is a whole concept of proper design elements. CSS use, proper layout etc...

But I find that there is very limited knowledge for programming. Concepts, good practices, theories, ways to tackle a project. Each book goes straight into code.

There has to be articles out there that suggest layouts, good coding practices (other than commenting), logic thought etc...

I am an intermediate php guy, not a guru. But everytime I code in CI, I KNOW there is a better way of doing but I just don't know how.

So I am asking you guys, to share any knowledge on good practices, techniques for either CI or MVC model.


Thanks in advance.
#2

[eluser]Colin Williams[/eluser]
Post some code you feel a bit awkward about and maybe we can feed you some pointers. Good code, in my opinion, is many things. It's efficient, it's logical, it's flexible, it's abstracted appropriately, and it's well commented.

Efficiency can be reached in many ways, like reducing the amount of function calls and not repeating code when necessary. Logical code means it can be read and understood by others reading it, for one. It also means you're doing things as straight-forward as possible, in the fewest steps possible.

Flexible code leads to a better API, leading also to less errors and happy developers that must interface with it. You'll notice that a lot of the methods of core CI libraries say things like, "You can pass an array, an object, or pass in values discretely..." This is flexible code.

Abstracted code goes back to efficiency, and sometimes you'll hear this called the "DRY principle," meaning "Don't Repeat Yourself." Finally, commenting well doesn't mean commenting every line. Too much commenting can be futile. Take a look through a core library in CI and notice how they comment every method, the class itself, and how they use comments to add visual separation between classes and other comments.

Hope that helps a bit.
#3

[eluser]bigdaddysheikh[/eluser]
Hey,

That is excellent. I have been looking at CI code and trying to mimic the procedures.

The DRY rule is something I want to follow. I repeat my code a lot, but I just do not know how to go about breaking it up. Some code is relational to other methods while some is not.
#4

[eluser]m4rw3r[/eluser]
You may also find this interesting: Ellis Lab's Developer Guidelines

It has helped me making my code more readable, though I don't agree with everything it says. Tongue
#5

[eluser]Matthieu Fauveau[/eluser]
Ellis Lab Developer Guidelines is a must read.
#6

[eluser]Colin Williams[/eluser]
I wish the dev guideline were in the CI user guide so I would have caught them earlier. Gonna pour through them tonight. Like m4rw3r, I don't agree with it all (what I've read) but I want my contributions to follow 'em regardless.
#7

[eluser]Randy Casburn[/eluser]
You'll also notice the guidelines are not completely followed within the CI libraries either. The staunch language about them not being optional yada yada...you get the picture. Try your best to follow the rules presented and be consistent with the code you see in the library. When you move to a different project with a different library or API, the first thing you want to do is look at how the developers have written the code, what conventions they use, how they comment, how frequently they comment, etc. Then mimic the library developers.

You'll do great.

Randy
#8

[eluser]Bramme[/eluser]
[quote author="Colin Williams" date="1216786980"]I wish the dev guideline were in the CI user guide so I would have caught them earlier. Gonna pour through them tonight. Like m4rw3r, I don't agree with it all (what I've read) but I want my contributions to follow 'em regardless.[/quote]

Same here, but I've just gone through them quickly now and there really is some good stuff in it.

I've been coding

Code:
class MyClass {
    function MyFunction() {
    }
}

for all my coding years, but I think I'll switch to the codeigniter way of indenting...
#9

[eluser]Colin Williams[/eluser]
Same here, Bramme. It's just little things that bug me, like how the ! is spaced out in IF controls:

Code:
if ( ! $bool)
{
  ...
}

I understand how that is effective, but it just visually bothers me, and I'll probably only do it to stick to the guidelines. Probably in last minute sweeps before committing stuff.
#10

[eluser]Colin Williams[/eluser]
Quote:I repeat my code a lot, but I just do not know how to go about breaking it up. Some code is relational to other methods while some is not.

Well, there certainly are times you don't want to be DRY. If you've read the Code Igniter docs, one principle that they followed is "loose coupling," meaning modules/libraries depend very little on each other. This could mean that sometimes small processes are not shared amongst libraries, but that's a good thing.




Theme © iAndrew 2016 - Forum software by © MyBB