I'm of the opinion that style conventions should only be relied on to make the nature of a thing clear when naming well and applying the principle of least astonishment isn't quite enough.
So in JavaScript for instance capitalizations helps us with the following:
function plainOlFunction(){...
function ClassLikeConstructor(){...
I consider that reasonable usage of a convention because functions can be used as class-like construct that builds instances or methods. Knowing the intent right off the bat is helpful.
Likewise in Java, C#, and I assume C++ classes get capitalized so we know the difference between:
SomeClass.someStaticMethod();
someInstance.someInstanceMethod();
The reason I'm shy about excess style conventions, however, is that a lot of devs take them as license to be lazy about naming. I've worked under the guy whose parameters were all demarcated by p_
with no name after the p_
exceeding more than 4 characters. And his funcs started with f_
, etc... Reading his code could be Hell. (hard to give the guy a nasty code review though since he also started the company)
So why _somePrivateField
? If you're in the TransactionManager class, it should be pretty obvious that transactions
is going to be a private field where all the transactions are stashed. It might be a local var or a param but usage and context should make that pretty clear.
Why someParam_
? You should really know what the params of the function you're dealing with are unless somebody's gone and put way too many to easily remember (more than 4 is pushing it IMO). So in that case the necessity of param identification is really just an enabler of excess param use where data structures probably should have come into play or there needed to be more than one method.
So this?
function screwTheNextGuy(
p_ew,
p_bleah,
p_yuck,
p_ick,
p_why,
p_RUcRius,
p_ugh,
p_drlord,
p_noob,
p_nomore,
p_coderage
){...
or this?
function makeTheNextGuyHappy( relatedSetOfValues, anotherSetOfValues ){...
Another common convention I disagree with is separating methods from non-methods somehow. Why do we need that when spoken language allows us to distinguish noun from verb? Do we need a hint to know if it's public or private or should that again be fairly obvious from the perspective of anything you wouldn't need to expose being private?
More specifically on the subject of underscores, important things to check for are the conventions of whatever language you're in because a lot exist. Single underscore prefix is popular for private vars but also for an include-like file and the same often applies to an object representing that file. I would generally never touch double-underscores for common usage since they represent magic vars, constants, reserved names, do-not-touch thingies and god knows what else typically. Mostly I use them as separators to indicate a style convention in the first place. For instance, that I mean $_jQueryObject
rather than $phpDevWritingJavaScriptVarNames
. So yes like that silly p_
but with a real name on the right of it.
The post-param underscore is definitely kind of odd. We read left to right so that would actually be fairly easy to miss on a quick scan. I wouldn't use that if I thought a style convention was needed.
As a general rule I think it's best to use style conventions sparingly and to make sure that when you establish conventions that you're thinking about the next guy as much as yourself because nobody should have to remember what p_fg
and p_deo
were supposed to be at line 500 of an enormous function with a dozen parameters. That they're obviously parameters isn't half as helpful as getting an idea of what values they hold.