ShawnKY

I was wondering if anyone can point me towards some information on having a single letter prefix before AND after a variable name. Is that a standard practice I have come across this in a number of different programs, yet still a bit unclear why they are used. Any info would be appreciated. Some examples of what I have seen:

aFireball.Value.Source

mWizard = new Sprite....

rotation_angle += .03M;



Re: XNA Framework Single letter prefix before a variable

Jim Perry

The letters before a variable name are usually to indicate scope and data type. If you see something like:

rotation_angle += .03f;

the "f" indicates a float data type value (I'm not sure what "M" is in your example). The MSDN site has a page on naming conventions in .NET.






Re: XNA Framework Single letter prefix before a variable

Johnnylightbulb

This is a touchy topic, I suggest you read the .NET Framework Class Design guidelines on MSDN, and then do whatever you feel like.

This is one of those religious-debate style topics that has no perfect answer.

Here's a link:

http://g.msn.com/9SE/1 http://msdn2.microsoft.com/en-us/library/ms229042.aspx&&DI=6066&IG=6d6851dbf763472d88f868eeb9905420&POS=4&CM=WPU&CE=4&CS=AWP&SR=4

 

My answer, for what it's worth, is: However you name your variables, design your classes, and architect your solutions, be consistant with yourself.





Re: XNA Framework Single letter prefix before a variable

Jim Perry

Johnnylightbulb wrote:
My answer, for what it's worth, is: However you name your variables, design your classes, and architect your solutions, be consistant with yourself.

QFT! I don't follow the "conventions" exactly, I do what makes sense for me. My code is readable by others so I don't see any problem with not following "conventions".






Re: XNA Framework Single letter prefix before a variable

George Clingerman

LOL, well from the examples you posted, you definitely were looking at my code.

I use single letters before my object to indicate their scope. I use the same naming standard consistently (which as was previously stated is really the most important part of naming standards) but I do not follow the typical or recommended naming standards.

I guess it might be a good idea for me to discuss my naming conventions on my site somewhere just to help anyone else that it might confuse.

Sorry about that, but yes, Jim was correct. The single letter in front of my object isn't doing anything special other than helping me (and hopefully others) determine the scope of the object at a glance.







Re: XNA Framework Single letter prefix before a variable

ryan.rogers

Johnnylightbulb wrote:

My answer, for what it's worth, is: However you name your variables, design your classes, and architect your solutions, be consistant with yourself.

And an excellent answer it is.

To take the same advice one step further, when working on a team project, please try to ensure the team members are consistent with one another. On a team project I find that a short but sweet "Coding Standards and Practices" document that every member buys into is worth a hundred times it's weight in gold. I

I totally agree that consistency is far more important than which standard you actually use. One can make religious arguments for one standard over the other and in fact there really is no right answer. It's all about personal preference. The fact that you have the standard and everybody follows it consistently is what is important. Not the standard itself.

Be consistent, and if you ever find yourself in a situation where you have to make some concessions with your teammates in order ot develop a universal convention that everybody can "buy into", then make those concessions! In the end it's really not that important what the standard is; it's far more important that everybody buys into it and follows it. No matter what conventions are used, the teammates will become accustomed to them, and as a result everybody will be able to review and maintain each others code more easily and more productively. Therefore, your teammates will not spend brain cycles during code reviews trying to decipher your conventions, as they will be second nature to them since they match their own. As such they will not only be able to conduct code reviews faster, but also more successfully. Furthermore, new teammates who are brought on board will only have to become familar with one convention, not a dozen. ;-)

Ryan





Re: XNA Framework Single letter prefix before a variable

George Clingerman

Wow, 4 developers ALL agreeing that WHAT naming standards you use isn't as important as USING naming standards and being consistent with them

Is the apocalypse upon us already or has the industry really just shifted it's stance on this I'm impressed everyone! Way to be forward thinking on this.

(interestingly enough, the naming standards that threw the original poster off are the ones I created for my team at work. So I'm so consistent that I use them at work and on my side projects at home. Now that's consistency! )






Re: XNA Framework Single letter prefix before a variable

ShawnKY

George Clingerman wrote:
LOL, well from the examples you posted, you definitely were looking at my code.

I use single letters before my object to indicate their scope. I use the same naming standard consistently (which as was previously stated is really the most important part of naming standards) but I do not follow the typical or recommended naming standards.

I guess it might be a good idea for me to discuss my naming conventions on my site somewhere just to help anyone else that it might confuse.

Sorry about that, but yes, Jim was correct. The single letter in front of my object isn't doing anything special other than helping me (and hopefully others) determine the scope of the object at a glance.


Yes sir, if judging from your signature, you are the author of xnadevelopment.com site, than yes, it is your code, lol. Your site has been very helpful in my crawling stage of XNA and C#, and look forward to seeing more tutorials. I have so far been able to create a "game" based off what I have learned at your site, however, collision detection is what I am stuck on at this point, lol. Keep up the good work over there.

As far as the prefix stuff goes, it may just be simply that I am a noob at coding, with only one semester of college VB under my belt. This venture into C# was to investigate actually being able to use XNA, which, to my surprise, is not that difficult to do, especially with the tutorials at your site.





Re: XNA Framework Single letter prefix before a variable

George Clingerman

Yep, that's me! Glad my tutorials are helping out. I'm currently working on a collision tutorial but with the holiday craziness I wouldn't expect it to go up until after I've recovered.

There aren't a lot of 2D collision tutorials out there right now. Ziggware has some up but they deal primarily with Pixel Perfect collision and they don't give an example of how to do the Pixel Perfect collision when you're comparing collisions of sprites pulled from a sprite sheet (it's similar to the method they use but requires you to use one of the overloads of the GetData() method)

You can do simple box or sphere collisions by using the BoundingBox and BoundingSphere objects built in to XNA.

      BoundingBox aTank = new BoundingBox(new Vector3(12, 15, 0), new Vector3(24, 30, 0));
      BoundingBox aMissle = new BoundingBox(new Vector3(25, 50, 0), new Vector3(50, 100, 0));
 
      if (aTank.Intersects(aMissle) == true)
      {
        //Tank blows up!
      }


So, something like that. Basically you set up a bounding box that corresponds to the current location and size of your sprites. Then you just pass one box into the Intersects method of the other box. It will return true if a collision occurred and false if one didn't.

That's the start of collision detection though so maybe that will help get you pointed in the right direction.

Thanks again for the kind words about my site! It's really appreciated.





Re: XNA Framework Single letter prefix before a variable

Wil Burton

I will put in my $2 (I'm opionated on this matter). I don't like to prefix anything. I always hate the reverse hungarian notation ***.
My reasoning is simple. Readability. If I were going to discuss a piece of code with a co-worker I wouldn't say:

"The vector velocity is added to the vector position each frame."

I would say:

"The velocity is added to the position each frame."

So when reading the code, my mind has to strip out the vec off of the variable named vecVelocity.
The one place I like to have a prefix is class member variables, to show scope. Typically an _ or m_. This still irks a bit, but I'm OK with it.

Oh and variables should be camel notation shipVelocity. Methods should use Pascal notation GetVelocity
It'd be nice if Visual Studio would color code member variables differently than local variables.
I will concede that if you like it, go for it. The biggest thing I think is coming to a consensus if you work in a group.





Re: XNA Framework Single letter prefix before a variable

AndyL

My opinion:

Java and C# have published standards - I see no reason to deviate from them, given the benefit of everyone working the same way. In some ways having these standards is a godsend - it is far easier to agree to all conform to 'the standard' than get 20 developers from different backgrounds to agree a common standard between themselves. It also means you don't have to get used to doing things a different way when you move team or to another company.

And rather like the previous poster, I hate hungarian notation, which is an anachronism left over from the days of C compilers that didn't check types and code editors that were called notepad/vi/emacs (err - anyone remember edlin )! I have to admit I carried on using it long after compilers had improved and IDEs had appeared.





Re: XNA Framework Single letter prefix before a variable

Fluxtah

I am not a fan of hungarian notation escpecially in the day and age of intellisense which gives us more information about members and local scope variables.

I do however name things in a particular way.

private member fields:

private Foo _FooBar;

method parameters:

Foo(string iUseCamelCaseForThese);

local variables:

string iUseCamelCaseForThese;

everything public

IUsePascalCase :)





Re: XNA Framework Single letter prefix before a variable

Jim Perry

"I see no reason to deviate from them, ..."

Other than someone might not like or agree with something in them.

Yes, if you're working in a team you might have to conform, but if it's just you why not just do what works best for you. I follow pretty close to the .NET Framework conventions, but not perfectly.






Re: XNA Framework Single letter prefix before a variable

AndyL

 Jim Perry wrote:

"I see no reason to deviate from them, ..."

Other than someone might not like or agree with something in them.

Yes, if you're working in a team you might have to conform, but if it's just you why not just do what works best for you. I follow pretty close to the .NET Framework conventions, but not perfectly.

I'm just glad I don't have to resolve the coding standard issue for each new team I have to run - I've still got the singed hair to show for the last debate I was involved in 10 years ago.   Oddly enough I don't ever remember having the same problem back when I was an engineer (safety critical electronics and control systems), even if software was involved in the system, as was usually the case.

Andy.





Re: XNA Framework Single letter prefix before a variable

Jim Perry

Almost every team I've been involved with over the last 10 years or so have used a similar enough style that it hasn't been a big deal. Maybe I've just been lucky.