Jump to content

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


alexweber15

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

Most functions will require some type of verb to be more descriptive, but I guess I was referring more to variables when I can keep them simple.  Typically, it is better to more descriptive, I just don't like using either method which is probably why I can't stick to one or the other.  I'm only hurting myself in the long run and need to pick one method and stick with it. 

Link to comment
Share on other sites

  • Replies 51
  • Created
  • Last Reply

Top Posters In This Topic

One of the things that annoys me about PHP in and of itself is that there's no real logic behind the way its own built-in functions are named.  It's very annoying to have errors crop up because, oops, the function you're trying to invoke doesn't have an underscore in its name.

 

Example, with two commonly used functions:

 

htmlentities();

mysql_query();

 

Either put an underscore in the first, or switch to camelCase, or do something to make it uniform.

Link to comment
Share on other sites

One of the things that annoys me about PHP in and of itself is that there's no real logic behind the way its own built-in functions are named.  It's very annoying to have errors crop up because, oops, the function you're trying to invoke doesn't have an underscore in its name.

 

Example, with two commonly used functions:

 

htmlentities();

mysql_query();

 

Either put an underscore in the first, or switch to camelCase, or do something to make it uniform.

 

That's one of the reasons I was wondering if there was some standard for naming in PHP.  I really haven't done any research but from the built in functions it doesn't point to any clear standard.

Link to comment
Share on other sites

Yeah, there's no clear standard as far as internal function naming goes... you either get used to knowing the function names (eventually you will if you program in php enough) or you can keep a reference close by.

 

I DO recall there being an offshoot of php, people made their own php distribution with the functions named in a more consistent manner. I wish I could remember its name.

 

http://framework.zend.com/manual/en/coding-standard.html

^ The coding standards I attempt to stick with for php ^

Link to comment
Share on other sites

Yeah zend is a good one.  I guess it would depend what you're working with to.  For example os-commerce, wordpress, etc. I think have their standards but then again they have to use the php functions so idk where I'm going with this....

Link to comment
Share on other sites

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?

 

 

I don't know.  Lots of little, strange things annoy me.

 

 

I even have a bias towards code written with camelCase.  It's strange.  I think it's the name "camelCase" >.<.  And it just looks weird to me.  Why doesn't it match?  What the hell?  aB doesn't make sense.  Why not AB (where A and B are first letters) or ab.  Guess it's more of a consistency thing for me.

Link to comment
Share on other sites

Two capitals have the same effect.

For me, I use camelCase for variables

and

CamelCase for classes, so it helps distinguish them.  Not like you can't tell by the syntax (->, :: ) but I'm just anal that way LOL.

 

A pet peeve of mine is that it seems the conventional wisdom is to start a class name with a capital, but I rarely see people use a capital when they instantiate it.

 

like:

$db = new DB();

Link to comment
Share on other sites

Two capitals have the same effect.

For me, I use camelCase for variables

and

CamelCase for classes, so it helps distinguish them.  Not like you can't tell by the syntax (->, :: ) but I'm just anal that way LOL.

 

A pet peeve of mine is that it seems the conventional wisdom is to start a class name with a capital, but I rarely see people use a capital when they instantiate it.

 

like:

$db = new DB();

 

thats odd its always been kind of an axiom for me.  ClassNames always in ProperCase with no other characters.  Always!

 

about your example btw: just to clarify: do you mean you think its weird that $db = new DB(); or are you saying that's how it should be? 

 

cause that's how it should be! ;)

Link to comment
Share on other sites

I would do

<?php
class DbFactory {
private static $instance = false;

private function __construct(){}
private function __clone(){}

public static function getInstance() {
	if(!$instance) {
		self::$instance = new DbFactory();
	}

	return self::$instance;
}

public function get($className) {
	try {
		$file = 'classes/' . $className . '.php';
		if(file_exists($file)) {
			require_once($file);

			if(!class_exists($className)) {
				throw new Exception("$className Doesn't Exist!");
			}
		}
		else {
			throw new Exception("'$file' Doesn't Exist!");
		}

		$class = new $className();
		if(!is_a($class, 'Db')) {
			throw new Exception("$className is not a Db class!");
		}
	}
	catch(Exception $e) {
		echo 'Uh oh! : ' . $e->getMessage();
	}

	return $class;
}
}

$dbFactory = DbFactory::getInstance();
$db = $dbFactory->get('Db');
?>

 

Don't you know programmers always make the simplest things complicated?!

 

http://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer.html

Link to comment
Share on other sites

I use camelCase because I learned that way in the process, and it seems a lot of people use it.

 

Like I said, I learned Java first which is why I'm accustomed to naming variables, functions etc. the way I do.  I guess it has a lot to do with what language you learn first because most of them have their conventional ways of naming.  If you learn PHP first then I guess you just make up your own...

Link to comment
Share on other sites

I think $DB = new DB;  would bother me.

I would do $db=new Db();

 

agreed on both counts!

 

I use camelCase because I learned that way in the process, and it seems a lot of people use it.

 

Like I said, I learned Java first which is why I'm accustomed to naming variables, functions etc. the way I do.  I guess it has a lot to do with what language you learn first because most of them have their conventional ways of naming.  If you learn PHP first then I guess you just make up your own...

 

and again! :P

Was introduced to OOP in Java and later C (still decent and command-line Java (havent touched an IDE in ages) and the only things I remeber about C are:

1) pointers SUCKED (in retrospect i don't see why such a big deal, but at the time there wasn't a single person in my class at uni who wouldn't cry inside thinking about them.... lol)

2) implemented enums in a cool/useful way

3) i hated how anal about header files (".h") it was... if anyone hasn't seen any C (dont know if its the same in C++) its basically a class interface (im pretty sure obligatory too) and its strictly 1 class per file (good practice) but inconvenient for quick tests.

4) uber stronhly typed: (iirc) return values and input hints obligatory for all functions, all functions obligatorily have to at least "return;", lots of type casting and conversions, strings and arrays are Objects (a string is an array of "chars" (basically an ASCII code) and an array is well, an array ad in fact i don't even think that keyword existed, it was implied when using []

5) vaguest error messages EVER.  debugging a missing semi-colon could take hours...lol

 

 

Anyway enough rambling... but I guess my background's to blame for some of the proposed language features I've been discussing recently :)

Link to comment
Share on other sites

I'm curious as to how you implemented OOP in C.  Did you just simulate it with structs?  Because, from what I remember, C doesn't have objects.  And you're right, pointers in C are a nightmare.  It would've been nice if they added some sort of pass by/return by reference capabilities to the language.

 

I agree with you regarding PHP's lack of return type in function signatures.  Given that one of the cornerstones of OOP is to program to an interface, it's strange that this aspect of a class' interface isn't available at a glance.  Consider the following base class:

 

abstract class BaseExample
{
   public abstract function getId();
}

 

Obviously getId returns an id.  That much is made clear by the method name.  But what is an id in this context?  Is it an auto-incremented integer coming from the database?  Is it a string containing both letters and numbers?  Is it, itself, an object of some sort, perhaps containing a reference to a collection of specific databases?

 

Documentation obviously helps, but enforcing a return type seems like it would reinforce the encapsulation of data as it adds another system-level guideline on how to implement a class.

 

I dunno...I just think it helps clarify things as it places all of the important info right in the function signature.

Link to comment
Share on other sites

camel case is best for oop future of  the apps but legecey still remains of undersores and lowercase messes,

 

I was going to guess what he meant but then I really thought about it and don't have the faintest idea.

Link to comment
Share on other sites

there are loads of huge old apps that arnt OOP and have hade classes introduced to them, this means you have to follow the naming conventions used in the proceedural style of teh app in the new oop feture of the app, i admit cmel case is the best foor oop, but i have applications that use camel case but are mostly made up from underscored lowercasee variables, the difference iwth web apps as apposed to traditional system proograms is that web apps have alot more languages going on and each one is continuously evolving, look u can have classes in JS but u gota use protoype to get them so people have huge libraries and frameworks should they change there whole naming convention just cos somone released prototype ? no they will adapt 

Link to comment
Share on other sites

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.