Author Topic: variable names  (Read 4670 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: 10658
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: 10658
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: 10658
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

MickD

  • King Gator
  • Posts: 3648
  • (x-in)->[process]->(y-out) ... simples!
Re: variable names
« Reply #15 on: October 15, 2015, 05:16:44 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.

ditto ^^

In regards to var names with type prefix, I agree it's not needed and give the variables a name like fileName, fileCount, fileType etc so it makes sense to what type they are.
I also like to keep methods as small as possible so short var names are less of an issue due to the small scope, if you use small var names in a mammoth method it's easy to get confused.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: variable names
« Reply #16 on: October 15, 2015, 05:43:54 PM »
Quote
I also like to keep methods as small as possible so short var names are less of an issue due to the small scope, if you use small var names in a mammoth method it's easy to get confused.
+1
I usally use single letter var names in lambda expressions.
Speaking English as a French Frog

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: variable names
« Reply #17 on: October 16, 2015, 01:18:43 PM »
I usally use single letter var names in lambda expressions.

This too!  For me it just makes it easier to read.  I don't need to know it's type just knowing its an object of the previous type is enough.
Revit 2019, AMEP 2019 64bit Win 10

CADbloke

  • Bull Frog
  • Posts: 345
  • Crash Test Dummy
Re: variable names
« Reply #18 on: October 18, 2015, 04:08:18 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 generally type out the whole name but all my screens are huge so long lines are not a problem, well not the problem that my short memory is.
I prefer to be verbose and remove as much ambiguity as I can than to outsmart myself with an abbreviation I need to parse or  I forget when I, or someone else, needs to revisit the code in the distant future.

I'm also often excessively verbose with method names, eg. MakeAbsolutePathFromPossibleRelativePathOrDieTrying(string basePath, string possibleRelativePath). You probably didn't just ask yourself "wtf does that do?".

Can anyone give an example of the `namespace collision' problem and why you would need to use abbreviated aliases?
I concur with the examples already given. I find the aliases the Giles showed are also a handy way to use the same code for different platforms. See http://www.theswamp.org/index.php?topic=49974.msg551546#msg551546. While not strictly necessary in most cases, I find they give a good anchor for the multiple contexts you will be looking at the code in.

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: variable names
« Reply #19 on: October 21, 2015, 05:17:43 PM »
NOTE: The following is not a useful contribution to this otherwise valuable thread.

I like using oid for ObjectId.
Then prefix with meaningful helper text.
This way my variables get amusing names.

boid, toid, roid

I am easily amused.
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: variable names
« Reply #20 on: October 21, 2015, 05:23:31 PM »

< ... >

boid, toid, roid



It scares me a little that that made perfect sense to me   :wink:
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.