Author Topic: variable names  (Read 4701 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
variable names
« on: October 15, 2015, 03:40:59 AM »

Most of us who have been reading C# code for a while get to recognize and accept certain variable names to represent objects from the AutoCAD API.

For instance, this sort of template would be fairly easily grocked.

Code - C#: [Select]
  1. using System;
  2. using System.IO;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using MgdAcApplication = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  5.  
  6. namespace NetAddinCS9 {
  7.     public class CmdGroup1 {
  8.         public void VariablesUsed() {
  9.             var doc = MgdAcApplication.DocumentManager.MdiActiveDocument;
  10.  
  11.             // var ed = doc.Editor;
  12.             // OR if doc is not used ??
  13.             var ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
  14.  
  15.             var db = HostApplicationServices.WorkingDatabase;
  16.  
  17.             using(var tr = db.TransactionManager.StartOpenCloseTransaction()) {
  18.                 //TODO: add/access stuff in the Database
  19.  
  20.                 tr.Commit();
  21.             }
  22.  
  23.             // working with an external Database
  24.             try {
  25.                 using(var extDb = new Database(false, true)) {
  26.                     extDb.ReadDwgFile("D:\\temp\\tester.dwg", FileShare.ReadWrite, false, "");
  27.                     using(var extTr = extDb.TransactionManager.StartTransaction()) {
  28.                         //TODO: add/access stuff in the external Database
  29.  
  30.                         extTr.Commit();
  31.                     }
  32.                 }
  33.             }
  34.             catch(System.Exception ex) {
  35.                 ed.WriteMessage(Environment.NewLine + ex.ToString());
  36.             }
  37.         }
  38.     }
  39. }
  40.  


Without being too pedantic or anal about it, I like to be consistent with naming variables.
... there is a certain comfort associated with a familiar system.

With that in mind, I'm wondering what you guys like to use for object names.

Some that come to mind are.

doc Document
db   Database
ed   Editor
tr    Transaction

extDb  Database external
extTr   Transaction external

docLock

id   ObjectId
xxxxxId  whatever ObjectId
ent  Entity

bt   BlockTable
btr BlockTableRecord

br  BlockReference

cs  CustomizationSection
ws Workspace
tab RibbonTab

rb ResultBuffer

peo  PromptEntityOptions
per  PromptEntityResult
pso PromptSelectionOptions  // these can be confusing and not obvious sometimes
psr  PromptSelectionResult

ucs  Matrix3d : ed.CurrentUserCoordinateSystem
ucsTbl  UcsTable

vt    ViewportTable
vtr   ViewPortTableRecord

lt   LayerTable
ltr  LayerTableRecord



Anyone got any favorites they like to use ??
... or alternatives ??

Regards,
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: variable names
« Reply #1 on: October 15, 2015, 07:20:51 AM »
Hi

I use nearly the same names plus some aliasses (mainly to avoid namespace.class conflicts):
Code - C#: [Select]
  1. using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
  2. using AcDb = Autodesk.AutoCAD.DatabaseServices;
  3. using AcGe = Autodesk.AutoCAD.Geometry;
  4. using AcGi = Autodesk.AutoCAD.GraphicsInterface;
  5. using AcRx = Autodesk.AutoCAD.Runtime;
Speaking English as a French Frog

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: variable names
« Reply #2 on: October 15, 2015, 09:00:58 AM »
I usually just type out the entire name to avoid any confusion and to make the code more readable.  So Transaction would become transaction, ObjectId is objectId, etc.  I realize that this is subjective though and a lot of people do not like this style.  I do use abbreviated aliases to avoid namespace collisions however.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Re: variable names
« Reply #3 on: October 15, 2015, 09:33:13 AM »
I usually just type out the entire name to avoid any confusion and to make the code more readable.  So Transaction would become transaction, ObjectId is objectId, etc.  I realize that this is subjective though and a lot of people do not like this style.  I do use abbreviated aliases to avoid namespace collisions however.

I know I'm just now getting into .net stuff (still extremely green) but this is the approach I take in my Cpp code so I imagine I will want to do the same thing.

Can anyone give an example of the `namespace collision' problem and why you would need to use abbreviated aliases?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: variable names
« Reply #4 on: October 15, 2015, 09:55:16 AM »
I use the longer downcased class name for variables for stuff that I don't use each day.
I have noticed, when reading or discussing code, that I verbalize the variable as the longer full name.

The only namespace clash I recall is Application. I have used namespace aliases in the past but not so much recently.

I went through a stage where I used the downcased class name for almost everything, but frugality won out.
The maxim 'if in doubt, spell it out' still stands for me though.

As Keith mentioned, this is subjective : I'm not advocating any particular method ... just trying to determine current usage.




 
« Last Edit: October 15, 2015, 10:00:45 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: variable names
« Reply #5 on: October 15, 2015, 10:06:16 AM »
Quote

Grok /ˈɡrɒk/ is a word coined by Robert A. Heinlein for his 1961 science-fiction novel, Stranger in a Strange Land, where it is defined as follows: Grok means to understand so thoroughly that the observer becomes a part of the observed—to merge, blend, intermarry, lose identity in group experience.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: variable names
« Reply #6 on: October 15, 2015, 10:14:05 AM »
Can anyone give an example of the `namespace collision' problem and why you would need to use abbreviated aliases?


As Kerry mentioned, the Application class is available in both System.Windows and Autodesk.AutoCAD.ApplicationServices namespaces.  So if you have a using statement in your code for both of those namespaces and try to use the Application class, the compiler will not know which one to use.  The solution is to either use the fully qualified namespace name, i.e. Autodesk.AutoCAD.ApplicationServices.Application or create a namespace alias such as Using AcApp = Autodesk.AutoCAD.ApplicationServices.Application; Then when you need to use the autocad application class you can just use AcApp.DocumentManager.MdiActiveDocument.  It is just an abbreviated form of the fully qualified namespace.


http://stackoverflow.com/questions/505262/c-sharp-namespace-alias-whats-the-point
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Re: variable names
« Reply #7 on: October 15, 2015, 10:16:32 AM »
Ah, I understand.

Ugh, don't get me started on downcasing; I can't decide from one day to the other (on variables not classes).

One thing that bothers me a little is the namespace indent but I suppose I'll get used to it.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: variable names
« Reply #8 on: October 15, 2015, 10:19:11 AM »
Hence the phrase "I grok Spock".  Yeah, space hippies!   :-D

I try to find a happy medium between understanding and brevity, but usually lean towards the former since autocomplete takes the work out of using longer names.  More verbose naming also reduces the need for commenting.

I frequently use namespace aliases, almost identical to what gile posted.


If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: variable names
« Reply #9 on: October 15, 2015, 10:38:26 AM »
From about 10 years ago :

I recall that Glenn Ryan used a p prefix for his commonly used class variables
eg pDb, etc

@Jonn,
Here's an example that shows alias namespace usage and Glenn's naming
http://www.theswamp.org/index.php?topic=7745.msg98269#msg98269

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Re: variable names
« Reply #10 on: October 15, 2015, 11:40:19 AM »
From about 10 years ago :

I recall that Glenn Ryan used a p prefix for his commonly used class variables
eg pDb, etc

@Jonn,
Here's an example that shows alias namespace usage and Glenn's naming
http://www.theswamp.org/index.php?topic=7745.msg98269#msg98269

`p' I would think denotes "pointer" (we use the same thing in Cpp but I don't think C# has "pointers" like I'm used to).
For example, here is some code I am currently looking at (my own project):
Code - C++: [Select]
  1. class TTextInBuffer {
  2.     protected:
  3.         std::fstream file;                  // input text file
  4.         char *const pFileName;              // ptr to the file name
  5.         char text[ maxInputBufferSize ];    // input text buffer
  6.         char *pChar;                        // ptr to the current char
  7.                                             //  in the text buffer.
  8. ...

That's a pretty good idea even if it isn't a "true pointer" (Glenn be smart).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: variable names
« Reply #11 on: October 15, 2015, 11:47:16 AM »

Yep, Glenn came from an arx background
... and the p represented pointer.

BUT I personally think the usage is redundant in C# .. we 'know' these particular variables represent Classes ( the 'knowledge' is from common usage).

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: variable names
« Reply #12 on: October 15, 2015, 01:29:46 PM »
My default is to spell it out with variables as camel casing, constants as all caps, and everything else as Pascal casing.  The exception is everyday Autodesk variables, i.e. doc, db, bt, ms, tr, etc.

Prefixes like p, m, and _ are a C++ thing and I leave them there.
« Last Edit: October 15, 2015, 01:34:22 PM by MexicanCustard »
Revit 2019, AMEP 2019 64bit Win 10

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: variable names
« Reply #13 on: October 15, 2015, 01:39:42 PM »
Personal quirk, but one thing I find annoying is prefixing everything with its type ie. intCounter, strName, dblDistance.  When its well named and used rationally it should be rather obvious a counter will be an int not a string, a name will be a string not a double, or a distance will be a double not a bool.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: variable names
« Reply #14 on: October 15, 2015, 04:45:38 PM »
@Se7en
Quote
Can anyone give an example of the `namespace collision' problem and why you would need to use abbreviated aliases?
Both Autodesk.AutoCAD.DatabaseServices and Autodesk.AutoCAD.GraphicInterface namespaces have a Polyline class.
Speaking English as a French Frog