乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 268|回复: 13

variable names

[复制链接]

116

主题

996

帖子

9

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1466
发表于 2015-10-15 03:40:59 | 显示全部楼层 |阅读模式
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.
[ol]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());
            }
        }
    }
}
[/ol]
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,
回复

使用道具 举报

15

主题

687

帖子

169

银币

中流砥柱

Rank: 25

铜币
582
发表于 2015-10-15 07:20:51 | 显示全部楼层
Hi
I use nearly the same names plus some aliasses (mainly to avoid namespace.class conflicts):
[ol]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;[/ol]
回复

使用道具 举报

61

主题

792

帖子

35

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1015
发表于 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.
回复

使用道具 举报

116

主题

996

帖子

9

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1466
发表于 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?
回复

使用道具 举报

116

主题

996

帖子

9

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1466
发表于 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.
回复

使用道具 举报

16

主题

506

帖子

6

银币

中流砥柱

Rank: 25

铜币
570
发表于 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
回复

使用道具 举报

116

主题

996

帖子

9

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1466
发表于 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.
回复

使用道具 举报

61

主题

792

帖子

35

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1015
发表于 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.
回复

使用道具 举报

116

主题

996

帖子

9

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1466
发表于 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
回复

使用道具 举报

24

主题

204

帖子

6

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
300
发表于 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):
[ol]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.
...[/ol]
That's a pretty good idea even if it isn't a "true pointer" (Glenn be smart).
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-2-4 22:09 , Processed in 0.236249 second(s), 72 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表