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 179021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:20:00+00:00 2026-05-11T14:20:00+00:00

Let’s say I am tasked with coding some kind of an RPG. This means

  • 0

Let’s say I am tasked with coding some kind of an RPG. This means that, for example, I’ll want to track a Character GameCharacter and stats thereof, like intelligence, damage bonuses or hitpoints.

I’m positively scared that by the end of the project I may end up with handling with very a high number of fields – and for each I would have to make sure they follow a very similar set of constraint and behaviours (for example, I want them to be bounded between a min and a max; I want to be able to distinguish between a ‘base value’ and a ‘temporary bonus’; I want to be able to increment and decrement both without going through a setters and getters). Suddenly, for every field I would need one (two?) getter and four setters and maybe a couple resetters too! Even for 10 fields that means a LOT of methods all alike, eek.

For DRYness I have started encapsulating the logic of messing with those stats in Field classes, so that I could write code such as intelligence.applyBonus(10) or hitpoints.get() (which takes care the value returned is in range), etc. I have even gone to such a length to create classes to group those fields together, but that’s not the point right now.

Now, I hit in this problem while ‘plugging’ Field into GameCharacter: most Java textbooks say that each class should have private fields with public getters and setters. That sounds good in theory, and I’ve already built a whole class around an int; however, the idea doesn’t sound as solid when you find yourself calling a getter to get… a getter:

thisCharacter.getIntelligence().get() //eeek 

I’d much rather access the field directly. Maybe it’s my Python/VB [1] ‘background’, but for me it is cleaner, clearer and more straightforward:

thisCharacter.intelligence.get() 

The (theoretical) problem with public fields is that I’m giving up all control on it; for example at some other point in the codebase, by misfortune, the following might happen:

thisCharacter.intelligence = somethingThatReallyIsNull; 

Sounds like a subtle bug… but… I mean, should I really worry about that? I for one never plan to assign the Field directly [2], I have documented in the Javadoc that this is not something that should be done, but still I am new here so I’m a bit torn.

So I would like to hear what your take on this topic. Are the advantages of encapsulation so massive that I should go ahead and have getter getters and setter getters and so on… or should I take encapsulation in healthy measures and leave the Field as a public field?


[1] Yes, I know. I’ve been trying to forget. But we’ve just recently seen also a bit of C# and man, aren’t properties sweet. Oh well.

[2] except in Constructors! And a getter won’t save me from a faulty constructor.

  • 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. 2026-05-11T14:20:01+00:00Added an answer on May 11, 2026 at 2:20 pm

    My experience is that in situations where you need a lot of fields, the number, nature, naming, and types of fields are so flexible and likely to change throughout the lifetime of your project that you would likely need some sort of map instead of fields.

    For example have an attribute map from keys to values.

    Provide public calls for getting and setting the attributes, but don’t let everybody use them (or make sure they don’t). Instead, create classes to represent each attribute you are interested in, and that class provides all the functions for manipulating that attribute. For example, if you have Strength, you could have a ‘StrengthManipulation’ class that is initialized to a specific Player object, and then provides getters, setters (All with appropriate validation and exceptions), and perhaps things like calculating strength with bonuses, etc.

    One advantage of this is that you decouple the use of your attributes from your player class. So if you now add an Intelligence attribute, you don’t have to deal and recompile everything that manipulates only strength.

    As for accessing fields directly, it’s a bad idea. When you access a field in VB (at least in old VBs), you usually call a property getter and setter and VB simply hides the () call for you. My view is that you have to adapt to the conventions of the language that you are using. In C, C++, Java and the like you have fields and you have methods. Calling a method should always have the () to make it clear that it is a call and other things may happen (e.g., you could get an exception). Either way, one of the benefits of Java is its more precise syntax and style.

    VB to Java or C++ is like Texting to graduate school scientific writing.

    BTW: Some usability research shows that it’s better to not have parameters to constructors and rather construct and call all the setters if you need them.

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

Sidebar

Related Questions

Let's assume I want to deserialize this (I've removed the namespaces to make things
Let's say I have a javascript array with a bunch of elements (anywhere from
Let's say I have a dataset, which can be neatly classified using weka's J48
Let say I have two UIViews: View1: - bounds: 0, 0, 20, 20 -
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let give you the details of this rather odd issue. We have a very
Let's say I'm writing a Windows Forms (.NET Framework 3.5) application which shows the
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's suppose I've got a 2D vector template class: template<typename T> class Vec2 {
Let simplify the question: All I need is to explode() string by a comma

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.