Skip to content

Commit

Permalink
[#228] [RF] Improve Cove Coverage. Describe magics
Browse files Browse the repository at this point in the history
  • Loading branch information
lyskouski committed Sep 5, 2023
1 parent c4b8c21 commit eb7fc8d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 56 deletions.
46 changes: 46 additions & 0 deletions docs/implementation-flow/ch01-s01-dart.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
% Copyright 2023 The terCAD team. All rights reserved.
% Use of this content is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.

\subsection{Dart}
\markboth{Bootcamping}{Dart}

\subsubsection{Overloading operators}

In Dart, "magic methods" are often referred to as "operator overloading" or "special methods." They allow you to define
custom behaviors for built-in operations:

\begin{itemize}
\item \q{toString} returns a string representation of an object, can be used for a serialization and deserialization
process of an object;
\item \q{call} allows an object to be treated as a function;
\item \q{hashCode} returns a hash code for an object (to use object as a key for \q{Map} and \q{Set});
\item \q{operator==} compares two objects for equality;
\item \q{get} and \q{set} -- to override the behavior of getting and setting properties.
\end{itemize}

\begin{lstlisting}
class Person {
// Required from constructor
String name;
// Post-initialization
late DateTime _createdAt = DateTime.now();
// var person = Person('Tom');
Person(this.name);
// person() // 'Hello from Tom!'
String call() => 'Hello from $name!';
// person.createdAt = DateTime(2023, 01, 01);
set createdAt(DateTime date) => _createdAt = date;
// print(person.createdAt); // 2023-01-01 00:00:00
DateTime get createdAt => _createdAt;
// print(Person('Tom') == Person('Terry')); // false
@override
bool operator ==(Person other) => other.name == name;
// person = Person.fromString('Tom');
factory Person.fromString(String name) {
return Person(name);
}
// print(person); // 'Tom'
@override
String toString() => name;
}
\end{lstlisting}
2 changes: 2 additions & 0 deletions docs/implementation-flow/ch01-s01-dart.tex-dump.tex
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
\subsection{[TBD] Dart}

Question: Flutter check that variable is a function

Answer: In Flutter, you can use the is operator to check if a variable is a function.
Expand Down
58 changes: 2 additions & 56 deletions docs/implementation-flow/ch01-s02-flutter.tex
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
% Copyright 2023 The terCAD team. All rights reserved.
% Use of this content is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.

\markboth{Bootcamping Flutter}{Bootcamping Flutter}

[TBD]
\subsection{Flutter}
\markboth{Bootcamping}{Flutter}


\subsubsection{Asserting Functions Definition}
Expand Down Expand Up @@ -39,59 +38,6 @@ \subsubsection{Ignoring Function Arguments}
\end{lstlisting}


\subsubsection{Applying Getters and Setters for Class Variables}

By migrating our data structures to class objects it can be used additionally getter and setter for class variables.
That will help us to cover, as an example, \q{description}-attribute for Budget structures by showing
"Spent Amount / Total Budget", and for \q{details}-attribute - "Left Amount".

\begin{lstlisting}
class MyClass {
String _name; // Private variable

// Getter for name
String get name => _name;

// Setter for name
set name(String value) {
_name = value;
}
}

void main() {
MyClass myObject = MyClass();

// Using the setter
myObject.name = 'John';

// Using the getter
print(myObject.name); // Output: John
}
\end{lstlisting}

In the example above, the \q{MyClass}-class has a private variable \q{\_name}. The getter for \q{\_name} is defined
using the get keyword, and the setter is defined using the set keyword. The getter returns the value of
\q{\_name}, and the setter sets the value of \q{\_name} to the provided value. By using that approach we can control
the access to class variables and perform additional logic or validation when getting or setting their values.

One more question, is how to set \q{DateTime.now()} inside a constructor as default value, since, by defining it inside
the constructor, an error would be taken: The default value of an optional parameter must be constant.

So, to set a default value for a DateTime variable as the current date and time, we can use the \q{DateTime.now()} method
during the initialization of the variable.

\begin{lstlisting}
class MyClass {
DateTime _createdAt;

MyClass({DateTime? createdAt})
: _createdAt = createdAt ?? DateTime.now();

DateTime get createdAt => _createdAt;
}
\end{lstlisting}


\subsubsection{Using cast to resolve dynamic structures}

Migration our Bills to classes construction has lead to an error on .firstWhere when Account of Budget not found.
Expand Down

0 comments on commit eb7fc8d

Please sign in to comment.