Why I started using Skribit!


I recently saw several folks whose blogs I follow incorporate Sribit so I got curious.

 

Sribit allows readers to suggest blog topics. It provides another mechanism for a blogger and the reader to interact.

You can always comment on existing blog posts.   Now you can also suggest blog posts.

You can ask questions about topics that I have never posted about or haven't posted on in a while.

So if you have a topic that you would like to see me blog about, by all means suggest away.

I will make every effort to at least respond to every suggestion.

So let me what you think.    What posts would you like to see?

author: Nick Harrison | posted @ Friday, November 20, 2009 12:37 PM | Feedback (0)

Reflector Meets the CodeDom


I recently published an article on Simple Talk where I step through creating a plug in for Reflector that will allow Reflector to translate an assembly into the CodeDom code needed to produce the original code. http://www.simple-talk.com/dotnet/.net-framework/-.net-reflector-meets-the-codedom/

The intention is to make CodeDom more accessible. Think of this as a Code Generator Generator. Instead of writing a full generator from scratch, we can start with code that does what we want and tweak the generated generator from there.

Obviously this is a much larger plugin than the article shows. There article outlined an approach to structuring the code and there is still much work to be done before the plugin can support every language structure that we are interested in.

I have setup a project on CodePlex to help facilitate filling in the missing functionality. If anyone is interested in assisting with filling in the gaps let me know. Here is the codeplex project http://reflectorcodedom.codeplex.com/

author: Nick Harrison | posted @ Thursday, October 15, 2009 12:52 AM | Feedback (1)

Simple talk article on Code Smells


Since January I have been writing articles for the simple-talk website. We are up to six now!

My most recent article concerned a concept that I called Code Deodorants as a means to try to prevent Code Smells. This particular article has generated some debate about the term Code Deodorant and whether or not what I am really talking about is better described as Prefactoring.

I would love to hear your thoughts. Do you have any practices that you follow to try to prevent code smells from developing?

Check out the article: http://www.simple-talk.com/dotnet/.net-framework/code-deodorants-for-code-smells/

Or any of the other articles that the folks at simple-talk have been nice enough to publish:

http://www.simple-talk.com/author/nick-harrison/

author: Nick Harrison | posted @ Friday, July 17, 2009 8:37 AM | Feedback (0)

Charlotte Code Camp


I will speaking at the Charlotte Code Camp on March 28. It should be a good time. My topic is titled "Metric Driven Refactorng" This will be based on an article that I recently published with simple talk. http://www.simple-talk.com/dotnet/.net-tools/metric-driven-refactoring-with-.net-reflector/

This is a topic near and dear to my heart. I will try to redeem metrics and make them work for us. This should serve to add some objectivity to an otherwise very subjective exercise. In this presentation and in the article, I have dubbed two metrics "Smelly Metrics" because of the way they help us identify code that needs to be refactored.

It is very nice to be able to run metrics on a solution and regardless of the number of projects, the number of classes, the number of methods, regardless of how complex the solution is, we can immediately identify the most complex areas and immediately target improvements.

If you are in the area, come check out the presentation. If you are not in the area or cannot make it, you can still follow my foray into publishing at http://www.simple-talk.com/author/nick-harrison/

author: Nick Harrison | posted @ Sunday, March 22, 2009 6:28 PM | Feedback (0)

Show – Off always Shown up in Showdown



Show – Off always Shown up in Showdown

This was my fortune cookie the other day.   It was attributed to Confucius.   Now, I doubt that Confucius actually said it.   The humor shines through only in English, I think.   For the sake of not plagiarizing, let’s continue attributing it to Confucius.   It reminds me of another quote that I heard a while back.   This one attributed to a Buddhist monk “What I am I am and say not.   Being is the great explainer”.    We will get to this quote in a moment.  

But first …

If you are a job candidate hoping to make it past the screening process, you may well feel like a show-off.   Regardless of your personality, the job interview is not the time for modesty.   The job interview is the time to say and with a straight face “Hey look at me.   Aren’t I wonderful?   Wouldn’t you like to have me working for you?”  

If you are looking for a job, depending on how you find yourself looking, this can be tough.    If you got fired (laid off, downsized, cost reduced, name your euphemism) such confidence will feel alien and fake.   If you are miserable in your current job and casually looking around, you aren’t really motivated to put it all out there, and you can’t maintain the strong self confident image that you need to present.

Fake or uncomfortable, awkward or contrived, however it makes you feel, this is the façade that you have to put on to get the job.

Most of us technology geeks will more easily tend towards the Buddhist quote listed earlier.    We would rather not have to toot our own horn.   We would rather be modest and casual keeping a low profile and avoiding the spotlight.   We try to live by the second quote mentioned earlier.

“What I am I am and say not.   Being is the great explainer”.    If you are a great programmer, it should be obvious if you are doing your job right.   As a general rule, if you have to walk around saying, “I am great at this” or “I am great at that” you usually are not.   More likely than not, you are an insufferable jerk.   If you really are great, it should be obvious and you should not have to point it out all the time.    This is a great philosophy for the daily grind.

Unfortunately, it doesn’t work so well for job searching.   In a short interview, you flat out don’t have the time for “being to explain” what you are, must shine through quickly, usually in 30 minutes or less.   If not, you may not get the rest of the time needed for being to explain.

So, if you are on the job hunt, let loose your inner braggart.   Just me sure to be him back in the cage after you land your next great opportunity.   If you are the screener, realize that the people you are dealing with are probably acting against their character and venturing outside their comfort zone.   Be patient.

author: Nick Harrison | posted @ Tuesday, October 28, 2008 8:29 PM | Feedback (0)

Thoughts on a Data Dictionary



Thoughts on a Data Dictionary

I have often thought about what would make a good data dictionary.   Usually I have lamented the absence of any meaningful data dictionary beyond what the DBMS itself keeps on its own.   If everyone would simply add comments everywhere that the database natively allows, we would all be oh so much better off.   But alas we don’t.

As is so often the case, if we don’t have to do something, we probably won’t.    Often times, it’s a matter of meeting deadlines, sometimes it’s a matter of shear laziness, but sometimes, it may just be a case of not understanding the need or not knowing what is intrinsically available.

Let’s tackle the need first…

Database objects rarely have meaningful names.   Database objects with meaningful names to one group may not be meaningful to another group.   When you are trying to learn a new data model, the history of this data model is useful to understand the conventions adopted or ignored.

In its simplest form, I have several goals for a data dictionary:

·        Define the naming conventions followed
·        Identify known exceptions to these conventions
·        Provide an English description for each database object (table, column, stored proc, trigger, etc)
·        Document the accepted data types and their purposes
·        Identify known exceptions to the data type rules
In Oracle or SQL Server, this can all be accomplished with a relatively small word document and the native data dictionary.

I think though that to actually enforce these conventions, we need a little extra support:

·        A Data Thesaurus
·        A rule validator similar to fxCop
The data thesaurus would be used to ensure that appropriate naming conventions are used.   Proposed object names would be split on a case change and run through the thesaurus.   If the individual words are found in main line entries, you are good to go.   If an individual word is found as an alternate, replace it with the main line entry and proceed.   If an individual word is not found in the thesaurus, flag it for review.

Such a process would eliminate such confusing names as:

·        ProcessDate
·        ProcessDte
·        Process_Date
The first name would work as entered.   The second one would be converted to the first one, and the third entry would be rejected since Process_ was not found in the thesaurus.   The key thing here is that it tracks and enforces the convention at the time of object creation not at the end of the process.

The above is one rule based on the data thesaurus.   Other rules that may be useful might include:

·        Insure that object names are not plural.   Each record in the “Members” table is a Member
·        Insure that each table name is a noun
·        Ensure that every column is either a noun or an adjective.   Columns should not be verbs
·        Ensure that every table has an associated comment
·        Ensure that every column has an associated comment
·        Ensure that every stored procedure has an associated comment
·        Ensure that stored procedures pass the thesaurus test
·        Ensure that stored procedures have the form ActionObject, such as InsertMember or SelectMembersByGroupID
Or

·        Ensure that stored procedures have the form NounAction such as MemberInsert or MemberSelectByGroupID

 

 

What are your thoughts on a data dictionary?   Has anyone incorporated similar ideas into one?   How did it work out?   Have you thought about and decided against?

author: Nick Harrison | posted @ Thursday, October 16, 2008 8:21 PM | Feedback (2)

Model View Presenter / Passive View Benefits



Model View Presenter / Passive View Benefits

A hot topic near and dear to my heart is Model View Presenter (MVP) or Passive View design pattern.   This design pattern forms a good model for keeping a strong separation between UI logic and business logic.

In the MVP model, the data access objects constitute the model, the View is the UI components kept as simple as they can be and the Presenter is where the business logic is.   These three components interact with other through interfaces to further reinforce the logical separation.  

The presenter should have no idea where the Model gets its data or how the data is persisted.    The Model defines an interface that must be implemented.   Anything that implements this interface can serve as the model.  This means that we can switch out the data storage strategy without having to change anything in the presenter.

The presenter should also have no idea about the nature of the View.    The View implements an interface that exposes all of the methods, properties, and events that the Presenter will need and the Presenter implements the business logic through this interface.   This means that View may actually been a Win Form, Web Form, a server control, a user control, a SharePoint web part, or even a class library with no UI components.   The only requirement is that the View must implement the interface.

Among other things, this gives us these benefits:

·        Simplified testing.   The Presenter can be tested without having to worry about the database or the UI.   Both the Model and View can be replaced with “Mock” objects designed to simplify testing
·        Business logic implemented in the Presenter can easily be retargeted to any number of UIs.    Once the business logic is implemented and tested, the original View can be converted from a Win Form to Web Form or a Mobile Web Form, etc without impacting the Presenter as long as the new Views continue to implement the same interface
·        The Model can be switched out to for demo purposes to read from local flat files or static XML files.   As long as the new Models implement the original interfaces, neither the Presenter nor the Views needs to change.
·        If you have multiple lines of business with similar business logic but very different data sources, new Models can be implemented targeting the new data sources.   As long as the new models implement the original interfaces, neither the Presenter nor the View needs to change.  
Future posts will explore what some of these benefits mean as well as ferret out some additional benefits.

What benefits do you see with the MVP pattern?  What hurdles have you had to face?

 

author: Nick Harrison | posted @ Monday, September 22, 2008 6:44 PM | Feedback (2)

What Can We Do For Meaningful Variable Names?




What can we do for meaningful variable names?

You hear much about naming conventions and some shops still have some arcane rules in place.   Fortunately most of us have stepped beyond Hungarian notation.   I have seen some shops define rules that the method name should include the number of parameters.   I have seen shops define rules that would ban overloading and insist on a sequence number for  methods that should be overloads.   I have seen shops that wanted the Requirement Number embedded in method names, and I have seen shops put maximum character size limits to simplify reading.   This unfortunately led to awkward abbreviations.

None of these standards have led to more meaning variable names.   The fact that a variable is an integer or a string does nothing for me.   Having a method signature come like:

        Public void int  DoSomething (string strParam, int intParam)

does nothing for us.    Having one like:

        Public void int  ProcessIncomingClaims (string branchCode, int transactionID)

Can be very enlightening.

Like legislating morality, none of these rules will guarantee that the names are good.   At the same time, almost any set of “good” rules could still be applied and end up with “bad” variable names.  We may not always know when a name is “good” but we usually know when the name is “bad”.  

Enter GhostDoc.   GhostDoc is a wonderful tool designed to help automate your comments.   Caution it works best for C#, but they are building support for VB.   The intention here is that GhostDoc takes your method, properties, parameters, etc. and splits them to come up with the documentation for that item.   There are several thinks that seem to be happening here.   First it splits the words at a case change.   Second, it interprets methods as being in the form verb – object and will attempt to structure the comment like that.

For instance, if you have a method called DataBind (), GhostDoc will come up with the comment:

/// <summary>

/// Datas the bind.

/// </summary>

 

This is clearly not what we want for a comment.   Note that the naming convention was not followed.   We would want to have methods following the pattern of Vern Object.   Here it is following the convention of Object Verb.   Ghost Doc ends up with a comment that does not make sense grammatically.   Now consider the comment generated for the method   BindData ():

/// <summary>

/// Binds the data.

/// </summary>

Our method name follows the naming conventions that we have setup and now the generated comment makes sense as well.

Try it and you will find that whenever GhostDoc does not come up with a boiler plate comment, your variable names or method names or property names are probably not named well.

Not a bad litmus test for good names!

Just like Ghost Writers in the publishing world, there is only so much that GhostDoc can do.  It does a good job of structuring your comments.  It does a good job of mapping out the details needed for comprehensive XML documentation.   It also does a good job brining in ancestor details when overriding from a base class.  

It does not explain how logic works or why logic is needed.   This you still need to add yourself, but at least you will know where it goes to produce in comprehensive documentation.     The better you name your methods and parameters, the less you should have to write.    The more highly cohesive your methods, the less you should have to write.    The less you have have to write manually, the better your code can be understand without your comments.

Has anyone else used GhostDoc to help ensure naming conventions or help ensure that code is readable?

?


You hear much about naming conventions and some shops still have some arcane rules in place.   Fortunately most of us have stepped beyond Hungarian notation.   I have seen some shops define rules that the method name should include the number of parameters.   I have seen shops define rules that would ban overloading and insist on a sequence number for  methods that should be overloads.   I have seen shops that wanted the Requirement Number embedded in method names, and I have seen shops put maximum character size limits to simplify reading.   This unfortunately led to awkward abbreviations.

None of these standards have led to more meaning variable names.   The fact that a variable is an integer or a string does nothing for me.   Having a method signature come like:

        Public void int  DoSomething (string strParam, int intParam)

does nothing for us.    Having one like:

        Public void int  ProcessIncomingClaims (string branchCode, int transactionID)

Can be very enlightening.

Like legislating morality, none of these rules will guarantee that the names are good.   At the same time, almost any set of “good” rules could still be applied and end up with “bad” variable names.  We may not always know when a name is “good” but we usually know when the name is “bad”.  

Enter GhostDoc.   GhostDoc is a wonderful tool designed to help automate your comments.   Caution it works best for C#, but they are building support for VB.   The intention here is that GhostDoc takes your method, properties, parameters, etc. and splits them to come up with the documentation for that item.   There are several thinks that seem to be happening here.   First it splits the words at a case change.   Second, it interprets methods as being in the form verb – object and will attempt to structure the comment like that.

For instance, if you have a method called DataBind (), GhostDoc will come up with the comment:

/// <summary>

/// Datas the bind.

/// </summary>

 

This is clearly not what we want for a comment.   Note that the naming convention was not followed.   We would want to have methods following the pattern of Vern Object.   Here it is following the convention of Object Verb.   Ghost Doc ends up with a comment that does not make sense grammatically.   Now consider the comment generated for the method   BindData ():

/// <summary>

/// Binds the data.

/// </summary>

Our method name follows the naming conventions that we have setup and now the generated comment makes sense as well.

Try it and you will find that whenever GhostDoc does not come up with a boiler plate comment, your variable names or method names or property names are probably not named well.

Not a bad litmus test for good names!

Just like Ghost Writers in the publishing world, there is only so much that GhostDoc can do.  It does a good job of structuring your comments.  It does a good job of mapping out the details needed for comprehensive XML documentation.   It also does a good job brining in ancestor details when overriding from a base class.  

It does not explain how logic works or why logic is needed.   This you still need to add yourself, but at least you will know where it goes to produce in comprehensive documentation.     The better you name your methods and parameters, the less you should have to write.    The more highly cohesive your methods, the less you should have to write.    The less you have have to write manually, the better your code can be understand without your comments.

Has anyone else used GhostDoc to help ensure naming conventions or help ensure that code is readable?

author: Nick Harrison | posted @ Monday, September 22, 2008 6:43 PM | Feedback (1)

Reflecting on Software Metrics


Reflecting on Software Metrics

 

At the risk of sounding pedantic, I like software metrics. They can prove to be invaluable in analyzing source code. To be clear, I am not proposing that developers be bonused based on metrics and their use in estimating and scheduling should be limited at best. I am however a fan of software metrics as benchmarks for evaluating design and understanding how software works.

There is an add-in for reflector called CodeMetrics that allows you to easily calculate a wide range of code metrics from within Reflector. We are most interested in one metric, Cyclomatic Complexity.

Cyclomatic Complexity or CC is the number of logical paths through a piece of code. This can be used to define a lower bound for the number of test cases needed to ensure proper coverage in unit testing. This can also be used to predict ongoing maintenance costs and be scaled to predict the likelihood of actually resolving a particular defect or introducing additional defects.

Tables similar to this are common:

CC

Testing Requirements

Ongoing Maintenance Costs

Resolution Likelihood

1-0

A simple procedure

Low

High

11-50

Complex

Moderate to High

Medium to low

>50

Very Difficult

High Risk

Low

The actual bands may vary depending on the line of business and skills of the developers involved, but these are generally good benchmarks.

A CC of 10 would indicate that 10 test cases are needed to fully test all of the functionality. Depending on what you are working on, that may be more complex than you want to consider or that may still be fairly trivial. As a general rule, I try to keep methods below 10.

When I am working on existing functionality, I try to review the code and refactor the code in the same module with the highest complexity, systematically reducing the overall complexity. Sometimes the best you can do is to review the more complex functions and add comments on how they may be refactored. Often times, the actual refactoring may be out of scope for the change that got you involved in the first place.

It is important to review the complexity of any code before making code changes. If you are about to change code that already has a high complexity, you want to make sure that your changes do not add more complexity. You need to be aware that with higher complexities comes a higher chance of introducing new defects and you are also less likely to resolve the underlying defect if it is embedded in complex code. This may change your estimates and should change your testing strategy.

Sometimes, complex code masks cohesion problems. If the code is very complex, there is a good chance that the particular method is trying to do more than one thing. If it is doing more than one thing, than simplifying the code may be nothing more than separating all of the different tasks that the method is trying to do. For example, code is often complex because data validation logic is blended with business logic or data mapping logic is blended with business logic. Simply pulling all of the data validation logic to a separate method and all of the data mapping logic to a separate method and isolating all of the business logic to a single method, you end up with several methods each of which is less complex than the original. Each of these methods will be much more cohesive. With cohesive methods, it is easier to know where to add new functionality or where to go when reviewing existing functionality.

Future blogs will explore some ways to reduce complexity.

author: Nick Harrison | posted @ Saturday, September 20, 2008 10:10 PM | Feedback (1)

Passive View and Cyclomatic Complexity



Passive View and Cyclomatic Complexity

We have previously talked about cyclomatic complexity and set forth the goal of keeping the complexity for our methods down below 10.   We have also talked about the goal of systematically refactoring any methods that we find to lower complexities whenever we make code changes.  These are good and admirable goals for business logic.

But what about Passive View?   Passive View is a slight variation on the Model View Presenter pattern designed to drive home the fact that the view should do very little processing.   The view should be passive.  Quiet literally the only code found in the view should be the implementation of the properties and the event handlers for the UI components.  The properties should do the absolute minimal to interact with the user, and the event handlers should simply forward calls on to the presenter.

I cannot stress this enough, there should be no actual logic in the view.

The driving force behind the Passive View pattern is to simplify testing.   Since it is harder to test the UI and the UI does not lend itself to automated testing, we want to move as much logic as possible to locations that are easier to test, class libraries.   Testing aside, logic housed in class libraries is more reusable and can be shared across multiple UIs.   In fact, the automated tests simply become a new UI.

All that being said, any method in the UI should have a cyclomatic complexity of 1!

Yep!   You heard me correctly.  One!   If you find yourself writing a method, event handler or property in the UI with a complexity greater than one, stop and ask yourself why whatever logic you are implemented is in the UI.   Chances are you will find that there is functionality that should be moved to the Presenter or a shared UI component.

I challenge everyone to identify UI code with a complexity greater than 1 that cannot be moved out of the View.   Note that I said out of the View and not out of the UI layer.   There are times when true UI processing may require some logical processing, but these are not so specialized that they cannot be removed from the View.

 

 

author: Nick Harrison | posted @ Saturday, September 20, 2008 10:04 PM | Feedback (1)

Pronouns



Using Pronouns

This may not be very technical, but hopefully it may generate some discussion and provide some insight.

Pronouns can be very enlightening about our attitudes towards work.   How long do you work for a company before you start thinking of the company as “we” instead of “they?”   Consider:   We are the number 1 producer of product X.   Vs.   They are the number 1 producer of X.   Do you ever make that pronoun switch?   What does it mean if you switch back from “we” to “they?”    What does it mean if you never make the switch?   Are you even aware of which one you use?

The dynamics with interpersonal relationships are also interesting.   Have you ever noticed that a single co –worker will probably say “I” or “my”, or “me” a lot.    A co- worker married or at least in a committed relationship will be much more likely to say “we” even if their partner is not with them.   What should we make of this?  : )

Of course you also get this with team dynamics as well.  A strong team player will frequently refer to “we.”    In this case, “we” probably refers to the team.   We are falling behind and need to catch up.   In a strong team, “I” rarely makes an appearance.    Hopefully, “you” is scarce as well.

I have also noticed something interesting with my wife that I thing is somewhat common.   The switch from “our” or even “mine” to “your” when we get upset about something.   I notice “our” dog becoming “your” dog when the dog does something bad.    Is this common?   Does it show up in other areas of life?    Do “our” processes become “your” processes when we feel like the processes don’t work?   Would such switches be a Freudian slip, malicious behavior, or a harmless word game?   Are we even of it when we make such switches?

So your use of pronouns can be used gauge the ties you feel towards your employer, your team, and your partner.    Here are some of my thoughts:

·        When your employer becomes “we” as in “We have a good work environment” instead of “They have a good work environment”, you finally feel like you are part of the corporate culture.
·        Should you go back to thinking of your employer as “they”, you may be feeling a little disenfranchised or disconnected.   Consider, “They have a strong business model” instead of “We have a strong business model”
·        If you never think of the company as “we,” are you perpetually disconnected looking in on your co-workers from the outside?
·        When you meet someone and they keep referring to “We went to the movies over the weekend”, chances are that person is married.    If your new friend states that “I went to the movies over the weekend,” you may have a new single friend.
·        When your team mates start referring to “we” as in “We have a good sense of the project timelines”, you probably have a strong team.   As long as you are still thinking in terms of “I think I know what needs to be done”, your team probably hasn’t truly formed yet.  Or maybe it is still “storming”
These are not hard and fast rules.   These are probably not even generally guidelines.   These are simple observations that I have made that seem to be true often.

Without a doubt there are many exceptions.   Most of us choose our pronouns without even thinking about it.   If we start thinking about it too much, we will probably throw the whole system out of balance, but a little self reflection is a good thing.

author: Nick Harrison | posted @ Saturday, September 20, 2008 10:03 PM | Feedback (1)

Why we Blog!


I was recently challenged by a very good friend that the contnet of my blog was only really clear to people who did not need to read it.   His comments challenged me on several fronts.   

  • Why do I post blogs?   
  • Who do I believe my target audience is?  
  • What do I hope this audience gets from the blogs that I post?

There are several reasons why I post blogs.   Sometimes I post a blog so that I can quickly find something that I struggled to figure out in the first place.    In this case, the target audience is myself, and I hope to make it easier for me the next time that I do something similiar.   If others find something useful there, that is great, but they are not necessarily the target audience.   I admit that these blogs are rare, or atleast I hope that they are rare.   Probably more blogs are written with me as the target audience than I intend.

Sometimes I post blogs with the intention of giving back to the community and helping others have an easier transition in learning something new, or encouraging someone to venture into new technologies that they may otherwise be intimidated by.   In such cases, the target audience is someone new to DotNet or someone who has not really explored much beyond what was needed to get specific jobs done.    This is where I probably have failed my audience the most in the past.  The content may not always be as readily accessible as I would intend.

Sometimes I post blogs with more of a "Look at me, ain't I smart" attitude.   I am ashamed to admit that this is more common than I wish and probably leads to the problems with many posts not being easily accessible to who I believe my target audience is.   I challenge anyone reading my blogs to help keep me inline.   Just be nice about it.

My friend recommended a format that he thought would make the content more accessible to a broader audience by focusing on:

  • What does this mean
  • How will this help me
  • How do I get started 
     

This sounds like sound criticism and good advice.   I challenge all of you who do post blogs to think about the three questions that I struggled with at the begining.  

  • Why do I post blogs?   
  • Who do I believe my target audience is?  
  • What do I hope this audience gets from the blogs that I post?

How would you have to answer these questions?   What do you think of my firend's suggested format?

author: Nick Harrison | posted @ Wednesday, March 05, 2008 9:23 AM | Feedback (2)

Debugging Attributes



Debugging Attributes

Have you ever been debugging code and examined the properties of an object? You get both the public properties and the private member variables!   Why is that?   Can this be avoided?

This seems to violate the principles of encapsulation.  It also complicates the process of examining the contents of an object at run time.   You don’t really want to have to look at all of the private member variables which by definition should be irrelevant to the task of debugging.

Fortunately the violations to encapsulation pose limited risk.   Even though the debugger will reveal hidden member variables, you still cannot access them in your code.

The confusing part is when you view an object’s detail and you have to filter through potentially twice as many variables as there are properties.

How do you remove this potential confusion in the objects that you create?   Enter the System.Diagnostics.DebugBrowsable attribute:

 Add a DebuggerBrowsable attribute to private member variables.

[System.Diagnostics.DebuggerBrowsable (System.Diagnostics.DebuggerBrowsableState.Never)]

Now these variables will not show up when you do a Quick Watch.   This will clean out the entries from the Quick Watch that you did not intend to be visible.

 

Another debugging frustration is stepping into methods or properties that you are no longer interested in.

Before c# 3.0 introduced implied properties, business entities could be particularly frustrating since they often included several very simple properties that you never had any need to step into.

I would often try to step into a method and have to step into several properties’ get accessors to pass their values as parameters.

Not only is this annoying, it is also potentially confusing and time consuming as the debug bounces around in irrelevant code as you are trying to figure out why an errant method is not behaving as you expect.

If only there was a way to instruct the debuger to ignore certain pieces of code …

Turns out that there is!

[System.Diagnostics.DebuggerHidden]  Causes the debugger to completely ignore the method.

[System.Diagnostics.DebuggerStepThrough] The debugger will not step into the method but will honor breakpoints in the method.

[System.Diagnostics.DebuggerNonUserCode] This attribute is similar to combining the first two attributes.   This is intended for generated code or designer code.  

Since these are attributes, you have to pay attention to the AttributeUsageAttribute.

DebuggerHidden and DebuggerNonUserCode can be tied to properties.    DebuggerStepThrough cannot.   It can be applied to methods though.   This means that it would have to be applied to the get and set individually for a property and not to the property as a whole.

The DebuggerStepThrough attribute causes some problems for CodeDom since there is no way to associate attributes with the individual methods that make up a property.  Use the DebuggerNonUserCodeAttribute instead.

 

author: Nick Harrison | posted @ Tuesday, February 19, 2008 8:58 PM | Feedback (0)

Conditional Attribute



ConditionalAttribute

Certain methods especially in the System.Diagnostics namespace are decorated with ConditionalAttributes similar to this:

[Conditional("DEBUG")]

What is this all about?  The conditional attribute provides a nice way to have code be conditionally ignored.  Methods decorated with this attribute will not have their calls compiled to IL unless the condition is true.

In the attribute shown above, any calls to the method will not be made unless the symbol DEBUG is defined.

This creates some very intriguing possibilities.  Some of which might be nice and some of which might be pure evil.

Code like this:

#define TEST
using System;
public class MyClass
{                
   public static void Main()
   {
      #if (TEST)
         Console.WriteLine("TEST is defined");      
      #endif
   }
}


Can be rewritten to code like this:

 

    #define TEST
    public class MyClass
    {

        public static void Main()
        {
            TestWriteLine("Test is defined");
        }

        [System.Diagnostics.Conditional("TEST")]
        public static void TestWriteLine(string message)
        {
            Console.WriteLine(message);
        }
   }
Is this better?   Consider this, you would have to wrap every method call the “if test compiler directives” to achieve the same effect of adding the attribute to one place.

If you are only calling the method from one place, this probably doesn’t matter, but if you are potentially calling this method from throughout multiple enterprise applications, this is unrealistic.

The compiler directives can cause confusion by interrupt the logic flow.  

Here are some examples where this might be very useful and practical

Suppose you wanted to be able to support a build that could be used for code coverage testing without having to change code for a production build.

Suppose you wanted to have a build to support performance metrics without having to change any code.

Suppose you wanted to have a build to support debugging information without having to change production code.

Turns out, this last example is the whole reason this attribute was created.   This is how Debug.WriteLine works.    

Try this, sprinkle your code with calls System.Diagnostics.Debug.WriteLine(),  compile in debug mode, and examine the assembly with Reflector.   You will notice that all of your calls to WriteLine are there.   Now compile in release mode and examine the assembly with Reflector.   You didn’t change any code, but now none of the WriteLine statements are there.

We could create a method decorated with a conditional attribute requiring that the symbol CodeCoverage must be defined and then call this method at the start of every function.   Compile the code defining this symbol.   Now when we run through a round of testing, we will get a list of every method that was called.    Recompile the code without defining this symbol and the overhead of logging function calls goes away.

As is often the case, there are a couple of caveats.  This is not documented on MSDN anywhere that I have seen.   Any method decorated with this attribute must return void and cannot have output parameters.  This is intended to prevent the removal of this code from causing any unintended side effects.  You would not want to rely on a variable being initialized by a function that may or may not be called.  

How could you envision using such attributes?  

 

 

author: Nick Harrison | posted @ Tuesday, February 19, 2008 8:56 PM | Feedback (0)

Why Developers should like Refactoring!


The refactoring book http://www.amazon.com/exec/obidos/ASIN/0201485672 is really one of the best books that I have seen on programming.   If you are new to programming, I highly recommend giving the book a read.   Learning some of these refactors and thinking about them as you write code will help you write better code.   Being familiar with the various refactors will help you quickly identify ways to improve code.   The concept of code smells is pure genius.

The concept of refactoring is very liberating and freeing.  You don’t have to worry about getting everything right the first time.   You never will.  Refactoring provides a general framework for not only reversing the inevitable deterioration of good code into bad code, trust me it happens.  It also provides a framework to move code that started out not so good into better code.

No one sets out to write bad code, but still it happens.  

Sometimes requirements change during development.

Sometimes requirements are not well understood leading to a bad design from the outset.    

Sometimes you simply run out of time and do the best you can knowing that it is not as you would like to write, but having to do it anyway.  

Sometimes you are learning a new technology and simply don’t know any better.    It’s dangerous writing software while best practices are still emerging.

Sometimes multiple people are working on the same thing and not everyone understands the design, assuming that there was an original design.  

Sometimes new functionality is “bolted on” awkwardly.  

Sometimes incremental changes skew the code base in awkward directions.

There are many reasons bad things can happen to code software.   When you see software that has gone bad, be careful casting blame.  There is no telling how bad your code may look over time.  Let’s face it; most code simply doesn’t age well.

Refactoring helps us limit how long we have to live with our mistakes, helps us improve code over time, and stop the deterioration.

Future blogs will explore some of the refactors that covered in this pivotal book.   What are some of your experiences with refactoring?

author: Nick Harrison | posted @ Wednesday, January 09, 2008 9:07 PM | Feedback (0)