omer727
omer727

Reputation: 7565

How watch values doesn't affect the environment

When debugging in IDE, how does the IDE know how to calculate the watch value without changing the environment (writing to file, writing result to DB)?

Watch Expression Screenshot

Upvotes: 0

Views: 26

Answers (1)

Thomas Weller
Thomas Weller

Reputation: 59302

Your observation cannot be generalized. An IDE typically makes changes during debugging, especially if a property has a side effect.

Visual Studio

The following C# code:

using System;

namespace EvaluateChangesValue
{
    class Program
    {
        static void Main()
        {
            var program = new Program();
            Console.WriteLine(program.Value);
            Console.ReadLine();
            Console.WriteLine(program.Value);
            Console.ReadLine();
        }

        private int member;

        private int Value => member++;
    }
}

Set a breakpoint at the first ReadLine(), then add program.Value to the watch window and see how the value gets increased due to the member++ statement.

Visual Studio changed value

Eclipse

In Java and Eclipse, it's a bit harder to make the same proof because for these reasons:

  1. In Java it's more clear whether you call a method or access a field.
  2. You need the "Expressions" window, which is not available by default
  3. Re-evaluation needs user interaction

The code is similar to C#:

public class Program {

    public static void main(String[] args)
    {
        Program p = new Program();
        System.out.println(p.member);
        System.console().readLine();
        System.out.println(p.member);
        System.console().readLine();
    }

    private int member;

    public int getMember()
    {
        return member++;
    }
}

And the screenshot:

Changed value in Eclipse

Upvotes: 1

Related Questions