Monday, October 18, 2010

General Coding Optimization Techniques


  1. Projects and Solutions

    1. In debug mode, compile with Enable Build Warnings option, disable optimizations, and enable integer overflow checks.
    2. In release mode, compile with Treat Compiler Warnings option, enable optimizations, and disable integer overflow checks.
    3. Disable incremental builds.

2. Inheritance

a.    Don't create object hierarchies more than three levels deep.
b.    Use Base suffix for abstract classes.
c.    Use compound name for derived class if possible
d.    In base classes, use abstract members to implement features that are mandatory in derived classes.
e.    Use sealed/NotInheritable keywords for types that shouldn't be derived from.
f.     Use a static class for types that are never instantiated or derived from.
g.    Abstract classes must have a constructor with protected scope.
h.    Mark members with virtual/Overridable keyword only if you know that they might be overridden in derived classes.
i.      If a member is overloaded, mark only the most complete version with the virtual/Overridable keyword.
j.      Override the ToString method to provide a textual representation of an object.
k.    Consider overriding the Equals method if two instances of a type might be considered as equal in some cases.
l.      Consider providing strong-typed versions of the Equals and Compare static methods.
m.   Don't inherit a type from MarshalByRefObject unless you strictly need its additional features.


 5. Fields and Variables

a.    Mark a field as read-only if it isn't supposed to change after instantiation.
b.    Never define public instance fields; instead, expose public or protected properties that wrap a private field.
c.    Mark all public static fields as read-only (or use a constant if possible).
d.    Declare all local variables at the beginning of the method, except block variables and variables wrapped by properties.
e.    Initialize fields in the constructor if initialization order is significant.
f.     Avoid the = (assignment) operator inside expressions.

 6. Properties

a.    Use a read-only property only if returning a private non-array field or the result of a simple calculation; in all other cases, use a method.
b.    Never define write-only properties; use Set-prefixed methods instead.
c.    Use the value keyword only inside the set block of a property.

 7. Methods

a.    Avoid methods longer than 50 executable statements.
b.    Avoid methods with more than 6 arguments.
c.    Define a single exit point for all methods, except special cases that are dealt with at the top of the method.
d.    Exit a function with the Return keyword
e.    Use parenthesis to enclose an expression returned by a function; don't use parenthesis to return a single value.
f.     Avoid overloading of a binary operator in a reference type.
g.    Don't modify arguments passed to a method that overloads an operator.
h.    Throw an exception from inside a constructor if one or more arguments aren't valid.
i.      If a type has a Finalize method, attempt to assign all fields before throwing an exception from inside a constructor.
j.      Ensure that you catch all exceptions in static constructors.

 8. Execution Flow

a.    Use curly braces also for if, else if, for, foreach, and while blocks that contain a single line.
b.    Don’t nest conditional and loop blocks to more than three levels, or four levels in exceptional cases.
c.    Avoid using the ? : ternary operator inside expressions
d.    Don't use the goto statement except to jump to another case block or to the default block in a switch statement.
e.    Don't use a floating-point variable as the controlling variable of a for loop
f.     Avoid unsafe code if possible; if you must use unsafe code, place all your unsafe methods in a DLL.

 9. Exception handling

a.    Don't throw exceptions to control execution flow, e.g. exit a procedure prematurely.
b.    Throw exceptions that are as specific as possible
c.    Don’t catch SystemException and ApplicationException objects.
d.    Don’t catch the following exceptions except for logging reasons: OutOfMemoryException, StackOverflowException, ThreadAbortException, and ExecutionEngineException.
e.    A custom exception type must inherit from ApplicationException and be marked with the Serializable attribute.


10. Numeric types

a.    Use CLS-compliant integer types if possible.
b.    Use shortened notation (e.g. x+=1) for simple increment/decrement operations.
c.    Use ++ and -- operators only inside expressions located in performance-critical code..
d.    Avoid using ++ and -- operators on a variable that appears more than once in the expression.
e.    Use a series of multiplications instead of exponentiation if the exponent is a small integer.

11. Strings

a.    Use resource files for strings that are used in the user interface.
b.    Use Char variables for one-char strings.
c.    Explicitly initialize strings to "" value.
d.    String methods and properties must return an empty string rather than a null string if the result has no characters.
e.    Always check that string arguments aren’t null object references before using them.
f.     Use the String.Compare method to compare strings in case-insensitive mode.
g.    Use the CompareOrdinal static method when comparing strings for equality in case-sensitive, locale-independent mode.
h.    Use the StringBuilder object to concatenate a variable number of strings or to concatenate strings inside a loop.
i.      Initialize the StringBuilder’s internal buffer if you know in advance a reasonable upper limit to the size of the result.
j.      Don’t append more characters to a StringBuilder after you’ve invoked its ToString method.
k.    Read a string from the configuration file if its value might change after deployment.

12. Memory usage

a.    Objects that are meant to live for the entire duration of the application lifetime should be created as early as possible.
b.    Don't box value-typed objects unnecessarily.
c.    Don’t explicitly set an object reference to null when you don’t need it any longer inside a method.
d.    Explicitly set an object reference to null if you are inside a loop and want the .NET runtime to collect the object before the loop ends.

13. ADO.NET Programming

a.    Always protect database operations from unhandled exceptions.
b.    Open a connection in a Try block and close it in the corresponding Finally block.
c.    Adopt Windows authentication to SQL Server security in Windows Forms applications.
d.    Never use the sa account when adopting SQL Server security
e.    Use an autoincrementing (identity) integer column as the primary key of a table if possible, selecting a large enough precision.
f.     Use the Command.ExecuteReader method with the CommandBehavior.SingleRow argument if the result includes max one row.
g.    Use the Command.ExecuteScalar method when the result is a single value.
h.    Consider using the TOP keyword or the SET ROWCOUNT statement to limit the number of rows returned by an SQL statement
i.      Use the CommandBehavior.CloseConnection argument when creating a DataReader object returned by a method.
j.      Invoke the Cancel method of the Command object before closing a DataReader, if you don’t want to read any remaining rows.
k.    Consider using a stored procedure for better performance and security, to execute a batch of commands, when clients can use multiple access technologies (e.g. ADO and ADO.NET).
l.      Close the DataReader object before attempting to read the return value or the output arguments of the stored procedure.
m.   Use CommandBehavior.SequentialAccess with the ExecuteReader method if you read one or more large binary or text columns.
n.    Use a DataTable instead of a DataSet object if possible.
o.    Don't rely on the DataAdapter's autoconnect feature; always explicitly open and close the connection.
p.    Never use CommandBuilder objects.
q.    Set both the AutoIncrementSeed and the AutoIncrementStep property to 1 for columns that correspond to identity fields in Access and SQL Server.
r.     Avoid passing large DataSet objects to remote components.
s.     Open an ADO.NET or COM+ serializable transaction when reading two or more tables in a master-detail relationship.
t.      Close a transaction as soon as possible. Neither accept user input while a transaction is open nor allow the user to decide whether a transaction should be committed or rolled back.
u.    Don’t rely on the default behavior of the DataAdapter object for managing update conflicts.
v.    Use the DataSet.GetChanges method to create a new DataSet pass this new object to the DataAdapter.Update method.

14. Web services

a.    Call a Web service synchronously only from server-side applications and programs with no user interface
b.    Call a Web service asynchronously from inside Windows Forms clients.
c.    Implement one-way Web service calls for lengthy methods if possible.
d.    Use the most recent version of Web Service Enhancements (WSE) to implement advanced functions.
e.    Provide a meaningful description in the WebMethod attribute.

No comments:

Post a Comment