coding standards asp.net

Upload: ravheemathpal957

Post on 29-May-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Coding Standards ASP.net

    1/18

    http://www.dotnetspider.com/tutorials/BestPractices.aspx

    C# Coding Standards and Best Programming Practices

    By

    http://www.dotnetspider.com

  • 8/9/2019 Coding Standards ASP.net

    2/18

    http://www.dotnetspider.com/tutorials/BestPractices.aspx

    1. Author ......................................................................................................................... 3

    2. License, Copyrights and Disclaimer ........................................................................... 3

    3. Revision History ......................................................................................................... 3

    4. Introduction................................................................................................................. 3

    5. Purpose of coding standards and best practices.......................................................... 36. How to follow the standards across the team.............................................................. 4

    7. Naming Conventions and Standards........................................................................... 48. Indentation and Spacing.............................................................................................. 7

    9. Good Programming practices ................................................................................... 10

    10. Architecture............................................................................................................... 1511. ASP.NET .................................................................................................................. 16

    12. Comments ................................................................................................................. 16

    13. Exception Handling .................................................................................................. 17

  • 8/9/2019 Coding Standards ASP.net

    3/18

    http://www.dotnetspider.com/tutorials/BestPractices.aspx

    1. Author

    This document is prepared by the dotnetspider team. Latest version of this document can bedownloaded from http://www.dotnetspider.com/tutorials/BestPractices.aspx. Please post yourcomments and feedback about this document in the above url.

    Most of the information in this document is compiled from the coding standards and bestpractices published in various articles in dotnetspider.com. Also, we referred to the guidelinespublished by Microsoft and various other sources.

    2. License, Copyrights and Disclaimer

    You are permitted to use and distribute this document for any non commercial purpose as long asyou retain this license & copyrights information.

    This document is provided on As-Is basis. The author of this document will not be responsiblefor any kind of loss for you due to any inaccurate information provided in this document.

    3. Revision History

    If you are editing this document, you are required to fill the revision history with your name andtime stamp so that anybody can easily distinguish your updates from the original author.

    Sl# Date Changed By Description1

    4. Introduction

    Anybody can write code. With a few months of programming experience, you can write 'workingapplications'. Making it work is easy, but doing it the right way requires more work, than justmaking it work.

    Believe it, majority of the programmers write 'working code', but not good code'. Writing 'goodcode' is an art and you must learn and practice it.

    Everyone may have different definitions for the term good code. In my definition, the followingare the characteristics of good code.

    Reliable

    Maintainable

    Efficient

    Most of the developers are inclined towards writing code for higher performance, compromisingreliability and maintainability. But considering the long term ROI (Return On Investment),efficiency and performance comes below reliability and maintainability. If your code is not reliableand maintainable, you (and your company) will be spending lot of time to identify issues, trying tounderstand code etc throughout the life of your application.

    5. Purpose of coding standards and best practices

  • 8/9/2019 Coding Standards ASP.net

    4/18

    http://www.dotnetspider.com/tutorials/BestPractices.aspx

    To develop reliable and maintainable applications, you must follow coding standards and best practices.

    The naming conventions, coding standards and best practices described in this document are compiled fromour own experience and by referring to various Microsoft and non Microsoft guidelines.

    There are several standards exists in the programming industry. None of them are wrong or bad and youmay follow any of them. What is more important is, selecting one standard approach and ensuring that

    everyone is following it.

    6. How to follow the standards across the team

    If you have a team of different skills and tastes, you are going to have a tough time convincingeveryone to follow the same standards. The best approach is to have a team meeting anddeveloping your own standards document. You may use this document as a template to prepareyour own document.

    Distribute a copy of this document (or your own coding standard document) well ahead of thecoding standards meeting. All members should come to the meeting prepared to discuss prosand cons of the various points in the document. Make sure you have a manager present in themeeting to resolve conflicts.

    Discuss all points in the document. Everyone may have a different opinion about each point, butat the end of the discussion, all members must agree upon the standard you are going to follow.Prepare a new standards document with appropriate changes based on the suggestions from allof the team members. Print copies of it and post it in all workstations.

    After you start the development, you must schedule code review meetings to ensure thateveryone is following the rules. 3 types of code reviews are recommended:

    1. Peer review another team member review the code to ensure that the code follows thecoding standards and meets requirements. This level of review can include some unittesting also. Every file in the project must go through this process.

    2. Architect review the architect of the team must review the core modules of the project

    to ensure that they adhere to the design and there is no big mistakes that can affect theproject in the long run.

    3. Group review randomly select one or more files and conduct a group review once in aweek. Distribute a printed copy of the files to all team members 30 minutes before themeeting. Let them read and come up with points for discussion. In the group reviewmeeting, use a projector to display the file content in the screen. Go through everysections of the code and let every member give their suggestions on how could that pieceof code can be written in a better way. (Dont forget to appreciate the developer for thegood work and also make sure he does not get offended by the group attack!)

    7. Naming Conventions and Standards

    NNoottee ::

    TThhee tteerrmmss PPaassccaall CCaassiinngg aanndd CCaammeell CCaassiinngg aarree uusseedd tthhrroouugghhoouutt tthhiiss ddooccuummeenntt..

    PPaassccaall CCaassiinngg-- FFiirrsstt cchhaarraacctteerr ooff aallll wwoorrddss aarree UUppppeerr CCaassee aanndd ootthheerr cchhaarraacctteerrss aarree lloowweerr ccaassee..

    EExxaammppllee:: BBaacckkCCoolloorr

    CCaammeell CCaassiinngg --FFiirrsstt cchhaarraacctteerr ooff aallll wwoorrddss,, eexxcceepptt tthhee ffiirrsstt wwoorrdd aarree UUppppeerr CCaassee aanndd ootthheerr cchhaarraacctteerrss aarree

  • 8/9/2019 Coding Standards ASP.net

    5/18

    http://www.dotnetspider.com/tutorials/BestPractices.aspx

    lloowweerr ccaassee..

    EExxaammppllee:: bbaacckkCCoolloorr

    1. UUssee PPaassccaall ccaassiinngg ffoorr CCllaassss nnaammeess

    ppuubblliicc ccllaassssHHeellllooWWoorrlldd

    {{

    ......

    }}

    2. UUssee PPaassccaall ccaassiinngg ffoorr MMeetthhoodd nnaammeess

    vvooiiddSSaayyHHeelllloo((ssttrriinngg nnaammee))

    {{

    ......

    }}

    3. UUssee CCaammeell ccaassiinngg ffoorr vvaarriiaabblleess aanndd mmeetthhoodd ppaarraammeetteerrss

    iinnttttoottaallCCoouunntt == 00;;

    vvooiidd SSaayyHHeelllloo((ssttrriinngg nnaammee))

    {{

    ssttrriinnggffuullllMMeessssaaggee == ""HHeelllloo "" ++ nnaammee;;

    ......

    }}

    4. UUssee tthhee pprreeffiixx II wwiitthh CCaammeell CCaassiinngg ffoorr iinntteerrffaacceess (( EExxaammppllee::IIEEnnttiittyy ))

    5. DDoo nnoott uussee HHuunnggaarriiaann nnoottaattiioonn ttoo nnaammee vvaarriiaabblleess..

    IInn eeaarrlliieerr ddaayyss mmoosstt ooff tthhee pprrooggrraammmmeerrss lliikkeedd iitt -- hhaavviinngg tthhee ddaattaa ttyyppee aass aa pprreeffiixx ffoorr tthhee vvaarriiaabblleennaammee aanndd uussiinngg mm__aass pprreeffiixx ffoorr mmeemmbbeerr vvaarriiaabblleess.. EEgg::

    ssttrriinngg mm__ssNNaammee;;

    iinntt nnAAggee;;

    HHoowweevveerr,, iinn ..NNEETT ccooddiinngg ssttaannddaarrddss,, tthhiiss iiss nnoott rreeccoommmmeennddeedd.. UUssaaggee ooff ddaattaa ttyyppee aanndd mm__ttoo rreepprreesseennttmmeemmbbeerr vvaarriiaabblleess sshhoouulldd nnoott bbee uusseedd.. AAllll vvaarriiaabblleess sshhoouulldd uussee ccaammeell ccaassiinngg..

    SSoommee pprrooggrraammmmeerrss ssttiillll pprreeffeerr ttoo uussee tthhee pprreeffiixxmm__ttoo rreepprreesseenntt mmeemmbbeerr vvaarriiaabblleess,, ssiinnccee tthheerree iiss nnoo ootthheerreeaassyy wwaayy ttoo iiddeennttiiffyy aa mmeemmbbeerr vvaarriiaabbllee..

    6.UUssee MMeeaanniinnggffuull,, ddeessccrriippttiivvee wwoorrddss ttoo nnaammee vvaarriiaabblleess..D

    Doo

    nn

    oo

    tt

    uu

    ssee

    aa

    bb

    bb

    rree

    vviiaa

    ttiioo

    nn

    ss..

    GGoooodd::

    ssttrriinngg aaddddrreessss

    iinntt ssaallaarryy

    NNoott GGoooodd::

    ssttrriinngg nnaamm

    ssttrriinngg aaddddrr

  • 8/9/2019 Coding Standards ASP.net

    6/18

  • 8/9/2019 Coding Standards ASP.net

    7/18

    http://www.dotnetspider.com/tutorials/BestPractices.aspx

    DDrrooppDDoowwnnLLiisstt ddl

    LLiissttBBooxx lst

    DDaattaaLLiisstt dtl

    RReeppeeaatteerr rep

    CChheecckkbbooxx chk

    CChheecckkBBooxxLLiisstt cbl

    RRaaddiiooBBuuttttoonn rdo

    RRaaddiiooBBuuttttoonnLLiisstt rbl

    IImmaaggee img

    PPaanneell pnl

    PPllaacceeHHoollddeerr phd

    TTaabbllee tbl

    VVaalliiddaattoorrss val

    14. FFiillee nnaammee sshhoouulldd mmaattcchh wwiitthh ccllaassss nnaammee..

    FFoorr eexxaammppllee,, ffoorr tthhee ccllaassss HHeellllooWWoorrlldd,, tthhee ffiillee nnaammee sshhoouulldd bbee hheelllloowwoorrlldd..ccss ((oorr,, hheelllloowwoorrlldd..vvbb))

    15. UUssee PPaassccaall CCaassee ffoorr ffiillee nnaammeess..

    8. IInnddeennttaattiioonn aanndd SSppaacciinngg

    1. UUssee TTAABB ffoorr iinnddeennttaattiioonn.. DDoo nnoott uussee SSPPAACCEESS.. DDeeffiinnee tthhee TTaabb ssiizzee aass 44..

    2. CCoommmmeennttss sshhoouulldd bbee iinn tthhee ssaammee lleevveell aass tthhee ccooddee ((uussee tthhee ssaammee lleevveell ooff iinnddeennttaattiioonn))..

    GGoooodd::

    //// FFoorrmmaatt aa mmeessssaaggee aanndd ddiissppllaayy

  • 8/9/2019 Coding Standards ASP.net

    8/18

    http://www.dotnetspider.com/tutorials/BestPractices.aspx

    ssttrriinngg ffuullllMMeessssaaggee == ""HHeelllloo "" ++ nnaammee;;

    DDaatteeTTiimmee ccuurrrreennttTTiimmee == DDaatteeTTiimmee..NNooww;;

    ssttrriinngg mmeessssaaggee == ffuullllMMeessssaaggee ++ "",, tthhee ttiimmee iiss :: "" ++

    ccuurrrreennttTTiimmee..TTooSShhoorrttTTiimmeeSSttrriinngg(());;

    MMeessssaaggeeBBooxx..SShhooww (( mmeessssaaggee ));;

    NNoott GGoooodd::

    //// FFoorrmmaatt aa mmeessssaaggee aanndd ddiissppllaayy

    ssttrriinngg ffuullllMMeessssaaggee == ""HHeelllloo "" ++ nnaammee;;

    DDaatteeTTiimmee ccuurrrreennttTTiimmee == DDaatteeTTiimmee..NNooww;;

    ssttrriinngg mmeessssaaggee == ffuullllMMeessssaaggee ++ "",, tthhee ttiimmee iiss :: "" ++

    ccuurrrreennttTTiimmee..TTooSShhoorrttTTiimmeeSSttrriinngg(());;

    MMeessssaaggeeBBooxx..SShhooww (( mmeessssaaggee ));;

    3. CCuurrllyy bbrraacceess (( {{}} )) sshhoouulldd bbee iinn tthhee ssaammee lleevveell aass tthhee ccooddee oouuttssiiddee tthhee bbrraacceess..

    4. UUssee oonnee bbllaannkk lliinnee ttoo sseeppaarraattee llooggiiccaall ggrroouuppss ooff ccooddee..

    GGoooodd::bbooooll SSaayyHHeelllloo (( ssttrriinngg nnaammee ))

    {{

    ssttrriinngg ffuullllMMeessssaaggee == ""HHeelllloo "" ++ nnaammee;;

    DDaatteeTTiimmee ccuurrrreennttTTiimmee == DDaatteeTTiimmee..NNooww;;

    ssttrriinngg mmeessssaaggee == ffuullllMMeessssaaggee ++ "",, tthhee ttiimmee iiss :: "" ++

    ccuurrrreennttTTiimmee..TTooSShhoorrttTTiimmeeSSttrriinngg(());;

    MMeessssaaggeeBBooxx..SShhooww (( mmeessssaaggee ));;

    iiff (( ...... ))

    {{

    //// DDoo ssoommeetthhiinngg

    //// ......

    rreettuurrnn ffaallssee;;

    }}

    rreettuurrnn ttrruuee;;

    }}

    NNoott GGoooodd::

    bbooooll SSaayyHHeelllloo ((ssttrriinngg nnaammee))

    {{

    ssttrriinngg ffuullllMMeessssaaggee == ""HHeelllloo "" ++ nnaammee;;

    DDaatteeTTiimmee ccuurrrreennttTTiimmee == DDaatteeTTiimmee..NNooww;;

    ssttrriinngg mmeessssaaggee == ffuullllMMeessssaaggee ++ "",, tthhee ttiimmee iiss :: "" ++

    ccuurrrreennttTTiimmee..TTooSShhoorrttTTiimmeeSSttrriinngg(());;

    MMeessssaaggeeBBooxx..SShhooww (( mmeessssaaggee ));;

    iiff (( ...... ))

    {{

  • 8/9/2019 Coding Standards ASP.net

    9/18

    http://www.dotnetspider.com/tutorials/BestPractices.aspx

    //// DDoo ssoommeetthhiinngg

    //// ......

    rreettuurrnn ffaallssee;;

    }}

    rreettuurrnn ttrruuee;;

    }}

    5. TThheerree sshhoouulldd bbee oonnee aanndd oonnllyy oonnee ssiinnggllee bbllaannkk lliinnee bbeettwweeeenn eeaacchh mmeetthhoodd iinnssiiddee tthhee ccllaassss..

    6. TThhee ccuurrllyy bbrraacceess sshhoouulldd bbee oonn aa sseeppaarraattee lliinnee aanndd nnoott iinn tthhee ssaammee lliinnee aassiiff,, ffoorr eettcc..

    GGoooodd::iiff (( ...... ))

    {{

    //// DDoo ssoommeetthhiinngg

    }}

    NNoott GGoooodd::

    iiff (( ...... )) {{

    //// DDoo ssoommeetthhiinngg

    }}

    7. UUssee aa ssiinnggllee ssppaaccee bbeeffoorree aanndd aafftteerr eeaacchh ooppeerraattoorr aanndd bbrraacckkeettss..

    GGoooodd::iiff (( sshhoowwRReessuulltt ==== ttrruuee ))

    {{

    ffoorr (( iinntt ii == 00;; ii

  • 8/9/2019 Coding Standards ASP.net

    10/18

    http://www.dotnetspider.com/tutorials/BestPractices.aspx

    9. KKeeeepp pprriivvaattee mmeemmbbeerr vvaarriiaabblleess,, pprrooppeerrttiieess aanndd mmeetthhooddss iinn tthhee ttoopp ooff tthhee ffiillee aanndd ppuubblliiccmmeemmbbeerrss iinn tthhee bboottttoomm..

    9. GGoooodd PPrrooggrraammmmiinngg pprraaccttiicceess

    1. AAvvooiidd wwrriittiinngg vveerryy lloonngg mmeetthhooddss.. AA mmeetthhoodd sshhoouulldd ttyyppiiccaallllyy hhaavvee 11~~2255 lliinneess ooff ccooddee.. IIff aammeetthhoodd hhaass mmoorree tthhaann 2255 lliinneess ooff ccooddee,, yyoouu mmuusstt ccoonnssiiddeerr rree ffaaccttoorriinngg iinnttoo sseeppaarraatteemmeetthhooddss..

    2. MMeetthhoodd nnaammee sshhoouulldd tteellll wwhhaatt iitt ddooeess.. DDoo nnoott uussee mmiiss--lleeaaddiinngg nnaammeess.. IIff tthhee mmeetthhoodd nnaammee iiss

    oobbvviioouuss,, tthheerree iiss nnoo nneeeedd ooff ddooccuummeennttaattiioonn eexxppllaaiinniinngg wwhhaatt tthhee mmeetthhoodd ddooeess..

    GGoooodd::vvooiidd SSaavveePPhhoonneeNNuummbbeerr (( ssttrriinngg pphhoonneeNNuummbbeerr ))

    {{

    //// SSaavvee tthhee pphhoonnee nnuummbbeerr..

    }}

    NNoott GGoooodd::

    //// TThhiiss mmeetthhoodd wwiillll ssaavvee tthhee pphhoonnee nnuummbbeerr..

    vvooiidd SSaavveeDDeettaaiillss (( ssttrriinngg pphhoonneeNNuummbbeerr ))

    {{

    //// SSaavvee tthhee pphhoonnee nnuummbbeerr..

    }}

    3. AA mmeetthhoodd sshhoouulldd ddoo oonnllyy ''oonnee jjoobb''.. DDoo nnoott ccoommbbiinnee mmoorree tthhaann oonnee jjoobb iinn aa ssiinnggllee mmeetthhoodd,,eevveenn iiff tthhoossee jjoobbss aarree vveerryy ssmmaallll..

    GGoooodd:://// SSaavvee tthhee aaddddrreessss..

    SSaavveeAAddddrreessss (( aaddddrreessss ));;

    //// SSeenndd aann eemmaaiill ttoo tthhee ssuuppeerrvviissoorr ttoo iinnffoorrmm tthhaatt tthhee aaddddrreessss iiss uuppddaatteedd..

    SSeennddEEmmaaiill (( aaddddrreessss,, eemmaaiill ));;

  • 8/9/2019 Coding Standards ASP.net

    11/18

  • 8/9/2019 Coding Standards ASP.net

    12/18

  • 8/9/2019 Coding Standards ASP.net

    13/18

    http://www.dotnetspider.com/tutorials/BestPractices.aspx

    {{

    HHttmmll,,

    PPllaaiinnTTeexxtt,,

    AAttttaacchhmmeenntt

    }}

    vvooiidd SSeennddMMaaiill ((ssttrriinngg mmeessssaaggee,, MMaaiillTTyyppee mmaaiillTTyyppee))

    {{

    sswwiittcchh (( mmaaiillTTyyppee ))

    {{

    ccaassee MMaaiillTTyyppee..HHttmmll::

    //// DDoo ssoommeetthhiinngg

    bbrreeaakk;;

    ccaassee MMaaiillTTyyppee..PPllaaiinnTTeexxtt::

    //// DDoo ssoommeetthhiinngg

    bbrreeaakk;;

    ccaassee MMaaiillTTyyppee..AAttttaacchhmmeenntt::

    //// DDoo ssoommeetthhiinngg

    bbrreeaakk;;

    ddeeffaauulltt::

    //// DDoo ssoommeetthhiinngg

    bbrreeaakk;;

    }}

    }}

    NNoott GGoooodd::

    vvooiidd SSeennddMMaaiill ((ssttrriinngg mmeessssaaggee,, ssttrriinngg mmaaiillTTyyppee))

    {{

    sswwiittcchh (( mmaaiillTTyyppee ))

    {{

    ccaassee ""HHttmmll""::

    //// DDoo ssoommeetthhiinngg

    bbrreeaakk;;

    ccaassee ""PPllaaiinnTTeexxtt""::

    //// DDoo ssoommeetthhiinngg

    bbrreeaakk;;ccaassee ""AAttttaacchhmmeenntt""::

    //// DDoo ssoommeetthhiinngg

    bbrreeaakk;;

    ddeeffaauulltt::

    //// DDoo ssoommeetthhiinngg

    bbrreeaakk;;

    }}

    }}

    12. DDoo nnoott mmaakkee tthhee mmeemmbbeerr vvaarriiaabblleess ppuubblliicc oorr pprrootteecctteedd.. KKeeeepp tthheemm pprriivvaattee aanndd eexxppoosseeppuubblliicc//pprrootteecctteedd PPrrooppeerrttiieess..

    13. TThhee eevveenntt hhaannddlleerr sshhoouulldd nnoott ccoonnttaaiinn tthhee ccooddee ttoo ppeerrffoorrmm tthhee rreeqquuiirreedd aaccttiioonn.. RRaatthheerr ccaallllaannootthheerr mmeetthhoodd ffrroomm tthhee eevveenntt hhaannddlleerr..

    14. DDoo nnoott pprrooggrraammmmaattiiccaallllyy cclliicckk aa bbuuttttoonn ttoo eexxeeccuuttee tthhee ssaammee aaccttiioonn yyoouu hhaavvee wwrriitttteenn iinn tthheebbuuttttoonn cclliicckk eevveenntt.. RRaatthheerr,, ccaallll tthhee ssaammee mmeetthhoodd wwhhiicchh iiss ccaalllleedd bbyy tthhee bbuuttttoonn cclliicckk eevveenntthhaannddlleerr..

    15. NNeevveerr hhaarrddccooddee aa ppaatthh oorr ddrriivvee nnaammee iinn ccooddee.. GGeett tthhee aapppplliiccaattiioonn ppaatthh pprrooggrraammmmaattiiccaallllyy aanndduussee rreellaattiivvee ppaatthh..

    16. NNeevveerr aassssuummee tthhaatt yyoouurr ccooddee wwiillll rruunn ffrroomm ddrriivvee ""CC::"".. YYoouu mmaayy nneevveerr kknnooww,, ssoommee uusseerrss mmaayyrruunn iitt ffrroomm nneettwwoorrkk oorr ffrroomm aa ""ZZ::""..

  • 8/9/2019 Coding Standards ASP.net

    14/18

  • 8/9/2019 Coding Standards ASP.net

    15/18

  • 8/9/2019 Coding Standards ASP.net

    16/18

  • 8/9/2019 Coding Standards ASP.net

    17/18

  • 8/9/2019 Coding Standards ASP.net

    18/18