Some basic misapprehensions or faults which we come accross in .NET programming and their replacements.
# The most common error is swallowing exceptions: try { //Something } catch { //Do nothing. } # Don't use "magic numbers" in your code.: Ex: if(mode ==3){...} elseif(mode ==4){...} Use Enumerations wherever possible, so the meaning, not the number, is exposed: if(mode ==MyEnum.value1){...} elseif(mode ==MyEnum.value2){...} (Note:switch statements can be used here, as well) # Performing large amount of string concatenation Bad: string s ="This "; s +="is "; s +="not "; s +="the "; s +="best "; s +="way."; Good: StringBuilder sb =newStringBuilder(); sb.Append("This "); sb.Append("is "); sb.Append("much "); sb.Append("better. "); # Checking a string variable if it is assigned. String object can be null so Wrong!, if(someStringVariable.length >0){ //Do you always get this far? } Correct, if(!String.IsNullOrEmpty(someStringVariable)){ //Yeah! This one is better! } # Using 'USING' statement Don't unnecessarily nest using statements do this instead: using(SQLconnection conn =newSQLConnection()) using(SQLCommand cmd =newSQLCommand("select 1", conn))// no need to nest { conn.open() using(SqlDatareader dr = cmd.ExecuteReader())//nessesary nest { //dr.read() } } # Unnecessary use oftry and catch try { int result =int.Parse(Request.QueryString["pageid"]); } catch {... Instead of using int result; if(!int.TryParse(Request.QueryString["pageid"],out result)) { thrownewArgumentException.... }
Hope U found it Healthy.
Stay Knollecting!!