Jump to content

POLL: Function and Variable naming conventions, which do you use and why?


What naming convention do you use?  

19 members have voted

  1. 1. What naming convention do you use?

    • camelCase for functions AND variables
      9
    • lowercase for functions AND variables
      0
    • lowercase + underscores functions AND variables
      3
    • camelCase for functions, lowercase for variables
      1
    • cameCase for functions, lowercase + underscores for variables
      4
    • lowercase for functions, camelCase for variables
      0
    • lowercase for functions, lowercase + underscore for variables
      0
    • lowercase + underscores for functions, camelCase for variables
      0
    • lowercase + underscores for functions, lowercase for variables
      2


Recommended Posts

Wish we could create multiple polls per post then I wouldn't have to create so many options!  :P

 

Anyway, here's to find out what conventions our fellow PHP coders use.

 

(I realize that a lot of us are forced into conventions depending on a particular framework but just answer what you would usually use)

 

My answers:

camelCase for functions

lowercase + underscore for variables

 

I guess because i got into the habit of naming all database stuff lowercase + underscores I decided to do the same with variables to make persistence easier :)

 

PS - Let me know if i'm missing any other options!

  • Replies 51
  • Created
  • Last Reply

Top Posters In This Topic

mine

lowercase and _'s for variables and camelCase for functions.

 

i like to use the camelCase cause i could have a command like:

 

insertCode() for like a forum txt box

or

insertImg() so it would tell me what im inserting and it would be a clean command, instead of

insert_image or something.

 

just cleaner and easier for me :)

I just try to stick with the standards for the language I'm using... I really don't have a preference as they are all equally readable to me.

 

Why so opposed to camelCase, corbin? just one of those things?

nice, interesting results, keep 'em coming! :)

 

i use camelCase cause i started with Java (well VB 3.0) to be honest but consider Java the first "real" programming language I learned (no offense to all BASIC fans out there)

 

and as far as the var goes i used to alternate between camelCase and this_case but for some reason I got into the habit of avoiding capitalization in DB-related stuff so all my Data objects started having the same variable naming style as my database columns and one thing just led to another :)

 

but yeah corbin why the camelCase hate?  I mean things starting with "camel" are usually kinda sweet; like "camel cigarettes" or "camel toe"  ;D

 

oh and this is a shocker to me:

Some_class;

??? i guess i'm just pedantic (except with capitalization when im posting this late at night) but my classes are 100% strict ProperCase :)

(which hasn't caused any problems yet despite someone mentioning it (and i do my local dev with apache + windows and my remote hosting is more often than note apache + nix) so i guess thanks to the php team for default strtolower() in __autoload()

 

which, this just crossed my mind, is unheard of according to your guys' poll votes ;)

 

strToLower() is so much more legible.... *sigh*  :D

oh and this is a shocker to me:

Some_class;

??? i guess i'm just pedantic (except with capitalization when im posting this late at night) but my classes are 100% strict ProperCase :)

 

When I was first learning OOP, whatever I was reading (some book) mentioned that all classes should start with a capitol letter.  That's how I learned it and it's just habit now, whether it's correct or not. 

I'm sort of... I dunno. Mainly camel case, mainly from when I used Sphere. I used to be an underscore man and all lowercase.

Now though, mainly camel case however I'm not very strict on myself because my scripts can range from including most of the above. But I never, ever start a variable or function name with a capital!

I tend to use camelCase for everything:

 

$someVar;

invokedFunction();

$x = new MyAwesomeClass();

 

Even when I was learning C++ in college, my professors tended to go with camelCase instead of lowercase + underscores.  I find it to be easier to read (and type) than using underscores, in any event.  All those wasted characters....

The way I've always learned it:

Java:

ClassName

functionName()

varName

 

C++:

Class_name

function_name()

var_name

 

PHP:

Path_To_ClassName (most of the time, some exceptions... it's weird)

functionName()

varName()

 

But I agree, the Class_name style has always seemed really weird to me, but it's been around longer than I have, so what do I know?

oh and this is a shocker to me:

Some_class;

??? i guess i'm just pedantic (except with capitalization when im posting this late at night) but my classes are 100% strict ProperCase :)

 

When I was first learning OOP, whatever I was reading (some book) mentioned that all classes should start with a capitol letter.  That's how I learned it and it's just habit now, whether it's correct or not.

 

the weird thing about that is not the capital letter at the start... its the underscore!  :);D

 

Java has the cleanest conventions/syntax rules in my opinion

 

Java

ClassName

functionName()

varName

What are yall's opinions on the prefix-private/protected-attributes/methods-with-underscore convention?

 

private function _whatever()

private $_whatever;

 

etc.

 

I've never really bothered with that.  I mean, it says right in the declaration whether something is private or not.  Further more, I never have any public attributes.  They're all either private or protected.  For my methods, most of mine tend to be public.  Private methods tend to have generic, private-sounding names like init();

 

Just to discuss other languages (I'm bored, hehe), naming conventions in C#, which I'm currently learning, tend to differ in terms of case.  C# has a properties mechanism, whereby you can create getters and setters without declaring a function.  So, typically, class code is littered with things like:

 

private string name;
private string email;

public string Name //note the capital letter
{
   get
   {
      return this.name;
   }
   set
   {
      this.name = value; //value is a keyword, representing the value on the right-side of an assignment statement
   }
}

public string Email
{
   get
   {
      return this.email;
   }
   set
   {
      this.email = value;
   }
}

 

So, if you wanted to use these properties, you'd do the following, assuming the class was named Example:

Example myExample = new Example(); //C# is strongly typed, so you have to specify the variable's datatype

myExample.Name = "Bubba";
myExample.Email = "bubba@bubbagump.com";

Console.WriteLine("My name is {0} and my e-mail address is {1}", myExample.Name, myExample.Email);

What are yall's opinions on the prefix-private/protected-attributes/methods-with-underscore convention?

 

private function _whatever()

private $_whatever;

 

etc.

 

for functions: practically never.

 

for variables: i don't like it (i even posted a thread about it a while ago) but its a good way to distinguish between private and public variables.  but in reality, if you are coding something half-decent NONE of your variables will be public (except maybe static ones) so its kind of contradictory.

 

after reading this i decided to adopt the use underscores to prefix variables that are of strictly internal use.  all variables are protected/private but the ones that i don't allow __get() to return i will try to prefix with an underscore.

 

how's that for a life-changing thread!  :D

 

yeah, I always use "getters and setters" when it comes to class attributes, no matter the language.

 

despite reading about how you should NOT use __get() and __set() magic methods lately, I've resorted to using __get() for attribute retrieval (allows more natural syntax, simulates public visibility) and using multiple setVariable() for setters.  Not sure why I diverged there but it just seems more natural to me:

 

$testclass = new Foo();

// set variable
$testclass->setX('bar');
// get variable
$testvar = $testclass->x;

 

 

I wish I could get myself to stick with one naming convention.  For whatever reason I change back and forth, but most of the time I use underscore instead of camelCase. I try to not use either if possible and keep variables and functions to one word where I can.  I always capitalize classes.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.