Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3222100
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:57:08+00:00 2026-05-17T15:57:08+00:00

Context From The Pragmatic Programmer : Every piece of knowledge must have a single,

  • 0

Context

From The Pragmatic Programmer:

Every piece of knowledge must have a single,
unambiguous, authoritative representation within
a system.

Questions

  • How is that statement reconciled with directly setting a private member variable’s value throughout a class in multiple places?
  • Does it matter as there can be no external dependencies on the value?
  • Is it duplication to directly change private member variables that have public accessors in other places besides the accessor?

Example

Consider the following code:

public class Line {
  private boolean changed;
  private double length;
  private Point start;
  private Point end;

  public Line( Point p1, Point p2 ) {
    this.start = p1;
    this.end = p2;
    this.changed = true;
  }

  public void setStart( Point p ) { this.start = p; this.changed = true; }
  public void setEnd( Point p ) { this.end = p; this.changed = true; }
  public Point getStart() { return this.start; }
  public Point getEnd() { return this.end; }

  public double getLength() {
    if( this.changed ) {
      this.length = start.distanceTo( end );
      this.changed = false;
    }

    return this.length;
  }
}

Even though the changed variable is never exposed (through public acccessors or otherwise), the same line of code is essentially repeated four times: this.changed = true (thrice) and this.changed = false (once). Similarly, the assignment of this.start and this.end happens multiple times. As opposed to:

  public Line( Point p1, Point p2 ) {
    setStart( p1 );
    setEnd( p2 );
  }

  public void setStart( Point p ) { this.start = p; dirty(); }
  public void setEnd( Point p ) { this.end = p; dirty(); }

  public double getLength() {
    if( isDirty() ) {
      setLength( getStart().distanceTo( getEnd() ) );
      clean();
    }

    return this.length;
  }

The updated code is quite similar, but the duplication of all assignments is removed (presume dirty() and clean() use accessors). (There is a duplicated call to dirty() in the constructor that was not there before due to reusing the accessor methods for assignment.)

The question is not about whether this.changed = true is more readily understood as dirty().

Clarification

The question is about whether this.variable = value is a “piece of knowledge” and should therefore have a “single, unambiguous, authoritative representation” that is used consistently: a corresponding accessor. Thus the general case:

public class C1 {
  private Object v;

  public C1() {
    this.v = new C1();
  }

  public void m1() {
    this.v = new String();
  }

  public void m2() {
    System.out.println( this.v );
  }
}

versus:

public class C2 {
  private Object v;

  public C2() {
    setV( new C2() );
  }

  public void m1() {
    setV( new String() );
  }

  public void m2() {
    System.out.println( getV() );
  }

  private void setV( Object o ) { this.v = o; }
  private Object getV() { return this.v; }
}

In C1, the variable v is directly assigned in multiple places. In C2, the variable v is is directly assigned in a single spot. Even though, in both cases, v is completely private, does the C1 implementation duplicate a “piece of knowledge”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:57:08+00:00Added an answer on May 17, 2026 at 3:57 pm

    How is that statement reconciled with directly setting a private member variable’s value throughout a class in multiple places?

    There is a single private member variable. Therefore there is a single representation. The statements that change this representation are not representations themselves. Having multiple statements that access / change the representation is not the same as having multiple representations.

    Does it matter as there can be no external dependencies on the value?

    No.

    Is it duplication to directly change private member variables that have public accessors in other places besides the accessor?

    No.

    That doesn’t necessarily mean that it is a good idea to do it though.

    In your example, the choice is between accessing and updating a “dirty” flag directly or doing this via light-weight private methods. IMO, this boils down to a value judgment as to which approach gives you more readable code. And my feeling is that there is little difference between the two approaches, at least in this case. In other cases, there could be a stronger case for using internal methods to access / update private state that is never exposed.

    If the state needs to be exposed outside of the class then there is a strong case for declaring the variables private and providing getters and setters for other classes to use. And if those getters and setters have been declared, then you can make a (weaker) case that the class itself should use them.

    For those people who are concerned about the efficiency or otherwise of getters and setters in Java, the chances are that it will make no difference to performance. The JIT compiler in a modern JVM will almost certainly inline methods like clean(), dirty() and isDirty() resulting in machine instructions are equivalent to the case where you get and set the variables directly. Indeed, the latest JIT compilers will even inline non-final public methods when they can deduce that the methods don’t need to be dispatched.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some Context From Javascript: The Definitive Guide : When regexp is a global regular
I need to get the spring application context from a non bean object. In
I need to populate a context menu from a database at run time. I
In the context of this question link text is possible from a Controller that
From C#, is it possible to detect the number of context switches that occurred
I've got a foo.war file from a third-party vendor. I've defined a context in
I have some content from server using $.ajax() function. I try to replace some
I would like to serve a different content from a single URL depending on
Context: From my javascript web UI, I launch a long-running (several minutes) operation that
I create a context from an UIImage, and then I draw into it with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.