Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > General > Best Practices

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-14-2008, 07:16 PM   #1
fx.barrett
Paintball Freak
 
fx.barrett's Avatar
 
Join Date: Jul 2008
Location: Romania, TM
Posts: 467
Send a message via MSN to fx.barrett Send a message via Skype™ to fx.barrett
Default Naming stuff...

I'm really curious to see how you guys name your variables and functions... I've been troubling with this for a while and I've been changing my coding style every 2 months because of this and I had enough, I want to know how others do this:

For example, we have a class that has a private variable in it and I want to define a getter and setter method that will help me manipulate my data ( variable )... the stupid thing about ActionScript is that I can't name the function the same way as my variable... so I hate 2 options: give a different name to the function, or a different name to the variable... but in certain cases it's just stupid... example:

ActionScript Code:
package com.wisebisoft.data {     public class Time     {         private var hour:uint;         private var minute:uint;         private var second:uint;                 public function Time(hour:uint, minute:uint, second:uint)         {             this.hour = hour;             this.minute = minute;             this.second = second;         }                 public function get hour():uint         {             return this.hour;         }                 public function set hour(value:uint):void         {             this.hour = value;         }                 public function get minute():uint         {             return this.minute;         }                 public function set minute(value:uint):void         {             this.minute = value;         }                 public function get second():uint         {             return this.second;         }                 public function set second(value:uint):void         {             this.second = value;         }                 public function clone():Time         {             return new Time(this.hour, this.minute, this.second);         }     } }
Obviouisly, that will throw an error... I see people using underscores for private variables but I really don't like underscores, I'd rather go with using "this." but then, I run into this naming problem... IMHO, it would be weird to name my private var from hour, time and minute, to initHour, initTime, initMinute or whatever name you want just to get rid of this conflict... likewise, it would be really bad to start thinking about names for the functions... and as I said, no underscores...

So, how do you guys name your stuff? How do you handle this annoying naming conflict?

Thanks a ton.
__________________
FLASHFORUM.RO - You must speak Romanian in order to join.
BLOG.WISEBISOFT.COM - Share and Experiment.
fx.barrett is offline   Reply With Quote
Old 11-15-2008, 12:42 AM   #2
fnx
fearless
 
Join Date: Feb 2008
Location: Italy
Posts: 48
Default

That's one of the reasons why I don't use getters and setters :P

Anyway, if you don't like underscores (me neighter) you can do something like that
for private variables:

p_hour, pHour, prHour, hour_p, hour_pr... and so on

I use p obviously to indicate private while the getter and setter stay the same.

Just my 2 cents
__________________
Federico "FNX" Nisoli
Lead Programmer - FNX Games
http://www.fnxgames.com
http://fnxdevbox.blogspot.com
fnx is offline   Reply With Quote
Old 11-15-2008, 12:46 AM   #3
fx.barrett
Paintball Freak
 
fx.barrett's Avatar
 
Join Date: Jul 2008
Location: Romania, TM
Posts: 467
Send a message via MSN to fx.barrett Send a message via Skype™ to fx.barrett
Default

Yeah, that would be an option... but I kind got bored of using that Anyway, thanks for the feedback, appreciate it.
__________________
FLASHFORUM.RO - You must speak Romanian in order to join.
BLOG.WISEBISOFT.COM - Share and Experiment.
fx.barrett is offline   Reply With Quote
Old 11-15-2008, 01:34 AM   #4
pj-co
Site Contributor
 
Join Date: Jun 2008
Location: Brooklyn
Posts: 311
Default

In my opinion the way you name you private variables isn't really all that important. People get too hung up on this detail. Personally I just add an _underscore if, and only if, I have a getter or setter. FlashDevelop does this when it auto-generates getters and setters too. Getters and setters absolutely have a use too. Whether you like to use getSomeProperty() or get someproperty { return _someproperty} is up to you, but I have to disagree with fnx that they should be avoided for not liking underscores.

What's more important (in my opinion) is language convention. I'm not sure if you code in English or not but this is something I do for my code in English.

1) all variables/properties should be named with a noun when possible.

examples:

Code:
width
height
children

2) functions/methods should be named with a verb that describes what the function does.

examples:

Code:
closeAllDoors(); 
resetInitialProperties();
moveLeft();


3) Nothing should be abbreviated that isn't an industry-wide common abbreviation. This is very important for people learning to code and people reading your code who may not speak your language very well. Also, it helps those who may just use a different set of abbreviations for things. It also improves readability for reasons described in #4.

example:

Code:
clickSnd    should be    clickSound
vertLine    should be    verticalLine
moveL       should be    moveLeft

4) Don't use "Hungarian" type notation for variables. It's severely decreases readability because it puts some letters in there that aren't a word. Humans read words by "glancing" at the overall shape of a word then comparing that shape to their memory. Also, variables are already able to be strongly typed and that information is already provided via compile time errors and is auto generated by just about every IDE out there.

examples:

Code:
strFirstName   should be   firstName : String
numCurrScore   should be   currentScore : Number

Ultimately though all this has another, more important effect on your code: it creates code that is it's own documentation. You've probably heard the "self documenting" code mantra before but this is really how it's done. If things are well named, you can come back to old code and know what everything does at a glance.

example:
ActionScript Code:
package {      import com.exmaple.myflashapplication.media.VideoPlayer;      public class ApplicationManager      {                 public function ApplicationManager ()           {                var videoPlayer : VideoPlayer = new VideoPlayer();                videoPlayer.flvURL ="myFLVfile.flv";                videoPlayer.autoPlayWhenLoaded = true;                videoPlayer.loopPlayback= true;                videoPlayer.load();           }      } }

I'll bet you can guess what all of that would do, and where it would go. You probably already recognize all this too because these are guidelines Adobe/Macromedia used in coding Flex and the ActionScript language itself.

If you want to read more, have a look here:

http://opensource.adobe.com/wiki/dis...ng+Conventions

I don't agree with all of what they say but it's a good read for picking up some ideas about situations you may have wondered about and finding ideas for other stuff you may encounter.

Last edited by pj-co; 11-15-2008 at 01:36 AM..
pj-co is offline   Reply With Quote
Old 11-15-2008, 02:10 PM   #5
fx.barrett
Paintball Freak
 
fx.barrett's Avatar
 
Join Date: Jul 2008
Location: Romania, TM
Posts: 467
Send a message via MSN to fx.barrett Send a message via Skype™ to fx.barrett
Default

Thanks for the reply pj-co. I was aware of the naming conventions just that they never seem to cover the situation I was talking about. I too have been using underscores in certain situations when dealing with getters and setter, all I wanted to know if there are any people out there who found a more elegant way of doing this...

I'm a perfectionist and in my opinion, it looks ugly when 80% of the variables are named without underscores and then we have 4 vars that use underscores... it might be confusing for someone who does not understand the way you write code. That's the main reason I started this thread, too see if anyone found an intelligent way of dealing with this problem but at the same time keep the code readable and easy to understand.
__________________
FLASHFORUM.RO - You must speak Romanian in order to join.
BLOG.WISEBISOFT.COM - Share and Experiment.
fx.barrett is offline   Reply With Quote
Old 11-15-2008, 03:43 PM   #6
pj-co
Site Contributor
 
Join Date: Jun 2008
Location: Brooklyn
Posts: 311
Default

Quote:
Originally Posted by fx.barrett View Post
Thanks for the reply pj-co. I was aware of the naming conventions just that they never seem to cover the situation I was talking about. I too have been using underscores in certain situations when dealing with getters and setter, all I wanted to know if there are any people out there who found a more elegant way of doing this...

I'm a perfectionist and in my opinion, it looks ugly when 80% of the variables are named without underscores and then we have 4 vars that use underscores... it might be confusing for someone who does not understand the way you write code. That's the main reason I started this thread, too see if anyone found an intelligent way of dealing with this problem but at the same time keep the code readable and easy to understand.
Yeah I hear ya. I guess in some ways I actually like that. When you see 4 private vars with underscores then you know they have getters/setters. To each their own I guess
pj-co is offline   Reply With Quote
Old 11-15-2008, 03:52 PM   #7
fx.barrett
Paintball Freak
 
fx.barrett's Avatar
 
Join Date: Jul 2008
Location: Romania, TM
Posts: 467
Send a message via MSN to fx.barrett Send a message via Skype™ to fx.barrett
Default

Yeah, but it might not be as obvious to others. anyway, I think I'll change my style ( but this time finally ) and I'll start using underscores for private vars... if that's what people are used to, it's fine with me.
__________________
FLASHFORUM.RO - You must speak Romanian in order to join.
BLOG.WISEBISOFT.COM - Share and Experiment.

Last edited by fx.barrett; 11-15-2008 at 03:54 PM..
fx.barrett is offline   Reply With Quote
Old 11-15-2008, 04:40 PM   #8
pj-co
Site Contributor
 
Join Date: Jun 2008
Location: Brooklyn
Posts: 311
Default

I don't use _them _for _all _private vars, just vars with getters/setters
pj-co is offline   Reply With Quote
Old 11-15-2008, 05:10 PM   #9
fx.barrett
Paintball Freak
 
fx.barrett's Avatar
 
Join Date: Jul 2008
Location: Romania, TM
Posts: 467
Send a message via MSN to fx.barrett Send a message via Skype™ to fx.barrett
Default

Quote:
Originally Posted by pj-co View Post
I don't use _them _for _all _private vars, just vars with getters/setters
Nah, if I'll be using underscores then I'll use them for everything
__________________
FLASHFORUM.RO - You must speak Romanian in order to join.
BLOG.WISEBISOFT.COM - Share and Experiment.
fx.barrett is offline   Reply With Quote
Old 11-16-2008, 12:51 AM   #10
fnx
fearless
 
Join Date: Feb 2008
Location: Italy
Posts: 48
Default

Quote:
Originally Posted by pj-co View Post
... but I have to disagree with fnx that they should be avoided for not liking underscores.
...
In fact I wrote it's ONE of the REASONS

Anyway, I totally agree with you on all conventions, when I have to follow
the "best practice" I use _varName too, but, when I'm not forced to, I avoid
using getters and setters only because they enlarge my source for nothing,
I have plenty of functions that only return or set a value..

When I can code "my way", I set private vars only for vars used by the class
and public everything that must be seen "outside".

Never said that my approach is the right one

Cheers
__________________
Federico "FNX" Nisoli
Lead Programmer - FNX Games
http://www.fnxgames.com
http://fnxdevbox.blogspot.com
fnx is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Naming conflict superpeppe ActionScript 3.0 2 07-05-2007 01:54 PM
removing stuff from SOAP request? gerry81611 ActionScript 2.0 0 01-11-2005 05:16 AM
drop down menu stuff.... nemoNslumberlnd Simple Stuff (Newbies) 3 11-24-2004 09:08 PM
Naming it a 'class package' or not. Stimpson Other Flash General Questions 3 09-16-2004 09:02 PM
my flash doesnt have the lines when drag stuff around... please help out jake Simple Stuff (Newbies) 4 11-09-2001 01:34 AM


All times are GMT. The time now is 10:18 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.