What are "watches" in IntelliJ and how to use them? -
when debug app, in debug tool window there watches window. have read manual on , over, cannot find practicle usage of watches.
somehow, think cool , useful tool , lack not using it.
can explain when should use , give few samples? ideally, description bound concrete (imaginary) situation better apply in work.
this section allows define expressions you'd see how evolve/change every step of debug process, without manually inspecting available objects , properties. let's take following simple sample intentionally throws npe:
public class watchsample { static class student { public static final int credits_required_for_graduation = 10; private string name; private integer credits; public student(string name, integer credits) { this.name = name; this.credits = credits; } string getname() { return name; } public boolean hasgraduated() { return credits >= credits_required_for_graduation; } public integer getcredits() { return credits; } } public static void main(string[] args) throws exception { list<student> students = simulatereadingfromdb(); (student student : students) { if (student.hasgraduated()) { system.out.println("student [" + student.getname() + "] has graduated [" + student.getcredits() + "] credits"); } } } private static list<student> simulatereadingfromdb() { list<student> students = new arraylist<>(3); students.add(new student("s1", 15)); students.add(new student("s2", null)); // <- simulate mistake students.add(new student("s3", 10)); return students; } }
at point in time may wonder how come npe , needs fixing. set breakpoint, add few watches , step through lines. you'll end troublemaker in sight:
of course basic example , should taken such. within regular app you'll have more complex scenarios , expressions you'd inspect, , make more sense, example: if (((position > 0 && position < max) || (position < 0 && position > min) && (players(currentplayer).isnotdead() && move.isallowed()) && time.notup())....
. in case can evaluate sub-expressions see 1 returns false
note: make breakpoint conditional program pause when specific event occurs:
Comments
Post a Comment