Use of getter and Setter on Dart

Asked

Viewed 1,874 times

1

I can not understand the real need to use the Get/Set for the case below, which, even if I leave the variable commotion private, I could still modify it by using methods.

class casa {
  int comodos = 4;
}

void main() {
  casa casa1 = casa();
  casa1.comodos = 10;
  print(casa1.comodos);
}

1 answer

1


Dear @Aiken, if you do by the methods you can modify by methods you can treat the input, allowing only "correct" values for the class rule/logic. This is the basics of "visibility" in OO with variables (read on the visibility of variables at the end of the answer, in case you don’t know how to use in Dart).

You is who will define what can and cannot, if it is public can "all" (in case any value int valid/acceptable). Now imagine your intention to comodos were a range limited from 5 to 50, anyone could put 4 or less, 51 or more, as values if it is "public", if private and treat by a method set of life then an IF will be more than enough to solve the limit range rule, example:

import 'dart:io';

class casa
{
  int _comodos = 4;

  setComodos(int value) {
    if (value < 2 || value > 50) {
        throw("Numero de comodos inválidos");
    } else {
        _comodos = value;
    }
  }

  comodos() {
    return _comodos;
  }
}

void main() {
    casa casa1 = new casa();
    casa1.setComodos(10); //10 comodos
    print(casa1.comodos());
}

If you do so:

    casa casa1 = new casa();
    casa1.setComodos(100); //100 comodos
    print(casa1.comodos());

It will issue a custom Exception that states that the number of guest rooms is invalid, something like:

Unhandled exception:
Numero de comodos inválidos
#0      casa.setComodos (file:///home/brcontainer/prog.dart:9:6)
#1      main (file:///home/brcontainer/prog.dart:22:11)
#2      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:261)
#3      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:148)

About private (variable visibility) on Dart

Dart will treat variables as private as long as the class is in a separate lib and use underline/underscore as prefix if the class is declared in the same document of main(), see the documentation:

  • 1

    Dart doesn’t use getters and setters like Java, Dart uses getters and setters like C#. If later the programmer wants to add a validation rule, he can without having to refactor the code to receive the values through methods. I don’t believe there’s any reason to create getters and setters on Dart when you don’t need validation, maybe it was bad advice.

  • 2

    Dear @user140828 it wasn’t clear to me that the author was talking about special methods , it seemed to me that he spoke of Get and Set in the sense of setting by methods rather than directly special methods That would be something else, but since it’s not well described in the question whether it’s them or not then I won’t edit the answer yet. I did not understand about the bad advice, I explained the question of when need of a rule would make sense to use private, when there is no "rule" so there is no reason why.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.