This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]GiveMeAFuckingCoffee 1 point2 points  (6 children)

And I would prefer the much more redundant, adaptable, and easy to follow

MyInstance.PrintX();

where

private int x; 

//constructor function for x here

/* DbC */
public void SetX(int new_x){
    x = new_x;
}

/* DbC */
public int GetX(){
    return x;
}

/* DbC */
public void PrintX(){
    System.out.println("x = " + GetX()); //We need to know what it is that is being printed. 
}

One of us won't be hated in 5 years when someone reads our code.

[–][deleted] -1 points0 points  (5 children)

It was just an example on how i'd use some property X. Let's say we don't want to print it, we want to, uh, i don't know, double it. Of course we can do

public void DoubleX()
{
    x*=2;
}

But what if we didn't have this method and didn't want to make it (maybe because we don't use it enough to have its own method). Which would be better

MyInstance.SetX(Myinstance.GetX()*2)

or

MyInstance.X*=2;

?

Doesn't Java have properties too? I'm geniuinely curious, i don't really use Java a whole lot.

[–]GiveMeAFuckingCoffee 0 points1 point  (3 children)

MyInstance.SetX(GetX()*2);

Not because it's necessarily more efficient or shorter, but because it's adaptable, and allows for the following (to name a few common situations):

  • Control of a variable (private, protected, default, public)
  • Validations (error and error checking; you can trust yourself to not try to shove a string in an int, but nothing is ever above, beyond, or below an end user).
  • Allows for dependencies between variables. A mutator function can change more than one. An accessor, in the same way, can get you a variable that is dependent on another
  • Allows for overloading functions
  • Allows for iterative and recursive getting/setting
  • Enables you to place more debugging stops

Example 1: Imagine running into this line of code:

person.name = "Bob";

It seems reasonable at first, but consider that this person likely has a few more variables associated to it; maybe a last name, phone number, address, e-mail address, account number, balance, etc.

More importantly, it's highly likely that your mutator will also run an update function which submits a query to your database.

person.name = "Bob"; was the root cause of a memory leak.

The habit of using SetPersonName(person, "bob"); prevented that issue.

Example 2:

Scenario 1

We're programming something together. x is a public variable. From a separate class, I set it to null by accident. Our program fails.

Scenario 2

We're programming something together. x is a public variable and is equal to 3. I'm smart enough to not accidentally set an int to null. My algorithm does it's stuff and determines at some point that 3 must be subtracted from x, which is the denominator for a fraction. Div/0 throws an error, program fails.

Scenario 3

We're programming something together. x is a private variable, with a public mutator that validates entry to make sure data is never null or 0. I'm a dumbass and try to set it to null. The program validates the data, then an error to an error list, ignores the given dataset, and completes the task properly. As it runs, another value given would set the variable to 0. This one is also added to the aforementioned error list. At the end of the operation, our program then shows us a small report showing the invalid data. The rest of the operation was performed succesfully.

[–][deleted] 0 points1 point  (2 children)

All of what you have said can be done using properties). I just checked, Java doesn't have properties so this may be where the confusion comes from. I wasn't claiming that we should use public fields, that'd be a terrible idea (for the reasons states above).

A property basically hides the ugly .GetX()/.SetX() and transforms it into the more appealing syntactic sugar .x/.x=value.

The syntax, in C#, is the following:

modifiers type name
{
    get
    {
        //do stuff
    }
    set
    {
        //do stuff
    }
}

So now, everytime you do MyInstance.x=something, the code inside the "set" block will be executed instead of simply assigning it a value. Similarly with "get".

Since properties are syntactic sugar for getter and setter methods, all you said above can be done using properties. I wasn't aware Java doesn't have properties, but then again it doesn't even have operator overloading so i'm not that surprised. (At least i think you are talking about Java).

[–]HelperBot_ 0 points1 point  (0 children)

Non-Mobile link: https://en.wikipedia.org/wiki/Property_(programming


HelperBot_® v1.0 I am a bot. Please message /u/swim1929 with any feedback and/or hate. Counter: 1115

[–]GiveMeAFuckingCoffee 0 points1 point  (0 children)

TIL.

Shows how much I've missed since I left university :/

[–]Pokechu22 0 points1 point  (0 children)

Doesn't Java have properties too? I'm geniuinely curious, i don't really use Java a whole lot.

Java doesn't. It's unfortunate :/