Kerry 发表于 2015-10-15 03:40:59

variable names

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.
using System;
using System.IO;
using Autodesk.AutoCAD.DatabaseServices;
using MgdAcApplication = Autodesk.AutoCAD.ApplicationServices.Core.Application;

namespace NetAddinCS9 {
    public class CmdGroup1 {
      public void VariablesUsed() {
            var doc = MgdAcApplication.DocumentManager.MdiActiveDocument;

            // var ed = doc.Editor;
            // OR if doc is not used ??
            var ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;

            var db = HostApplicationServices.WorkingDatabase;

            using(var tr = db.TransactionManager.StartOpenCloseTransaction()) {
                //TODO: add/access stuff in the Database

                tr.Commit();
            }

            // working with an external Database
            try {
                using(var extDb = new Database(false, true)) {
                  extDb.ReadDwgFile("D:\\temp\\tester.dwg", FileShare.ReadWrite, false, "");
                  using(var extTr = extDb.TransactionManager.StartTransaction()) {
                        //TODO: add/access stuff in the external Database

                        extTr.Commit();
                  }
                }
            }
            catch(System.Exception ex) {
                ed.WriteMessage(Environment.NewLine + ex.ToString());
            }
      }
    }
}

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
extDbDatabase external
extTr   Transaction external
docLock
id   ObjectId
xxxxxIdwhatever ObjectId
entEntity
bt   BlockTable
btr BlockTableRecord
brBlockReference
csCustomizationSection
ws Workspace
tab RibbonTab
rb ResultBuffer
peoPromptEntityOptions
perPromptEntityResult
pso PromptSelectionOptions// these can be confusing and not obvious sometimes
psrPromptSelectionResult
ucsMatrix3d : ed.CurrentUserCoordinateSystem
ucsTblUcsTable
vt    ViewportTable
vtr   ViewPortTableRecord
lt   LayerTable
ltrLayerTableRecord
Anyone got any favorites they like to use ??
... or alternatives ??
Regards,

gile 发表于 2015-10-15 07:20:51

Hi
I use nearly the same names plus some aliasses (mainly to avoid namespace.class conflicts):
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcGe = Autodesk.AutoCAD.Geometry;
using AcGi = Autodesk.AutoCAD.GraphicsInterface;
using AcRx = Autodesk.AutoCAD.Runtime;

JohnK 发表于 2015-10-15 09:00:58

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.

Kerry 发表于 2015-10-15 09:33:13


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?

Kerry 发表于 2015-10-15 09:55:16

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.

dgorsman 发表于 2015-10-15 10:14:05


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

Kerry 发表于 2015-10-15 10:16:32

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.

JohnK 发表于 2015-10-15 10:19:11

Hence the phrase "I grok Spock".Yeah, space hippies!   
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.

Kerry 发表于 2015-10-15 10:38:26

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

MexicanCustard 发表于 2015-10-15 11:40:19


`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):
class TTextInBuffer {
    protected:
      std::fstream file;                  // input text file
      char *const pFileName;            // ptr to the file name
      char text[ maxInputBufferSize ];    // input text buffer
      char *pChar;                        // ptr to the current char
                                          //in the text buffer.
...
That's a pretty good idea even if it isn't a "true pointer" (Glenn be smart).
页: [1] 2
查看完整版本: variable names