Sie sind auf Seite 1von 8

Coding Optimization

Use Data Set in Following Situation


Navigate between multiple discrete tables of results.
Manipulate data from multiple sources (for example, a mixture of data from more than one
database, from an XML file, and from a spreadsheet).
Exchange data between tiers or using an XML eb service. !nli"e the DataReader, the
DataSet can be passed to a remote client.
#euse the same set of rows to achieve a performance gain b$ caching them (such as for
sorting, searching, or filtering the data).
%erform a large amount of processing per row. Extended processing on each row returned
using a DataReader ties up the connection serving the DataReader longer than necessar$,
impacting performance.
Manipulate data using XML operations such as Extensible &t$lesheet Language
'ransformations (X&L' transformations) or X%ath (ueries.
Use the DataReader in your application if you:
)o not need to cache the data.
*re processing a set of results too large to fit into memor$.
Need to (uic"l$ access data once, in a forward+onl$ and read+onl$ manner.
Use Datatable In he Following Situation:
'he )ata'able is a central ob,ect in the *)-.NE' librar$. -ther ob,ects that use the
)ata'able include the )ata&et and the )ata.iew.
/f $ou are creating a )ata'able programmaticall$, $ou must first define its schema b$ adding
)ata0olumn ob,ects to the )ata0olumn0ollection 'o add rows to a )ata'able, $ou must first use
the New#ow method to return a new )ata#ow ob,ect. 'he New#ow method returns a row with
the schema of the )ata'able, as it is defined b$ the table1s )ata0olumn0ollection.
'he schema of a table is defined b$ the )ata0olumn0ollection, the collection of
)ata0olumn ob,ects. 'he )ata0olumn0ollection is accessed through the 0olumns propert$.
'he )ata'able contains a collection of 0onstraint ob,ects that can be used to ensure the
integrit$ of the data..
'o determine when changes are made to a table, use one of the following events2
#ow0hanged,#ow0hanging,#ow)eleting, and #ow)eleted.
'o #etrieve records from one 'able then no need to select )ata&et instead we can
select )ata'able.
!mlreader:
!mlReader provides forward+onl$, read+onl$ access to a stream of XML data. 'he
current node refers to the node on which the reader is positioned. 'he reader is advanced using
an$ of the read method
"#$% O C#OOS$ D&&'RID2
3.'he )ata4rid eb control provides the greatest feature set of the three data eb controls, with
its abilit$ to allow the end+user to sort, page, and edit its data.
5.'he )ata4rid is also the simplest data eb control to get started with, as using it re(uires
nothing more than adding a )ata4rid to the eb page and writing a few lines of code.
6. 'he ease of use and impressive features comes at a cost, though, namel$ that of performance2
the )ata4rid is the least efficient of the three data eb controls, especiall$ when placed within a
eb form.
"#$% O C#OOS$ D&&(IS2
3.ith its templates, the )ataList provides more control over the loo" and feel of the displa$ed
data than the )ata4rid.
5.!sing templates, however, t$picall$ re(uires more development time than using the )ata4rid1s
column t$pes.
6.'he )ataList also supports inline editing of data, but re(uires a bit more wor" to implement than
the )ata4rid.
7.!nfortunatel$, providing paging and sorting support in the )ataList is not a trivial exercise.
8. Ma"ing up for these lac"ing built+in features, the )ataList offers better performance over the
)ata4rid.
"#$% O C#OOS$ R$)$&$R2
3.'he #epeater control allows for complete and total control of the rendered 9'ML mar"up.
5.ith the #epeater, the onl$ 9'ML emitted are the values of the databinding statements in the
templates along with the 9'ML mar"up specified in the templates:no ;extra; 9'ML is emitted,
as with the )ata4rid and )ataList.
6.<$ re(uiring the developer to specif$ the complete generated 9'ML mar"up, the #epeater often
re(uires the longest development time.
7.=urthermore, the #epeater does not offer built+in editing, sorting, or paging support.
8.9owever, the #epeater does boast the best performance of the three data eb controls.
>. /ts performance is comparable to the )ataList1s, but noticeabl$ better than the )ata4rid1s
US$ D&&S$ I% #$ FO((O"I%' SIU&IO%:
3.Navigate between multiple discrete tables of results.
5.Manipulate data from multiple sources (for example, a mixture of data from more than one
database, from an XML file, and from a spreadsheet).
6.Exchange data between tiers or using an XML eb service. !nli"e the )ata#eader, the
)ata&et can be passed to a remote client.
7.#euse the same set of rows to achieve a performance gain b$ caching them (such as for
sorting, searching, or filtering the data).
8.%erform a large amount of processing per row. Extended processing on each row returned
using a )ata#eader ties up the connection serving the )ata#eader longer than necessar$,
impacting performance.
ips
*+ Disable the Debug ,ode or Set Debug -.false.
9ow it affect performance2 <$ default this attribute is ;true; when $ou create new application and
is useful when $ou are developing the application. )ebug ? true means that pdb information to be
inserted into file and this results a larger file si@e and it1s performance issue. <efore deplo$ment
$ou should set the following tag

Acompilation defaultLanguage?;0B; debug?;false;C
5. Set trace enabled-.false.

9ow it affects performance2 ith help of tracing, we can trac" the application1s and the
se(uences. Enabling tracing adds performance overhead and might expose private information,
so it should be enabled onl$ while an application is being activel$ anal$@ed. Dou can turn off
tracing using

A'race enabled?;false; + + + + +EC
6. "hile de/eloping using 0isual Studio+%$

hen $ou set 0onfigurations -ption as ;debug; mode, it creates pdb file to store the debug
information hence before deplo$ing the application set it to the ;#elease; mode.

Dou can set using

&elect Menu <uild +C 0onfiguration Manager ++ C &et the configuration option of pro,ect to the
;#elease; mode.
7. Disable the /iewstate:

ith the help of automatic state management feature, the server control is re+populate
their values without writing an$ code. *nd it affects the performance. 'he alwa$s set
Enable.iew&tate ? false when not re(uires.
=or control
Aasp2 datagrid Enable.iew&tate?;false; datasource?;...; runat?;server;EC
=or %age
AFG %age Enable.iew&tate?;false; FC
8. Use Caching to impro/e the performance of your application+
-utput0aching enables $our page to be cached for specific duration and
can be made invalid based on various parameters that can be specified. 'he
0ache exists for the duration $ou specif$ and until that time, the re(uests do not
go to the server and are served from the 0ache.
)o not assign cached items a short expiration. /tems that expire (uic"l$
cause unnecessar$ turnover in the cache and fre(uentl$ cause more wor" for
cleanup code and the garbage collector. /n case $ou have static as well as
d$namic sections of $our page, tr$ to use %artial 0aching (=ragment 0aching) b$
brea"ing up $our page into user controls and specif$ 0aching for onl$ those
0ontrols which are more+or+less static.
>. &lways use the String builder to concatenate string
The memory representation of string is an array of characters, So on re-assigning
the new array of Char is formed & the start address is changed. Thus keeping the old
string in memory for garbage collector to be disposed. Hence application is slow down.
Always use the string builder for concatenating string.
1est )ractice For #andling $2ception:
$2ception #andling
3. Never do a 1catch exception and do nothing1. /f $ou hide an exception, $ou will never "now if
the exception happened or not.
5. /n case of exceptions, give a friendl$ message to the user, but log the actual error with all
possible details about the error, including the time it occurred, method and class name etc.
6. *lwa$s catch onl$ the specific exception, not generic exception.
4ood2
void #ead=rom=ile ( string fileName )
H
tr$
I H
EE read from file.
J
catch (=ile/-Exception ex)
H
EE log error.
EE re+throw exception depending on $our case.
throwK
J
J
Not 4ood2
void #ead=rom=ile ( string fileName )
H
tr$
H
EE read from file.
J
catch (Exception ex)
H
EE 0atching general exception is bad... we will never "now whether it
EE was a file error or some other error.
EE 9ere $ou are hiding an exception.
EE /n this case no one will ever "now that an exception happened.
return ;;K
J
J
7. No need to catch the general exception in all $our methods. Leave it open and let the
application crash. 'his will help $ou find most of the errors during development c$cle.
8. Dou can have an application level (thread level) error handler where $ou can handle all
general exceptions. /n case of an 1unexpected general error1, this error handler should catch the
exception and should log the error in addition to giving a friendl$ message to the user before
closing the application, or allowing the user to 1ignore and proceed1.
>. )o not write tr$+catch in all $our methods. !se it onl$ if there is a possibilit$ that a a specific
exception ma$ occur. =or example, if $ou are writing into a file, handle onl$ =ile/-Exception.
I. )o not write ver$ large tr$+catch bloc"s. /f re(uired, write separate tr$+catch for each tas" $ou
perform and enclose onl$ the specific piece of code inside the tr$+catch. 'his will help $ou find
which piece of code generated the exception and $ou can give specific error message to the user.
L. Dou ma$ write $our own custom exception classes, if re(uired in $our application. )o not
derive $our custom exceptions from the base class &$stemException. /nstead, inherit from
*pplicationException.
M. !se Exception, if the Event is reall$ Exceptional and it gives error(Li"e !nExcepted End of
=ile), because catching the error programmaticall$ ta"es less time than exception.
3N. *lwa$s order exceptions in catch bloc"s from the most specific to the least specific. 'his
techni(ue handles the specific exception before it is passed to a more general catch bloc".
33. /nclude a locali@ed description string in ever$ exception. hen the user sees an error
message, it is derived from the description string of the exception that was thrown, rather than
from the exception class.
35. !se grammaticall$ correct error messages, including ending punctuation. Each sentence
in a description string of an exception should end in a period.
36. %rovide $2ception properties for programmatic access. /nclude extra information in an
exception (in addition to the description string) onl$ when there is a programmatic scenario
where the additional information is useful.
Concatenation of String
0ancatenation of string should not be done li"e this
&tring s ? ;abc;K
sO? ;def;K
sO?;ghi;
instead
&tring<uilder s ? New &tring<uilder()
s.*ppend(;abc;)K
s.*ppend(;def;)K
string " ? s.'o&tring()K
%ote 2 'he &tring<uilder class is available in System+e2t namespace.
Casting
)o not use t$pe casting li"e this
s ? (classname) ob,ect name
instead use
s ? -b,ectname as classname
Session 0ariables
*lwa$s tr$ to minimi@e the usage of session variables as the$ affect the
performance severel$.
#ard Coding
*lwa$s avoid hard coding
eg2 &tring abc?;hello;K
instead
public const string MD&'#/N4?;hello;K
string abc?MD&'#/N4
can be used.
'he above said scenario can be used. if hard coding is done in more than
one place.
$mpty String
)o not use
&tring s?; ;K
instead of use
&tring s?&tring.Empt$K
Commenting:
*ll procedures and functions should begin with a brief comment describing the
functional characteristics of the procedure (what it does).
%rocedure header comment bloc"s should include the following section headings.
Section heading Comment description
%urpose hat the procedure does (not how).
/nputs Each argument that ma$ not be obvious. *rguments are on a
separate line with inline comments.
#eturns Explanation of the values returned b$ functions.
)oints to %ote
Ever$ important variable declaration should include an inline comment describing the use
of the variable being declared.
.ariables, controls, and procedures should be named clearl$ enough that inline
commenting is onl$ needed for complex implementation details.
Note: This structure should be implemented in all functional pages
AFG Language?Pava&criptFC
EE QRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
EEName 2 Name of the *&%X
EEQ)escription 2 * brief description of the pageSs functionalit$
EE Q)eveloper 2 Name of the developer
EE QList of pages this aspx navigates to2 page3, page5
EE Q0reation )ate 2 )ate on which the page was created
EE QLast revised 2 )ate on which the page was last revised
EE Q#evision histor$
EE QModified b$ )ate Modified #eason for modification
EE Qxxxxx xxExxExxxx xxxxx
EE Q$$$$ $$E$$E$$$$ $$$$$$$$
EE QRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
EE)eclare variables
int n0ounter
string str=Name
File Comments
*t the top of $our .cs file please have a description of $our cs file2
EEEAsummar$C
33File %ame 2 *pplication Name
33Description 2 'he main purpose of this file is to.................
33De/eloper 2 )eveloper Name
33Creation Date 2 0reation )ate
EEEAEsummar$C
EEA#E./&/-NT9/&'-#DC
EEA&No value?UUC
EE Modified <$2
EE )ate of Modification2
EE #eason for modification2
EE Modification )one2
EE )efect /d (/f an$)2
EEA&NoC
EEAE#E./&/-NT9/&'-#DC
,ethods and #elper function Comments:
*t the top of ever$ method in $our class please have a description of $our method2
EEE Asummar$C
EEE Method Name 2/nsert!ser
EEE %urpose 2 'his method is used to insert user details
333)eveloper 2 )eveloper Name
3330reation )ate 2 0reation )ate
EEEAEsummar$C
EEE Aparam name ? ; ; t$pe ? ;/nput;C&tring arra$ holding the user detailsAEparamC
EEEAparam name ? ; ; t$pe ? ;/nput;C0onnection &tring of the format AEparamC
EEEAparam name ? ; ; t$pe ? ;-utput;C&tring containing the #ecord /dAEparamC
EEEAreturnsC'he result as a booleanAEreturnsC
EEA#E./&/-NT9/&'-#DC
EEA&No value?UUC
EE Modified <$2
EE )ate of Modification2
EE #eason for modification2
EE Modification )one2
EE )efect /d (/f an$)2
EEA&NoC
EEAE#E./&/-NT9/&'-#DC
Class Comments:
*t the top of ever$ class in $our file please have a description of $our class2
EEE Asummar$C
EE 0lass Name 2 %rotot$pe
EE %urpose 2'his %rotot$pe is used V..
33)eveloper 2 )eveloper Name
330reation )ate 2 0reation )ate
EEEAEsummar$C
EEA#E./&/-NT9/&'-#DC
EEA&No value?UUC
EE Modified <$2
EE )ate of Modification2
EE #eason for modification2
EE Modification )one2
EE )efect /d (/f an$)2
EEA&NoC
EEAE#E./&/-NT9/&'-#DC
'he above comments should be in the form of XML comments.

Das könnte Ihnen auch gefallen