group-icon
PHP Programming
Group for all things PHP, Coding Help, Problems and Solutions
104 Members
Join this group to post and comment.
04 Jan 2019

Variables: camelCase or snake_case - What's your preference?

I was wondering what's the better way of naming variables. Of course it's quite difficult to keep a consistent naming pattern; but I'm inclining towards 'longer' variable names over 'shorter' ones. For example,

I'd prefer: $user_name over $userName and $user_id over $userId

The reason is simple, when reading code; the variable with underscores between words look more natural to eyes and it makes it easier to scan through the code - without having the brain to decipher the variable names. 

A lot of code that I write still has $userId; and I'm now switching over to using underscores in variable names. What about you?

Rohit Kumar
Rohit Kumar
Electronic
3mos ago

Hated camel case since the beginning of the time. 

3mos ago

Yeah, it makes the code much readable. There is no other 'obvious' advantage to it, I think. 

Jasmine SA
Jasmine SA
Communications
3mos ago

It's always been snake_case for me. Never been a fan of camel casing. 

DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
3mos ago

Our brain quickly and easily replaces the underscores with spaces and quickly understand the variable in snake_case. When it comes to perception and reading many times our brain is recognizing the shape of the word and the letters in it. When the Words are separated with an Underscore is easy to understand.

I usually prefer snake_case as it helps me and others to understand my code very easily and quicky.

There are few other cases such as kebab-case which is used in list , then MACRO_case is used someone occationally and not for regular use and also Mixed_Case_With_Underscores