Thursday, January 29, 2015

Naming conventions: camelCase versus underscore_case ? what are your thoughts about it? - Programmers Stack Exchange

Source>> stackexchange forum

Both!

I do a lot of development in CakePHP, and I use either CamelCase or $underscored_vars, in the following fashion (even outside CakePHP Projects):
  1. filenames — /lowercased_underscored.php Typical, but worth mentioning.
  2. classes — class CamelCase extends ParentObject. Note that, when using CamelCase, the initial character is not lowercase. I find camelCase to look really weird.
  3. variables — $are_variables_underscored === TRUE;
  4. variables that hold instances — $CamelCase = new CamelCase(); //seen it in qb lib but in some languages like ruby everything is a class
  5. array keys — $collection['underscored_keys'];
  6. constants — I think everyone can agree that constants should be ALL_CAPS_AND_UNDERSCORED.
  7. methods — $CamelCase->foo_method_underscored();
  8. static methods — CamelCase::just_like_regular_methods();


Some comment: Absolutely agree it bothers me too

have to agree with you, having the first character be lower case drives me crazy! I hate camelCase with a passion. –  HLGEM Jan 6 '11 at 22:00


For programming languages I've used, like Java, Python, C++, I've adopted a clear format:
  • ClassNamesArePascalCase
  • methodNamesAreCamalCase
  • variable_names_are_underscore
  • CONSTANT_VARIABLES_ARE_CAPITAL_VARIABLES
  • _private_variable_names_are_prefixed_with_an_underscore 
  • _PRIVATE_CONSTANT_VARIABLES_ARE_PREFIXED_WITH_AN_UNDERSCORE


=============================
End Source>> stackexchange forum

Another aspect of naming convention is words order:
1. ModelCallReport or CallReportModel
create_date  or  date_create
In PHP main principle of global function creation in having one prefix for same group of functions
It makes it easier to search them
array_map
array_filter
Class methods:
1. call to action notation - first word is action in lower case then camel case
     collection->findFirst(...)
     obj->getName()

2. No action method naming
     App::log()
     DB::table('users')->lastMonth()->get()
   
     I like when lower case part is either proverb or adjective 
     e.g
         lastMonth
         firstElement
         xmlPath

   In method camel case first lwcase word should be specification 
   and last one the most general one eg (object type)

Functions:
1. Javascript style
      function onSaveHanlder

2. PHP/Ruby style
      send_email()

CSS

1. hyphen/slug style
   class="save-btn"
   id="header-container-section" // logic structure: first root element then child then child-child

2. camel
    name="first_name"

3. Bootstrap style (spesialization frist the most general)
   class="btn btn-sm btn-default"  // logic structure: type then size the color

Some people use camel case for css I dond like it 


Summary: 

In general I like ruby naming convention and ruby approach for naming:
  -readability
  -clarity
  -conciseness (short better then long without lose of clarity)

Not sure about snake case methods: I would use it only for properties instead of getter setter
Private fields should be prefixed with underscore :
    eg:  private $_items; 

No comments:

Post a Comment