Welcome Guest, Not a member yet? Register   Sign In
Code Igniter and CamelCase naming
#1

[eluser]Unknown[/eluser]
Hello all,

I would like to tell you about an experience I had with Code Igniter and CamelCase naming. I had a major problem the other day when I was deploying a re-structured version of my website using Code Igniter. My site was unavailable for 30mins while I tried to sort out what the cause of the error messages was. I finally discovered that CodeIgniter wasn't happy with the CamelCase naming I had used for the filenames of my models. This resulted in me having to change the filenames to all lowercase and make the changes in over 100 places where I had referenced those classes.

What I would like to know is why Code Igniter has such tight restrictions when it comes to file naming and prefers to see practically everything in lowercase?? Beats me.

Cheerio
#2

[eluser]jedd[/eluser]
[quote author="newbietoprofessional" date="1237777175"]
I would like to tell you about an experience I had with Code Igniter and CamelCase naming. I had a major problem the other day when I was deploying a re-structured version of my website using Code Igniter. My site was unavailable for 30mins while I tried to sort out what the cause of the error messages was.
[/quote]

Hi Newbie - it's often good to have a staging site. Say on your local computer. Where you can test things like production releases, before releasing them into production. VMware, or VirtualBox, are two good options to look at.

Quote:I finally discovered that CodeIgniter wasn't happy with the CamelCase naming I had used for the filenames of my models.

Hmm. If only you'd read the manual first, you could have saved yourself a lot of grief! The phrase - [url="http://ellislab.com/codeigniter/user-guide/general/models.html#anatomy"]The file name will be a lower case version of your class name[/url] - lacks ambiguity.

Quote:This resulted in me having to change the filenames to all lowercase and make the changes in over 100 places where I had referenced those classes.

I don't see why your class names would have changed. They can be anything you like, though of course the manual does make it clear they should be first-letter-upper, and the rest lower. Did you actually try changing the half dozen filenames first, because I think that should have been sufficient.

Quote:What I would like to know is why Code Igniter has such tight restrictions when it comes to file naming and prefers to see practically everything in lowercase?? Beats me.

Now that is a good question. My gut feel is so that on real operating systems (I gather you develop on a pretend operating system and run your production on a real one, yes?) where case is respected and relevant, it's measurably faster to hit a file by name, rather than by searching for it in a directory. If you make an arbitrary rule - in this case that the filename is always lower case - then it's very easy to algorithmically identify what the filename of a given class name will be - you just strtolower() the thing and you've got it.

If you let people do whatever they like with filenames, you're introducing a) more complexity into the code as it has to handle permutations, b) a greater cost to the 'find this file and load it' function, and consequently c) a reduction in performance.

That's my guess, but there may be other reasons.
#3

[eluser]Unknown[/eluser]
Hi jedd,

Yes, you're right, I didn't read the manual, I just jumped straight into CodeIgniter. Smile Also regarding the CamelCasing, strange enough it did work on my local development version, however the moment I uploaded it to the web it just went into chaos.

Anyway, your response has been very informative. I have indeed learnt something interesting this time round.

Cheerio
#4

[eluser]jedd[/eluser]
You're allowed to jump in without reading the manual - I suspect we all did it - it just means you can't complain about it later, that's all Wink

Quote:Also regarding the CamelCasing, strange enough it did work on my local development version

You mean the filenames or the class names? The latter, as I say, shouldn't have been so touchy - I don't think you should have needed to modify them. A lot of people post code that has camel case model and controller names, and their stuff seems to work. (Personally, I find CC harder to scan, and at 70wpm I don't notice the extra time to put in underscores.)

If the former, I'm guessing it's because you are developing on an NTFS/FAT file system, where case is maintained, but ignored - so a CI internal library call to load the model file for the class 'MyFunkyModel' would work, because the file system would still happily return the file even if it was called something like 'mYfUnKymodEl.php'.

I speculate your production system is probably running some GNU/Linux distro, where 'mYfUnKymodEl' and 'MyFunkyModel' are two different files entirely, and can happily co-exist in the same directory. A request by CI to the file system for myfunkymodel.php wouldn't return either of those earlier two files.

Coming back to an earlier point, you may want to set up a local virtual machine running the same environment as your production system (or as close to it as you can get). There aren't many of these kinds of platform-related gotchas, but since it's $-free to run such systems, and gives you extra options (staging against different versions of PHP, MySQL, etc) it's pretty compelling.
#5

[eluser]xwero[/eluser]
[quote author="jedd" date="1237780165"]
Quote:What I would like to know is why Code Igniter has such tight restrictions when it comes to file naming and prefers to see practically everything in lowercase?? Beats me.

Now that is a good question. My gut feel is so that on real operating systems (I gather you develop on a pretend operating system and run your production on a real one, yes?) where case is respected and relevant, it's measurably faster to hit a file by name, rather than by searching for it in a directory. If you make an arbitrary rule - in this case that the filename is always lower case - then it's very easy to algorithmically identify what the filename of a given class name will be - you just strtolower() the thing and you've got it.

If you let people do whatever they like with filenames, you're introducing a) more complexity into the code as it has to handle permutations, b) a greater cost to the 'find this file and load it' function, and consequently c) a reduction in performance.
[/quote]
I think it has more to do with consistent coding style.

I don't think letting people free in naming files and classes would hinder performance. I think it would give developers less headaches because if they load files they have to do it with their proper name because CI will not alter it then. But a configuration option that loads files, like helpers and language files, with a camelcase extension instead of an underscore extension would be needed.

The problem i see is when a developer goes for camelcasing, he/she still has to use all the underscore methods the framework classes provide. So this brings us back to coding style consistency.

On the other hand the developer will be able to identify which methods belong to that application and which ones are from the framework. If that is more important than coding style who are the EL developers to make that decision for them?
#6

[eluser]jedd[/eluser]
Hi xwero,

I wouldn't be so quick to say there's no performance hit involved.

I recall the WINE and SAMBA guys had all kinds of problems with trying to map a case-insensitive virtual file system over a case-sensitive one. Lots of things become expensive - especially proving out that a particular file does not exist -- you have to scan the entire directory, and normalise each filename to compare it against the, also normalised, filename you're looking for. Sure, you can start to cache such things, but managing caches are expensive and complicated in turn, all the more so in an interpreted language. And if the goal is to reduce complication while squeezing every last gram of performance out of the system, why would you?

In comparison, the single $model = strtolower($model); line in libraries/Loader.php that obviates all that mucking about.

I did a quick search but couldn't find any pleasantly definitive benchmarks on the intergoogle, but I'm sure they're out there. The effect is perhaps reduced in our situation by the fact that most people would not have bucketloads of files in their model or controller directories.

Quote:The problem i see is when a developer goes for camelcasing, he/she still has to use all the underscore methods the framework classes provide. So this brings us back to coding style consistency.

I concur on this point of coding style and consistency.

Quote:On the other hand the developer will be able to identify which methods belong to that application and which ones are from the framework. If that is more important than coding style who are the EL developers to make that decision for them?

I don't agree with this one. One of my models is called Member. Am I using camelcase in my project? Alternatively, is the View function in there an application or a framework method?

Short prefixes are a far more effective way to provide core vs app-specific identification.
#7

[eluser]xwero[/eluser]
You don't need to scan the directory to find a variation on the filename, just check if the file name added by the developer exists or not, this works on all file systems.
If a developer doesn't code case sensitive it will be his/her problem if the test server has a case insensitive file system.

If you want the object added to the CI instance named differently you can use the parameters of the load methods.

If your model is called Member it can be both, like the base classes Controller and Model can be used in both coding styles.

You are right there are other ways to separate the app specific classes and methods from the core ones but i wanted to point out coding style does not trump everything else.
#8

[eluser]Unknown[/eluser]
If you're not a fan of CodeIgniter naming convention tyranny and want to use CamelCase file names do this

1. Make a file called path/to/app/application/core/My_Loader.php
2. In the file make a class like this

class My_Loader extends CI_Loader
{

}

3. Find the CI_Loader class in path/to/app/system/core/Loader.php
4. Copy method 'public function model'
5. Paste it into My_Loader
6. Delete the the line '$model = strtolower($model);' and the line '$model = ucfirst($model);'

Shazam!




Theme © iAndrew 2016 - Forum software by © MyBB