`
renzhelife
  • 浏览: 668796 次
文章分类
社区版块
存档分类
最新评论

是雏还是鹰——编程规范之变量命名

 
阅读更多

看过很多人找工作的经历,从他们的言语中感触最深的几句话就是“应届生不好找工作”,“没有工作经验根本没戏”如此云云。那么为什么应届生不好找工作,为什么用人单位想要有工作经验的人?

简单说来就是,用人单位希望它花钱雇的人可以马上为他创造效益,他们要的是“鹰”不是“雏”!

这么说来其实并不是应届生不好找工作,也不是没经验不好找工作。而是你不能直接为企业创造效益所以不好找到工作,这才是根本。那么怎样让用人单位认为你可以为它创造效益,或者说怎样让用人单位认为你是专业人士而不是个新手?

当然了区别专业与业余的标识除了基础知识以及专业技能之外,还有一个最基本的也是很多人最容易忽视的地方——编码规范。基础知识以及专业技能先不说,因为那些是需要长时间的学习,需要坚强的精神和不拔的毅力,绝不是一朝一夕可以解决的问题。而今天我们从最基本的最容易的也是很多人最容易忽视的编码规范说起。

从一个人的代码中可以大致断定这个人是“雏”还是“鹰”,是专业人士还是刚毕业的学生。

先看看下面的代码:

   1:  Sub ExportExcel(ByVal a As DataGridView, ByVal b As Boolean)
   2:          Try
   3:              Dim n, i, j, row, col As Integer
   4:              Dim e As Excel.Application = New Excel.Application
   5:              e.Application.Workbooks.Add(True)
   6:              col = 1
   7:              For n = 0 To a.ColumnCount - 1
   8:                  If b Then
   9:                      If a.Columns(n).Visible Then
  10:                          e.Cells(1, col) = a.Columns(n).HeaderText
  11:                          col = col + 1
  12:                      End If
  13:                  Else
  14:                      e.Cells(1, n + 1) = a.Columns(n).HeaderText
  15:                  End If
  16:              Next
  17:              row = 2
  18:              For i = 0 To a.RowCount - 1
  19:                  col = 1
  20:                  For j = 0 To a.ColumnCount - 1
  21:                      If b Then
  22:                          If a.Columns(j).Visible Then
  23:                              e.Cells(i + 2, col) = a.Rows(i).Cells(j).Value
  24:                              col = col + 1
  25:                          End If
  26:                      Else
  27:                          e.Cells(i + 2, j + 1) = a.Rows(i).Cells(j).Value
  28:                      End If
  29:                  Next
  30:              Next
  31:              e.Visible = True
  32:          Catch ex As Exception
  33:              Throw ex
  34:          End Try
  35:  End Sub

上面是一个并不复杂的过程,相信没几个人能耐着性子看完,更别说看懂了。如果你在面试的时候写出这样的代码,那么可以放心的告诉自己“对不起,肯定被Pass了!”,因为从代码上用人单位就认为你是一个新手,或者说就认为你不能马上为企业创造效益。

仅从变量命名方面看就知道写这段代码的人是个“雏”!用字母命名变量,什么ab等等。这是谁干的事?这是学生干的事,肯定不是专业人士干的事!这样的命名给后来的代码维护增加的工作量,前面刚定义的a为一个string类型的变量。阅读这段代码,30行之后鬼才知道你的a是什么类型,干嘛用的。如果整个工程中都是这样的代码,那么可以想象维护起来该是一件多么痛苦的事!用人单位为什么要用这种“雏”,用他只能给自己的公司带来不必要的麻烦。

正确的变量命名规范:

l 采用匈牙利标记法标记数据类型或者控件类型

l 为变量赋予表意性强的名字

例如:intIndexdgvStudentInfo,像strTemp这样的变量名是极其令人恶心的。

l 尽量避免对变量名的缩写,如果必须缩写一定保证缩写后不影响对变量的理解。

l 变量名中混合使用大小写

例如:

-Dim strFIRSTERNAME as stringdim strlastname as string都是不规范的

l 使用统一的量词并放在结尾

例如:

-Dim strFirstCustomer As String

-Dim strLastCustomer As String

-Dim strPreviousCustomer As String

上面的变量定义就没有下面的变量定义更加容易理解与搜索

-Dim strCustomerFirst As String

-Dim strCustomerLast As String

-Dim strCustomerPrevious As String

我们可以通过以上几条原则来规范自己的代码使自己代码的可读性增强。

如下所示

<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>
   1:  Sub ExportExcel(ByVal dgvSouse As DataGridView, ByVal blnIsOnlyVisible As Boolean)
   2:          Try
   3:              Dim IntColumnNumHead As Integer
   4:              Dim IntRowNumText As Integer
   5:              Dim IntColumnNumText As Integer
   6:              Dim IntCurrentRow As Integer
   7:              Dim IntCurrentCol As Integer
   8:              Dim Excel As Excel.Application = New Excel.Application
   9:              Excel.Application.Workbooks.Add(True)
  10:              IntCurrentCol = 1 
  11:              For IntColumnNumHead = 0 To dgvSouse.ColumnCount - 1
  12:                  If blnIsOnlyVisible Then                    
  13:                      If dgvSouse.Columns(IntColumnNumHead).Visible Then                        
  14:                          Excel.Cells(1, IntCurrentCol) = dgvSouse.Columns(IntColumnNumHead).HeaderText
  15:                          IntCurrentCol = IntCurrentCol + 1
  16:                      End If
  17:                  Else                   
  18:                      Excel.Cells(1, IntColumnNumHead + 1) = dgvSouse.Columns(IntColumnNumHead).HeaderText
  19:                  End If
  20:              Next           
  21:              IntCurrentRow = 2 
  22:              For IntRowNumText = 0 To dgvSouse.RowCount - 1
  23:                  IntCurrentCol = 1
  24:                  For IntColumnNumText = 0 To dgvSouse.ColumnCount - 1
  25:                      If blnIsOnlyVisible Then                       
  26:                          If dgvSouse.Columns(IntColumnNumText).Visible Then                           
  27:                              Excel.Cells(IntRowNumText + 2, IntCurrentCol) = dgvSouse.Rows(IntRowNumText).Cells(IntColumnNumText).Value
  28:                              IntCurrentCol = IntCurrentCol + 1
  29:                          End If
  30:                      Else                        
  31:                          Excel.Cells(IntRowNumText + 2, IntColumnNumText + 1) = dgvSouse.Rows(IntRowNumText).Cells(IntColumnNumText).Value
  32:                      End If
  33:                  Next
  34:              Next
  35:              Excel.Visible = True 
  36:          Catch ex As Exception
  37:              Throw ex
  38:          End Try
  39:  End Sub
<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>

但是仅仅有了符合规范的变量还是不够的。一段可读性强的代码还应注意注释的编写。关于代码注释的编写,咱们下回再说。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics