Style survey

For questions and postings not covered by the other forums
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Style survey

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:02 pm

by Anonymous >> Wed, 25 Oct 2006 0:18:18 GMT

Do you prefix every property with self dot?

If so, why?



People I've asked who do this tell me it's just down to individual preference, but (and you can tell I'm one of the people that don't) ...

- it adds a stack of (in my humble option) unnecessary clutter to what could otherwise be concise and easy-to-read methods

- the properties can't be inspected in the debugger without first inspecting self, then finding the property (trying to inspect the expression self.myLuckyRabbitsFoot usually rewards you with "Sorry I can't evaluate that expression".

- you loose out on benefiting from occasional use of self dot to distinguish from some other symbol with the same name, such as a local variable.



(you can probably tell I've been maintaining more of other people's code recently than I would ideally choose to)



So what's self dot all about? Come on ye zealots and commentators alike ...

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Style survey

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:02 pm

by Darrell Duniam >> Wed, 25 Oct 2006 0:35:30 GMT

I use "self." all the time, because:

a) it's immeditatly obvious that you're dealing with a local attibute/method as opposed to a variable;

b) you can use Ctrl+1/2 to find an attribute/method rather than typing the whole name.

Incidentally, I also like to prefix method parameters with a "p", again to distinquishe them from variables, attributes and methods, and I like to suffix a method call with "()" so that it's obvious that it is a method call and not an attribute.

As for adding clutter, I don't agree.

Just my two bob's worth.

cheers,
darrell.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Style survey

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:02 pm

by cnwjhp1 >> Wed, 25 Oct 2006 1:02:58 GMT

I do the opposite of all those things, Darrell, as the excess clutter makes the methods too hard to read. On the rare occasion I want to know any of those things you are making "immediately obvious", I simply use F11. The improved readability of the more concise code more than makes up for the occasional F11. Also, the fact that all those things are optional means that they invariably get left out sometimes, which makes things even more confusing.

Cheers,
John P

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Style survey

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:02 pm

by Brendan Doolan >> Wed, 25 Oct 2006 14:37:23 GMT

I agree with John. Using "self." at every possible opportunity adds very little if anything to the understanding of a piece of code and succeeds mainly in adding extra clutter.

The "Hungarian Notation" for naming variables (prefixing a variable name with one or more characters to indicate its type) is in much the same category. I believe it is completely unnecessary in a strongly typed language but possibly has value in a loosely typed language, though I'm not convinced of that either. I don't use it, and I find it very distracting when reading code written by someone who does.

I would much rather read (and write) code which did neither of the above but used well chosen variable names and was laid out in a clear and concise manner. Using "self." and Hungarian Notation makes this goal much more difficult to achieve.

There are also some interesting comments at http://en.wikipedia.org/wiki/Hungarian_notation in both the article and discussion sections.

Brendan

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Style survey

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:02 pm

by cnwjhp1 >> Wed, 25 Oct 2006 20:42:45 GMT

It seems to me that there is a tendency to use conventions that solve yesterday's problems. A perfect example is prefixing methods with "z" so you can tell it's protected. Then the IDE is changed to provide icons to do that, making the "z" redundant.

Similarly, the variable naming, especially "self." and the type prefixes made sense back when we printed things out to read them, but now it is so easy to F11 that the value of that convention goes away.

One exception to this is references on classes. It is important to know that an attribute points to another object, so using the "my" and "all" prefixes is helpful. I don't use that for method variables though, as the code is obvious enough, and all in a small space (which it wouldn't be if there was a lot of clutter).

Back in 1979 when I started in this business, I experimented with various conventions. Then I read some of the boss' code, and was amazed at how readable it was compared to mine. He used the "keep it simple" method. He didn't add any unnecessary spacing aside from indentation, used meaningful names where that mattered and short names where it doesn't (eg loop counters). That has shaped my outlook ever since.

Cheers,
John P

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Style survey

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:02 pm

by Alan >> Thu, 26 Oct 2006 9:21:33 GMT

I am now working on a system developed using fairly strict naming standards that differ from those I have previously used. Class attributes prefixed with an "a"; references (including collections) prefixed with an "r"; method parameters prefixed with a "p"; locals prefixed with an "l"; methods with a "m" or "z". The remainder of the names were as descriptive as space would allow (collections defined as the plural rather than using "all" e.g. rTowns).

It was a real pain initially but after a while I have come to find it useful (belive it or not) - there is never (well almost never) any ambiguity as to the source of data – I have found that it assists understanding within methods while still being simple.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Style survey

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:02 pm

by Robert >> Fri, 27 Oct 2006 1:53:29 GMT

Hey Alan, they say initial impressions are usually correct - it IS a real pain!
I've had the pleasure of working with those standards, and, okay, having (a)ttributes and (r)eferences together was handy, especially on form classes. I think I started using 'rAll' for collections, after a while. But the "p", "l", "m" and "z" didn't add any value ... often (as John alluded earlier), f11 was required to check that the naming was correct. Since the compiler can't check the convention, then you jsut can't trust it.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Style survey

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:02 pm

by Darrell Duniam >> Thu, 26 Oct 2006 21:34:50 GMT

I don't find that using "self." clutters up code at all, as long as the code is well laid out, but hey, that's just me.

I agree 100% with your comments re Hungarian Notation - it adds nothing to a strongly typed language, and makes code messy and difficult to read.

At the end of the day, it comes down to personal preferences, but I do find it frustrating when numerous people working on the same project don't adhere to a common standard.

cheers,
darrell.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Style survey

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:02 pm

by John J. Johnkerson >> Fri, 27 Oct 2006 3:06:46 GMT
b) you can use Ctrl+1/2 to find an attribute/method rather than typing the whole name.

By the way the self reference is not required for this. If you ctrl+ 1/2 in any method space by default it will supply you with the appropiate schema entities from the currently selected class.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Style survey

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:02 pm

by allistar >> Wed, 25 Oct 2006 6:22:09 GMT

I very rarely use "self.". In my opinion the "self" is obvious and implied, typing it in only makes for more clutter. I tend to prefix parameters passed into "set" methods with an underscore, especially if they have the same name as a property. This makes it obvious what property this parameter will eventually set (after validation is done, of course).

I find prefixing local properties with anything to distinguish the type leads to very messy code. E.g. I've seen code where a local property referencing an object is prefixed with "o". Strings are prefixed with
an "s", Booleans with a "b" etc. In my opinion, property should be obvious enough without the need for this, and it makes the code much easier to read (again in my opinion).

Anyone who's seen my code (in any language) will know that I like long descriptive variable names. It's not unusual to have methods or properties that use up the maximum allowed length. The only exception is "i" and "j" for loop counters. It makes the code easier to understand. In "normal" languages (C, C++ etc) I find that an editor that has clever drop downs to match partially entered words (such as Quanta or KDevelop) remove any issues with having lengthy names. It would be great for the Jade editor to also provide this feature without having to press Ctrl-1 etc.

Allistar [who realises there is no "right" way, just the way we each prefer].


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 36 guests