11 Replies - 1381 Views - Last Post: 16 September 2015 - 03:03 PM

#1 blueSCORCH  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 16-September 15

I don't understand the advantage of having class attributes privat

Posted 16 September 2015 - 06:15 AM

Hi

I've read in books and tutorials that class attributes should be private and that the best way of changing them is through a method (in C# think it's called a function in some other languages).

I think the reason was that it protects the attribute from being changed by things that shouldn't access it, but if you have a method that changes it anyhow I don't see how it's protected.

It just seems like this:

C.R = 5



would be the same as this

C.ChangeR(5)



Except you don't have to write a new method with the first way.

Also since we're writing the code anyway, I don't understand why we need to protect the attribute since we're the one writing the software its not like it would just access something unless we wrote it to do that.


Any insight into this would be much appreciated, it's really bugging me lol.

Cheers

Is This A Good Question/Topic? 0
  • +

Replies To: I don't understand the advantage of having class attributes privat

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6508
  • View blog
  • Posts: 14,381
  • Joined: 02-June 10

Re: I don't understand the advantage of having class attributes privat

Posted 16 September 2015 - 06:25 AM

There is a tutorial on classes linked in my signature block, where I explain the *why* of this. If it doesn't help, come back here and let me know and we can go over it some more.
Was This Post Helpful? 1
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: I don't understand the advantage of having class attributes privat

Posted 16 September 2015 - 06:29 AM

consider the following: what if you pass "a" instead of 5? does the code using this property still works with that value? Setters allow you to verify that a property value meets certain constraints. additionally, properties cannot be set in interfaces.
Was This Post Helpful? 1
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5930
  • View blog
  • Posts: 20,278
  • Joined: 05-May 12

Re: I don't understand the advantage of having class attributes privat

Posted 16 September 2015 - 08:31 AM

Actually, properties can be set in interfaces if the property is declared as part of the interface with a setter. Ex.
interface IFoo
{
   int Bar { get; set; }
}


Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6508
  • View blog
  • Posts: 14,381
  • Joined: 02-June 10

Re: I don't understand the advantage of having class attributes privat

Posted 16 September 2015 - 08:43 AM

Quote

I've read in books and tutorials that class attributes should be private and that the best way of changing them is through a method

Part of your confusion is lack of understanding because this is all new. A method doesn't have to be JUST a stand alone method.
Properties are at the heart of C#. They are like a smart merge of a field and a couple methods.

Here's a really short and simple reason - just one of any - why we use public properties to set private fields:

Because the set method within a property can do validation and keep you from breaking things.

Let's say the valid range for a property is 10-200. Why doesn't matter in this example: For some program someplace you're going to have such ranges.

If your remote calling class can just reach in and set any value it likes there is nothing stopping it from setting..
class SomeOtherClass
{
    public int w;
}
class LogicCode
{
   SOmeOtherClass.W = -14;// This is going to break down the line because its not in the valid range.
}



Now we use a property (which as we just said has built-in methods for get and set) - which will range check and correct bad incoming values


class SomeOtherClass
{
    private int m_w;
    public int W
    {
       get { return m_w; }
       set 
       {   
          if (value < 10) value = 10;
          if (value > 200> value = 200;
          m_w = value;
       }
     }
}
class LogicCode
{
   SomeOtherClass.W = -14; // The W property will range check and force this to a valid value
}


There is a tutorial on properties linked in my signature block. It should give you a lot more background and understanding on the topic.

This post has been edited by tlhIn`toq: 16 September 2015 - 08:46 AM

Was This Post Helpful? 1
  • +
  • -

#6 blueSCORCH  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 16-September 15

Re: I don't understand the advantage of having class attributes privat

Posted 16 September 2015 - 11:53 AM

Thanks for the help guys I appreciate it, the random number example makes a lot of sense, though I was wondering wouldn't this accomplish the same thing?

if (value < 10) Class.Property = 10;

if (value > 200> Class.Property = 200;

else Class.Property = Value;



Or is this not a good way of doing it?


tlhIn`toq I went through your class tutorial and it was great, though I might have to read over it a few times to get it to sink in. I know it's a little off topic but reading that tutorial I had another quick class question if you don't mind?

It's about deciding what should be a class, in that example I would've kept the ingredients as part of the dish class instead of its own before reading the tutorial. When you're deciding what should be it's own class do you have a rule you usually follow or is it just experience? It said "How small and self contained can I make these little Lego blocks of code?" but how do you know when you've gone small enough?
Was This Post Helpful? 0
  • +
  • -

#7 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: I don't understand the advantage of having class attributes privat

Posted 16 September 2015 - 12:02 PM

But then the client (caller code) has to do all the validation. You want separation of responsibilities, let the Class decide whether or not the new value is good enough, not you.

Experience is a lot of it. You don't want to go too far as to generate a load of redundant code, but you want to go far enough that your model accurately describes your situation. Generally a class should only be responsible for itself (one thing). If you find you have a lot of other code in there that isn't doesn't directly deal with the entity, then it should probably go somewhere else. If you go so far that you end up with classes that don't do anything, then you've probably gone too far.

For example a Car class should model the pieces of a Car (it's wheels, engine, which could themselves be other classes), it shouldn't however be going out and getting input from the user or accessing the network for some reason. A Car shouldn't need to do that, it should however be able to accelerate(), crash() etc.

This post has been edited by Ryano121: 16 September 2015 - 12:03 PM

Was This Post Helpful? 1
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: I don't understand the advantage of having class attributes privat

Posted 16 September 2015 - 12:03 PM

Quote

though I was wondering wouldn't this accomplish the same thing?

but it's optional. you would always have to remember to write that code whereever you want to set the value, which violates DRY.
Was This Post Helpful? 1
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6508
  • View blog
  • Posts: 14,381
  • Joined: 02-June 10

Re: I don't understand the advantage of having class attributes privat

Posted 16 September 2015 - 12:40 PM

View PostblueSCORCH, on 16 September 2015 - 12:53 PM, said:

It's about deciding what should be a class, in that example I would've kept the ingredients as part of the dish class instead of its own before reading the tutorial. When you're deciding what should be it's own class do you have a rule you usually follow or is it just experience? It said "How small and self contained can I make these little Lego blocks of code?" but how do you know when you've gone small enough?


The rule is... Go by your experience.

As for ingredients... Picture a larger more integrated system. Maybe there is a module for inventory control as well as recipies and ordering. If Ingredients is part of Dish, then Dish has to be part of the inventory system. I don't like that. But if Ingredients can exist seperately then it can be shared across various modules. Try not to look so much at how things exist IN ONE CASE, but also how they could look in four future case scenarios three years from now.
Was This Post Helpful? 1
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5930
  • View blog
  • Posts: 20,278
  • Joined: 05-May 12

Re: I don't understand the advantage of having class attributes privat

Posted 16 September 2015 - 12:50 PM

Sorry to contradict folks, but in my experience, there tends to be a duplication of code in the model and in the view (specially when you have a multi-tier system). The model does it's own input or state validation, but the view also has code that also does input or state validation. The reason for this is that the view doesn't have to go all the way to calling a web service to try to update the model only to get an error.

Basic example: you have a Bank account object that has a Withdraw() method. It does checks to ensure that someone doesn't pass in a negative value. On the UI side, you'll either have explicit code that also checks that the user does not enter a negative number in the textbox, or the UI indirectly sets the validation properties on the up-down control.
Was This Post Helpful? 1
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6508
  • View blog
  • Posts: 14,381
  • Joined: 02-June 10

Re: I don't understand the advantage of having class attributes privat

Posted 16 September 2015 - 02:37 PM

I don't think that contradicts anything.

You *can* write in a way as to validate at the lowest level.
You *can* write in a way as to validate at the highest level.
But those two things are not exclusive. You can also do both. You can expect that every other developer is an idiot, that the users are out to break the system, that some hacker is going to find a way to bypass as much as he can, that in the future a 3rd party program will be put in the mix even though its not on the roadmap today - and thus you write in a ROBUST manner. But with that you have to recognize that if you validate at low level, and validate at high level that when the rules change you have to change the validation in two places. In that situation I like to have a business rules layer. The model will check with the rules, the UI will check with the rules, so you have your robustness - and a single point of update.
Was This Post Helpful? 0
  • +
  • -

#12 blueSCORCH  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 16-September 15

Re: I don't understand the advantage of having class attributes privat

Posted 16 September 2015 - 03:03 PM

I think I understand it now guys, using methods and properties in the class/object means we can put code in the assignment to stop any values that it can't handle and any error handling I'm guessing if they do.

Which is better than writing the code to check for valid values outside of the class because that would then have to be wrote every time you assign a value to it.

This has been something that's proper confused me but you guys have explained it nicely cheers!

I'll try and think about how to break things down into classes better, though I guess that sort of knowledge comes with experience.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1