2013-12-03 26 views
5

很久以前,我快速瀏覽了一段代碼(PERL ... urp),其中一位管理人員正在編寫代碼,並被他在代碼中使用的橫幅/標題系統驚呆了。我沒有機會詳細研究他的代碼,但只是從屏幕上的橫幅評論中,我可以很容易地知道代碼是做什麼的,即使是從很遠的地方。評論標題/標題最佳做法/示例?

不幸的是,很久以前,我們當時的談話並不適合我說:「是的,忘記我們的網絡新創公司下馬,我可以記錄您的編碼風格嗎?」

多年以後,我還沒有達到高級評論風格,其清晰度與我當天看到的(可能現在是神話般的)代碼相同。

當我說「橫幅」時,我指的是許多編碼人員用來在代碼中創建更高級別的版本的高級塊分區/標題。他們通常由簡單的ASCII破折號,斜線,等號等。在我目前的日常使用的語言,一個代碼旗幟/頭層次可能是:

# ======================================================== 
# = Header 1 
# ======================================================== 

# -------------------------------------------------------- 
# - Header 2 
#--------------------------------------------------------- 

# == Header 3 ============================================ 

# -- Header 4 -------------------------------------------- 

# Header 5 

和所有常見的變化。

雖然我的搜索沒有什麼值得注意的,當然在某個地方在網絡上有人試圖收集這些例子,並以系統的方式呈現它們?

有人能指向橫幅評論樣式「系統」,他們發現有用嗎?我並沒有想到「哦,我喜歡那些使用星號的人」,但更多的是風格的總體策略,這使得高層次的代碼構建變得快速易懂,而且易於理解。顯然,從示例中選擇一個首選系統比比較描述更容易。

注:我對內容的評論本身不感興趣,但評論上使用的「flair」提供了整體代碼內容和組織的明確指示。

回答

1

我發現驗證評論風格的最佳方式之一是使用代碼文檔工具,如doxygen,其他人列表here,然後查看輸出是什麼樣 - 輸出更清晰評論越好。

我會說最大的一點是一致性和清晰的排名指示,其次是完整性和簡潔性,即一旦你看了一個,你應該知道其他人會是什麼樣子,你是多麼重要看着是。 這迫使你有一個好的設計,因爲沒有它你不知道有多重要的東西。你需要應該存在它應該是足夠短一目瞭然採取

接下來的所有信息 - 但是滿足這迫使你改變你的編碼風格使物體/代碼不是太大,是有名的,沒有太多的參數等,所有的東西,如棉絨工具試圖教我們

對於python代碼,PEP-257提供了很多有用的指導方針和一些示例。

在我的機器上快速查找一些「良好」的代碼Andrea Gavana's Aquabutton.py我已經在下面包含了一段代碼,但您可以看到完整代碼here - 我不得不說這是Andrea的第一個模塊,我打開了,在字母表的早期,但我相信任何事情都會完成。

# --------------------------------------------------------------------------------- # 
# AQUABUTTON wxPython IMPLEMENTATION 
# 
# Andrea Gavana, @ 07 October 2008 
# Latest Revision: 24 Nov 2011, 22.00 GMT 
# 
# 
# TODO List 
# 
# 1) Anything to do? 
# 
# 
# For all kind of problems, requests of enhancements and bug reports, please 
# write to me at: 
# 
# [email protected] 
# [email protected] 
# 
# Or, obviously, to the wxPython mailing list!!! 
# 
# 
# End Of Comments 
# --------------------------------------------------------------------------------- # 

""" 
:class:`AquaButton` is another custom-drawn button class which *approximatively* mimics 
the behaviour of Aqua buttons on the Mac. 


Description 
=========== 

:class:`AquaButton` is another custom-drawn button class which *approximatively* mimics 
the behaviour of Aqua buttons on the Mac. At the moment this class supports: 

* Bubble and shadow effects; 
* Customizable background, foreground and hover colours; 
* Rounded-corners buttons; 
* Text-only or image+text buttons; 
* Pulse effect on gaining focus. 

And a lot more. Check the demo for an almost complete review of the functionalities. 


Usage 
===== 

Sample usage:: 

    import wx 
    import wx.lib.agw.aquabutton as AB 

    app = wx.App(0) 

    frame = wx.Frame(None, -1, "AquaButton Test") 

    mainPanel = wx.Panel(frame) 
    mainPanel.SetBackgroundColour(wx.WHITE) 

    # Initialize AquaButton 1 (with image) 
    bitmap = wx.Bitmap("my_button_bitmap.png", wx.BITMAP_TYPE_PNG) 
    btn1 = AB.AquaButton(mainPanel, -1, bitmap, "AquaButton") 

    # Initialize AquaButton 2 (no image) 
    btn2 = AB.AquaButton(mainPanel, -1, None, "Hello World!") 

    frame.Show() 

    app.MainLoop() 


Supported Platforms 
=================== 

AquaButton has been tested on the following platforms: 
    * Windows (Windows XP); 
    * Linux Ubuntu (10.10). 


Window Styles 
============= 

`No particular window styles are available for this class.` 


Events Processing 
================= 

This class processes the following events: 

================= ================================================== 
Event Name  Description 
================= ================================================== 
``wx.EVT_BUTTON`` Process a `wxEVT_COMMAND_BUTTON_CLICKED` event, when the button is clicked. 
================= ================================================== 


License And Version 
=================== 

:class:`AquaButton` control is distributed under the wxPython license. 

Latest Revision: Andrea Gavana @ 22 Nov 2011, 22.00 GMT 

Version 0.4 

""" 

import wx 

# Constants for the hovering and clicking effects 
HOVER = 1 
""" Indicates that the mouse is hovering over :class:`AquaButton` """ 
CLICK = 2 
""" Indicates that :class:`AquaButton` has been clicked """ 


class AquaButtonEvent(wx.PyCommandEvent): 
    """ Event sent from the :class:`AquaButton` buttons when the button is activated. """ 

    def __init__(self, eventType, eventId): 
     """ 
     Default class constructor. 

     :param integer `eventType`: the event type; 
     :param integer `eventId`: the event identifier. 
     """ 

     wx.PyCommandEvent.__init__(self, eventType, eventId) 
     self.isDown = False 
     self.theButton = None 


    def SetButtonObj(self, btn): 
     """ 
     Sets the event object for the event. 

     :param `btn`: the button object, an instance of :class:`AquaButton`. 
     """ 

     self.theButton = btn 

剪斷

class AquaButton(wx.PyControl): 
    """ This is the main class implementation of :class:`AquaButton`. """ 

    def __init__(self, parent, id=wx.ID_ANY, bitmap=None, label="", pos=wx.DefaultPosition, 
       size=wx.DefaultSize, style=wx.NO_BORDER, validator=wx.DefaultValidator, 
       name="aquabutton"): 
     """ 
     Default class constructor. 

     :param Window `parent`: parent window. Must not be ``None``; 
     :param integer `id`: window identifier. A value of -1 indicates a default value; 
     :param Bitmap `bitmap`: the button bitmap (if any); 
     :param string `label`: the button text label; 
     :param `pos`: the control position. A value of (-1, -1) indicates a default position, 
     chosen by either the windowing system or wxPython, depending on platform; 
     :type `pos`: tuple or :class:`Point` 
     :param `size`: the control size. A value of (-1, -1) indicates a default size, 
     chosen by either the windowing system or wxPython, depending on platform; 
     :type `size`: tuple or :class:`Size` 
     :param integer `style`: the button style (unused); 
     :param Validator `validator`: the validator associated to the button; 
     :param string `name`: the button name. 
     """ 

     wx.PyControl.__init__(self, parent, id, pos, size, style, validator, name) 

     self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM) 

     self.Bind(wx.EVT_PAINT, self.OnPaint) 
     self.Bind(wx.EVT_ERASE_BACKGROUND, lambda event: None) 
     self.Bind(wx.EVT_SIZE, self.OnSize) 
     self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) 
     self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) 
     self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave) 
     self.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter) 
     self.Bind(wx.EVT_SET_FOCUS, self.OnGainFocus) 
     self.Bind(wx.EVT_KILL_FOCUS, self.OnLoseFocus) 
     self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) 
     self.Bind(wx.EVT_KEY_UP, self.OnKeyUp) 
     self.Bind(wx.EVT_TIMER, self.OnPulseTimer) 

     if "__WXMSW__" in wx.PlatformInfo: 
      self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDown) 

     self._mouseAction = None 
     self.SetBitmapLabel(bitmap) 
     self._hasFocus = False 
     self._saveBitmap = True 
     self._storedBitmap = wx.NullBitmap 
     self._pulseOnFocus = False 
     self._gammaFactor = 1.0 
     self._gammaIncrement = 0.1 

     self._timer = wx.Timer(self, wx.ID_ANY) 

     self.SetLabel(label) 
     self.InheritAttributes() 
     self.SetInitialSize(size) 

     # The following defaults are better suited to draw the text outline 
     if "__WXMAC__" in wx.PlatformInfo: 
      self._backColour = wx.Colour(147, 202, 255) 
      self._hoverColour = self.LightColour(self._backColour, 30) 
      self._disableColour = self.LightColour(self._backColour, 70) 
      self._textColour = wx.BLACK 
     else: 
      self._backColour = wx.SystemSettings.GetColour(wx.SYS_COLOUR_ACTIVECAPTION) 
      self._hoverColour = self.LightColour(self._backColour, 30) 
      self._disableColour = self.LightColour(self._backColour, 70) 
      self._textColour = wx.WHITE 


    def SetBitmapLabel(self, bitmap): 
     """ 
     Sets the bitmap label for the button. 

     :param `bitmap`: the bitmap label to set, an instance of :class:`Bitmap`. 
     """ 

     self._bitmap = bitmap 
     self.Refresh() 
+0

同意。你能指出我的一些(可選的Python)代碼來證明這一點嗎?我發現迄今爲止我找到/創建的例子並不令人滿意。 –

+0

上面添加了一個示例。 –