unused variables

Posted on 2011-05-02
Last Modified: 2012-05-11
I am writing a program to identify all the unused variables in the code...

how can i achieve that... becoz we cannot say the variable which is not repeated twice as unused
becoz the unused variable may be in comments...

so any idea... i am using c sharp
Question by:shragi
    LVL 40

    Accepted Solution

    If you plan to write such a program, you need to properly parse C# syntax, so that means your program will ignore comments and not be confused by variables in a comment.

    So proper code analysis for this (this is data flow analysis) would involve what we call a du-chain (declaration - usage chain).
    --Compute the line that the variable is declared
    --Compute the lines where the variable is used

    Unused variables will have a d but no u
    Local variables that are used will have separate d-u lines
    Public members, etc. may be used all over the place, and may be static, out of order, in other files. This will be your challenge.

    Start with a proper C# parser.
    LVL 18

    Expert Comment

    How are you reading the files?  Streamreader?

    Looking for multiple uses of the variable sounds like a good start.  When counting the times a variable is used, you'll have to ignore commented lines.  How you do that will depend on how you are reading the files.
    LVL 29

    Assisted Solution

    About working with code, from my own experience :). mrjoltcola told already about analyzing separate lines. That's what I did. I read text into a list of strings, each line is a separate items. E.g., in this case you may check easily if a line, e.g., starts from comment mark "//" etc. Or if a variable name is after comment mark on this line.
    About unused variables.
    1. Line where variable is declared should not have assignments ("=" sign, I believe)
    2. There is no assignment in the following code (in not commented out part)

    It's not to hard to check if this variable is class level variable or a local method variable. In the later case you need to scan the method only while in the firs case you need to scan entire class from the declaration. For big classes it may be an issue (we have classes with up to 25000 lines - stupid but fact :))..

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now