Code reviews and coding recommendations

We don't have code reviews, but sometimes I have to tell the team quite obvious coding recommendations.

There are a few examples:

1. We should do the check if array element is available before access it:

 E.g. BAD:
            string departureCity = Flight[0].StartPoint;

GOOD:      if (Flight.Rows.Count <= 0)
           {
                WarningMessageEvent.Raise("Flight.Rows.Count <= 0");
                return;
            }
            string departureCity = Flight[0].StartPoint;

2. Keep separate functionality in small functions, rather than add morre code to existing functions, that makes them too big.

It is easier to read, test, maintain and investigate errors.

You can use VS Refactor –Extract Method to make functions smaller.

Also highly recommend to install RefactorAsp from http://www.devexpress.com/Products/Visual_Studio_Add-in/RefactorASP/index.xml.

See "Common Refactors" section here about

  • Extract method
  • Replace Method with Method Object
  • Decompose Conditional
  • List of Refactors can be viewed here(or original here).

    3. Do not "eat" exceptions
     

    Even if you don’t want to report errors in production, Debug.Assert them in development environment to understand, why they happen and avoid them.
    E.g. BAD:
                catch
                {
                    //we ignore any errors
                }
     
    GOOD:
                catch(Exception exc)
                {
                    //we ignore any errors in production 
                    Debug.Assert(false, "Investigate why " + exc.ToString());
    }
    4. Use enums, do not hardcode magic values
    See
    Use Enum (C#) instead of magic numbers 
    and (more general) C# Coding Style Guide .
    Aside note: By some reason this particular post is not searchable by Google. I have no idea why it is different from other posts.
    posted @ Thursday, January 22, 2009 8:58 PM
    Print

    Comments on this entry:

    # re: Code reviews and coding recommendations

    Left by codemeit software developer at 2/21/2009 8:05 AM
    Gravatar
    It might be worth to log somewhere fir
    catch(Exception exc)
    {
    LOG.write(exc);
    }

    Your comment:



    (not displayed)

     
     
     
     
     

    Live Comment Preview:

     
    «October»
    SunMonTueWedThuFriSat
    2829301234
    567891011
    12131415161718
    19202122232425
    2627282930311
    2345678