<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: asuna24</title>
    <description>The latest articles on DEV Community by asuna24 (@asuna24).</description>
    <link>https://dev.to/asuna24</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1160428%2F278b42bf-6db8-4bd9-8adf-0acc5f874648.jpeg</url>
      <title>DEV Community: asuna24</title>
      <link>https://dev.to/asuna24</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asuna24"/>
    <language>en</language>
    <item>
      <title>Back to basic : Flutter widget lifecycle</title>
      <dc:creator>asuna24</dc:creator>
      <pubDate>Wed, 06 Mar 2024 16:51:33 +0000</pubDate>
      <link>https://dev.to/asuna24/back-to-basic-flutter-widget-lifecycle-3844</link>
      <guid>https://dev.to/asuna24/back-to-basic-flutter-widget-lifecycle-3844</guid>
      <description>&lt;p&gt;Flutter is a cross platform framework to build seamless user interface across all platforms, from web to mobile apps. Flutter introduces &lt;code&gt;Widget&lt;/code&gt; as its main UI builder, we are living in the world of digital and reactive apps, so we need to understand how does Flutter app manage its &lt;code&gt;Widget&lt;/code&gt; lifecycle.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick review : Flutter's Widget
&lt;/h2&gt;

&lt;p&gt;Based on Flutter official documentation, "Flutter widgets are built using a modern framework that takes inspiration from React. The central idea is that you build your UI out of widgets. Widgets describe what their view should look like given their current configuration and state."&lt;/p&gt;

&lt;p&gt;So Widget is simply the object that build every piece of UI for Flutter. Each of widgets can have their own configuration and state, so if we build a page that consists of more than one widget, every widget will have their own lifecycle.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9jldn3sl7ndxztdioei8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9jldn3sl7ndxztdioei8.png" alt="Image description" width="800" height="522"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Widget Lifecycle
&lt;/h2&gt;

&lt;p&gt;Here are the order of Flutter widget's lifecycle : &lt;/p&gt;

&lt;h2&gt;
  
  
  1. Initialization
&lt;/h2&gt;

&lt;p&gt;This is the state where Flutter widget constructor is called. The object of the widget and state created. After the widget object is created, then it will call 2 kinds of framework functions, &lt;code&gt;initState()&lt;/code&gt; and &lt;code&gt;didChangeDependencies()&lt;/code&gt;. Details will be explained after this section.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Build
&lt;/h2&gt;

&lt;p&gt;After state of the widget initialized and the context is already exists, then this time Flutter widget will build the UI. &lt;code&gt;build(BuildContext context)&lt;/code&gt; framework function will be called here. The output of this step is the UI will be shown in our end user app.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. State changes and rebuild
&lt;/h2&gt;

&lt;p&gt;We know that the best app is an app that users can interact with. Flutter stateful widget have defined &lt;code&gt;state&lt;/code&gt; as a mutable value that can be changed by app's/user's behavior/interaction. This step, Flutter will use &lt;code&gt;setState()&lt;/code&gt;  framework function to change the state. After the state changes, the widget will be rebuilt with diffing algorithm to optimize build process.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Deactivate
&lt;/h2&gt;

&lt;p&gt;Deactivate is the state where a widget is removed from widget tree temporarily or permanently. For example, if we have a stream where at some point we will destroy that widget but we'll need to use it again later.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Disposal
&lt;/h2&gt;

&lt;p&gt;Disposal is the state where a widget is removed from widget tree permanently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Widget Lifecycle Framework Functions
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftw6fhbqe40t0vbqsm9wz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftw6fhbqe40t0vbqsm9wz.png" alt="Image description" width="800" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  createState()
&lt;/h3&gt;

&lt;p&gt;This method will create the State object under the widget. Usually this method exists on the widget if we create a stateful widget.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class StateLifecycleExample extends StatefulWidget {
  const StateLifecycleExample({super.key});

  @override
  State&amp;lt;StateLifecycleExample&amp;gt; createState() =&amp;gt; _StateLifecycleExampleState();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  initState()
&lt;/h3&gt;

&lt;p&gt;This method will be called only once when the state object is created for the first time. This method is executed before the &lt;code&gt;build()&lt;/code&gt; function called. So we could say that this method had no access to widget's &lt;code&gt;BuildContext&lt;/code&gt;. Usually we use this method to initialize some state's initial value or call some non-context related function to make sure those function ran on widget initialization process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;late int a,b,c;

@override
void initState() {
  a = &amp;lt;something manipulated&amp;gt;
  b = &amp;lt;something manipulated&amp;gt;
  c = &amp;lt;something manipulated&amp;gt;
  super.initState();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  didChangeDependencies()
&lt;/h3&gt;

&lt;p&gt;This method will be called after initState(). We could access &lt;code&gt;BuildContext&lt;/code&gt; here. Also, this method can be called multiple times based on dependencies that changed or object changes inside the widget. For example, if in one page we opened and closed a keyboard, then it would be counted as widget changed, so this method will be called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@override
void didChangeDependencies() {
  print('this is didChangeDependencies, context : ${context} already exists here');
  super.didChangeDependencies();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  setState()
&lt;/h3&gt;

&lt;p&gt;This method used to change state value and refresh the widget to show latest state of the widget. Fun fact, we don't have to put the changed value inside setState's callback function. So they would produce the same result. Why ? because setState will always rebuild the widget no matter how the callback function is.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = 10;
setState((){})

and

setState((){
  a = 10
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  deactivate()
&lt;/h3&gt;

&lt;p&gt;Just like the definition above, "Deactivate is the state where a widget is removed from widget tree temporarily or permanently. For example, if we have a stream where at some point we will destroy that widget but we'll need to use it again later."&lt;/p&gt;

&lt;h3&gt;
  
  
  dispose()
&lt;/h3&gt;

&lt;p&gt;A bit different with deactivate, &lt;code&gt;dispose()&lt;/code&gt; will be called only when the widget is removed from widget tree.&lt;/p&gt;

&lt;p&gt;On that, we can see that &lt;code&gt;deactivate&lt;/code&gt; and &lt;code&gt;dispose&lt;/code&gt; are talking about removing widget from widget tree. Deactivate called when a widget removed temporary/permanently. But &lt;code&gt;dispose&lt;/code&gt; called when widget removed permanently only. So if a widget is removed, &lt;code&gt;deactivate&lt;/code&gt; and &lt;code&gt;dispose&lt;/code&gt; will be called together.&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>widget</category>
      <category>indonesia</category>
    </item>
    <item>
      <title>Flutter/Dart tips : What is Mixin ? is it really helpful or more like useless ?</title>
      <dc:creator>asuna24</dc:creator>
      <pubDate>Sun, 03 Mar 2024 16:11:37 +0000</pubDate>
      <link>https://dev.to/asuna24/flutterdart-tips-what-is-mixin-is-it-really-helpful-or-more-like-useless--2fk7</link>
      <guid>https://dev.to/asuna24/flutterdart-tips-what-is-mixin-is-it-really-helpful-or-more-like-useless--2fk7</guid>
      <description>&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;For those who already learned about Object Oriented Programming must have heard about "inheritance". Where inheritance defined as "a mechanism that allows a class to inherit properties and behaviors from another class". &lt;br&gt;
Example : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0h8clv3h7jqwmralgnb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0h8clv3h7jqwmralgnb.png" alt="Image description" width="800" height="565"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above, we have 1 Vehicle parent class which had 2 child classes, SUV and Sedan. Each of them have 2 kinds of car and each of them had some shared features (turbo usage, rear camera, and hybrid system).&lt;/p&gt;

&lt;p&gt;OOP concept teaches us to achieve these kind of needs we can just create &lt;code&gt;Turbo&lt;/code&gt;, &lt;code&gt;Camera&lt;/code&gt;, and &lt;code&gt;Hybrid&lt;/code&gt; class and implement their attributes and functions. Then each of those classes will be inherited based on class who needs that functionality.&lt;/p&gt;

&lt;p&gt;But in Dart, every class is limited only able to inherit to 1 class. For example, &lt;code&gt;Civid&lt;/code&gt; which already inherited Sedan class cannot inherit &lt;code&gt;Turbo&lt;/code&gt; and &lt;code&gt;Camera&lt;/code&gt; class to fulfill their features.  Or, we just implement turbo, camera, and hybrid system inside all classes that had those features. But eventually this approach can be so frustating to manage because of there are many code duplications for the same functionality. Here comes the usage of Dart's Mixin.&lt;/p&gt;

&lt;p&gt;Mixin enables us to create a way to share reusable code between classes but not to inherit them. So Mixin is not a class, its just a way to implement things. It has no constructor, or even attributes. It consists of reusable method only.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to create a mixin ?
&lt;/h2&gt;

&lt;p&gt;If we want to use inheritance, we can use &lt;code&gt;extends&lt;/code&gt; in the class definition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Sedan extends Vehicle {...}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want to use mixin, we need to create them by using &lt;code&gt;mixin&lt;/code&gt; key without any constructor. Just define the method there. To use mixin, we need to put them on our class definition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create mixin
mixin Turbo {
  void useTurbo() {
    print('I\'m using turbo, speed increased');
  }

  void stopTurbo() {
    print('I\'m stopping the turbo');
  }
}

mixin RearCamera {
  void useRearCamera() {
    print('I\'m using rear camera');
  }

  void turnOffCamera() {
    print('I\'m turning off the camera');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After defining our mixin, we need to create the class and use key &lt;code&gt;with&lt;/code&gt; after the class definition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class A with Turbo, RearCamera { ... }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Vehicle {
  final int numberOfWheels;
  final int maxSpeed;
  final String color;

  Vehicle({
    required this.numberOfWheels,
    required this.maxSpeed,
    required this.color,
  });

  void start() {
    print('I\'m starting');
  }

  void drive() {
    print('I\'m driving');
  }
}

class Sedan extends Vehicle {
  final bool isHatchback;

  Sedan({
    required super.numberOfWheels,
    required super.maxSpeed,
    required super.color,
    required this.isHatchback,
  }) : super();
}

mixin Turbo {
  void useTurbo() {
    print('I\'m using turbo, speed increased');
  }

  void stopTurbo() {
    print('I\'m stopping the turbo');
  }
}

mixin RearCamera {
  void useRearCamera() {
    print('I\'m using rear camera');
  }

  void turnOffCamera() {
    print('I\'m turning off the camera');
  }
}

mixin HybridSystem {
  void useElectricEnergy() {
    print('I\'m using hybrid system');
  }

  void stopElectricEnergy() {
    print('I\'m stopping the hybrid system');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we create the grandchild classes here using inheritance and mixing :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Civid extends Sedan with Turbo, RearCamera {
  Civid({
    required super.numberOfWheels,
    required super.maxSpeed,
    required super.color,
    required super.isHatchback,
  }) : super();
}

class Innovi extends SUV with Turbo, RearCamera, HybridSystem {
  Innovi({
    required super.numberOfWheels,
    required super.maxSpeed,
    required super.color,
    required super.isFourWheelDrive,
  }) : super();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By defining classes above, we can see that Civid inherits Sedan but since Civid had Turbo and RearCamera feature, then it uses those mixin. The same applied to Innovi which had Turbo, RearCamera, and HybridSystem. By using this approach, we are reducing code duplication for the exact same functionality.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void main() {
  final Civid cividObject = Civid(
    numberOfWheels: 4,
    maxSpeed: 200,
    color: 'blue',
    isHatchback: true,
  );
  // Civid can use turbo and rear camera only
  cividObject.useTurbo();
  cividObject.useRearCamera();

  final Innovi innoviObject = Innovi(
    numberOfWheels: 2,
    maxSpeed: 200,
    color: 'red',
    isFourWheelDrive: true,
  );
  // Innovi can use turbo, rear camera, and hybrid system
  innoviObject.useTurbo();
  innoviObject.useRearCamera();
  innoviObject.useElectricEnergy();
  innoviObject.stopElectricEnergy();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Dart as an Object Oriented Programming language enabled us to use inheritance with only 1 class inherited. What if we need more functionality for our class but we're afraid to create many code duplication for the same usage for a class ? Mixin is the answer. Mixin could be super helpful if we have some reusable code that can be used across classes that need the exact same functionality. &lt;/p&gt;

&lt;p&gt;Thank you!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Flutter/Dart Tips - Enhanced enums</title>
      <dc:creator>asuna24</dc:creator>
      <pubDate>Mon, 26 Feb 2024 18:16:18 +0000</pubDate>
      <link>https://dev.to/asuna24/flutterdart-tips-enhanced-enums-h64</link>
      <guid>https://dev.to/asuna24/flutterdart-tips-enhanced-enums-h64</guid>
      <description>&lt;p&gt;Have you ever used enum before ? Enum defined as "a type of data where only a set of predefined values exist". In Dart, enum is a special kind of class that used to represent constant values.&lt;/p&gt;

&lt;p&gt;For example, we want to have predefined constant value that represent something. Lets say we want to have a type to define rainbow color.. as we know, rainbow colors consist of red, orange, yellow, green, blue, indigo, and violet. Each of these colors can have its &lt;code&gt;Color&lt;/code&gt; value, label, and index.&lt;/p&gt;

&lt;p&gt;With normal enum type, we can create something like this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum RainbowColor {
  red, 
  orange, 
  yellow, 
  green, 
  blue, 
  indigo, 
  violet
}

extension RainbowColorExt on RainbowColor {
  static Map&amp;lt;RainbowColor, Color&amp;gt; mapRainbowToColor = {
    RainbowColor.red: Colors.red,
    RainbowColor.orange: Colors.orange,
    RainbowColor.yellow: Colors.yellow,
    RainbowColor.green: Colors.green,
    RainbowColor.blue: Colors.blue,
    RainbowColor.indigo: Colors.indigo,
    RainbowColor.violet: Colors.purple
  };

  Color getColor() {
    return mapRainbowToColor[this]!;
  }

  static Map&amp;lt;RainbowColor, String&amp;gt; mapRainbowToLabel = {
    RainbowColor.red: 'Red',
    RainbowColor.orange: 'Orange',
    RainbowColor.yellow: 'Yellow',
    RainbowColor.green: 'Green',
    RainbowColor.blue: 'Blue',
    RainbowColor.indigo: 'Indigo',
    RainbowColor.violet: 'Violet'
  };

  String getLabel() {
    return mapRainbowToLabel[this]!;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we created &lt;code&gt;RainbowColor&lt;/code&gt; enum with its defined extension and functions. To receive the label and color, we can do something like this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const RainbowColor red = RainbowColor.red;
final String redLabel = red.getLabel();
final Color redColor = red.getColor();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But you see that we need to connect our enum by using extension to get its corresponding label and color.&lt;/p&gt;

&lt;p&gt;Since Dart 2.17, they showed us new way to provide something like above.. by using Enhanced Enum.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enhanced Enum
&lt;/h2&gt;

&lt;p&gt;Dart also allows enum declarations to declare classes with fields, methods, and const constructors which are limited to a fixed number of known constant instances.&lt;/p&gt;

&lt;p&gt;This mean that we can create an enum to just like creating a special class that consist of some constants inside. See below :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum RainbowColor {
  red(Colors.red, 'Red'),
  orange(Colors.orange, 'Orange'),
  yellow(Colors.yellow, 'Yellow'),
  green(Colors.green, 'Green'),
  blue(Colors.blue, 'Blue'),
  indigo(Colors.indigo, 'Indigo'),
  violet(Colors.purple, 'Violet');

  const RainbowColor(Color this.color, String this.label);
  final Color color;
  final String label;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to get corresponding color and label, we can just use this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const RainbowColor red = RainbowColor.red;
final Color redColor = red.color;
final String redLabel = red.label;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Other than that, we also can create a function for this specific enum :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum RainbowColor {
  red(Colors.red, 'Red'),
  orange(Colors.orange, 'Orange'),
  yellow(Colors.yellow, 'Yellow'),
  green(Colors.green, 'Green'),
  blue(Colors.blue, 'Blue'),
  indigo(Colors.indigo, 'Indigo'),
  violet(Colors.purple, 'Violet');

  const RainbowColor(Color this.color, String this.label);
  final Color color;
  final String label;

  // New Function
  String toString() {
    return 'This color is $label';
  }
}

const RainbowColor red = RainbowColor.red;
final Color redColor = red.color;
final String redLabel = red.label;

// return : "This color is Red"
final String result = red.toString();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This update really helped us to reduce our code and simplify our way to create enum for our Flutter/Dart App.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Enum helps us to predefine constant values to represent something in our app. Previously, to create some functionality for our enum we need to create an extension with longer way to achieve our needs. Since Dart 2.17, Dart introduced Enhanced Enum which can help us to reduce our code to define enum with its functionality. Just like how we can define class with attributes and functions.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>programming</category>
      <category>dart</category>
    </item>
    <item>
      <title>Does Creating a unit test for Private Functions worth it ?</title>
      <dc:creator>asuna24</dc:creator>
      <pubDate>Fri, 23 Feb 2024 19:02:24 +0000</pubDate>
      <link>https://dev.to/asuna24/does-creating-a-unit-test-for-private-functions-worth-it--4d2c</link>
      <guid>https://dev.to/asuna24/does-creating-a-unit-test-for-private-functions-worth-it--4d2c</guid>
      <description>&lt;p&gt;Unit tests are quite mandatory nowadays despite of how every engineers thought about it. It could improve the confidence level of a company about their software/feature quality even though sometimes quite painful for engineers to create a good unit tests.&lt;/p&gt;

&lt;p&gt;After working on so many projects that included unit tests inside, I literally created unit tests for any function I had created. Starting from public functions and also private functions. But not all programming language allow us to create unit test for private functions. One that I've found that we can create private function unit test is Golang, and most of other OOP language don't allow us to create unit test for private function. Recently, I questioned, why is that ? and how should we test that private function ?&lt;/p&gt;

&lt;h2&gt;
  
  
  Visible For Testing
&lt;/h2&gt;

&lt;p&gt;I just found out that some programming language enables us to give an annotation that tells "theres a function that exists only for testing". Which we can use to test private function in OOP language. Example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#### Dart

class GeneralUtilities {
  int res = 0;

  void foo() {
    _add(1);
  }

  void _add(int value) {
    res += value;
  }

  @visibleForTesting
  int getPreviousNumber() {
    return _getPreviousNumber();
  }

  int _getPreviousNumber() {
    return res - 1;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example above enables us to call &lt;code&gt;getPreviousNumber()&lt;/code&gt; outside, but we will get a warning which said &lt;code&gt;The member 'getPreviousNumber' can only be used within '&amp;lt;package name&amp;gt;' or a test.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Therefore, we can use this as our way to create a unit test for private function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void main() {
  test('GeneralUtilities.getPreviousNumber() tests', () {
    final GeneralUtilities gu = GeneralUtilities();
    gu.foo();
    final int res = gu.getPreviousNumber();

    ## returns 0
    expect(res, 0);
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  But is that worth it ?
&lt;/h2&gt;

&lt;p&gt;After some comparison, I would say that this is not worth it. Why ? &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Being in a big-scale software codebase is pretty though, we should really focus on what matters. So what really matters about all of this functions ? The most important thing is, does this utility function works as it needed or not ? Does the client who would use this function can get the right answer/data or not ? So instead of focusing ourselves on testing private functions, we should focus on testing our public function that will use our private function also. By focusing on public function unit tests, we can focus on what matters and still creating a good quality software.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using @visibleForTesting would make our class dirty. Because there are some functions that exists only for testing, and that really doesn't make sense. Functions inside class should do something about that class, not fulfilling the needs for unit testing. Unit testing should be done based on the contract of that class.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sometimes I feel that creating a unit test for private functions are needed when the size of a public function is very big. Lets say we had 3-5 if conditions inside a public function and each condition will call another private function. It really sucks right to create a super big unit tests for that public function ? But in my opinion, a good public function is a function that only does one thing, not many things together. So instead of thinking how we should create the unit test for that function, we should think first, does this function accept Single Responsibility Principle or not ? Does this function built in the right way ?&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Unit tests are needed. We should focus on what matters, instead of trying to create a unit test for private function, try to create a simple public function that calls private function which not more than 1 responsibility.&lt;/p&gt;

</description>
      <category>dart</category>
      <category>programming</category>
      <category>testing</category>
    </item>
    <item>
      <title>Journey to Astro - #1 Pre-Condition</title>
      <dc:creator>asuna24</dc:creator>
      <pubDate>Sat, 30 Sep 2023 14:18:19 +0000</pubDate>
      <link>https://dev.to/asuna24/journey-to-astro-1-pre-condition-1ndh</link>
      <guid>https://dev.to/asuna24/journey-to-astro-1-pre-condition-1ndh</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh9063u5ywa28sxljs72h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh9063u5ywa28sxljs72h.png" alt="Image description" width="800" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Saya sudah hampir 6 bulan mendalami &lt;a href="https://nextjs.org/"&gt;Next.js&lt;/a&gt; yang berbasis React.js sebagai library utama dan didominasi oleh kekuatan Javascript didalamnya. Namun saat ini saya sedang mengeksplorasi bagaimana Astro bekerja, karena dari yang saya baca, Astro mampu memberikan performa yang jauh lebih baik dari framework web development manapun.&lt;/p&gt;

&lt;p&gt;Melalui postingan berkala ini, saya akan membagikan pengalaman saya dalam mendalami Astro sekaligus memberikan sedikit perbandingan dengan Next.js sebagai framework yang saat ini menjadi acuan utama para web developer.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;!!! Quick review&lt;/strong&gt;, website terdiri atas 3 komponen utama : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTML : Struktur halaman / DOM (Document Object Model)&lt;/li&gt;
&lt;li&gt;CSS : Styling DOM&lt;/li&gt;
&lt;li&gt;Javascript : Membangun interaksi/halaman dinamis&lt;/li&gt;
&lt;li&gt;React.js : salah satu library terkenal dengan berbasis component (&lt;a href="https://react.dev/"&gt;https://react.dev/&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Next.js : salah satu framework web development yang sedang naik daun (&lt;a href="https://nextjs.org/"&gt;https://nextjs.org/&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bagaimana React.js menampilkan sebuah webpage ?
&lt;/h2&gt;

&lt;p&gt;Proses utamanya terdiri atas 2 tahap, yakni Render &amp;amp; Hydration. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Render&lt;/strong&gt; adalah proses dimana React akan membangun DOM structure website anda sehingga anda sudah bisa melihat hasil akhir web nya secara tampilan namun belum bisa dilakukan interaksi apapun.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hydration&lt;/strong&gt; yaitu proses dimana React akan mencari DOM yang perlu dipasangkan Javascript untuk membangun interaksi yang dinamis.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Kondisi setelah Render :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6djk68bp2lr2ia4zizej.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6djk68bp2lr2ia4zizej.png" alt="Image description" width="780" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Kondisi setelah Hydration :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqb2rljs30ib8q35vcc4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqb2rljs30ib8q35vcc4.png" alt="Image description" width="800" height="509"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Kesimpulannya adalah bahwa React.js secara default akan melakukan proses Hydration, dan di Next.js proses Hydration ini pasti dilakukan di level halaman setiap mau menampilkan sebuah webpage.&lt;/p&gt;

&lt;h2&gt;
  
  
  "Wah" jadi agak lemot dong ya karena ada 2 proses ini ?
&lt;/h2&gt;

&lt;p&gt;Secara user menurut saya 2 hal ini tidak akan begitu terlihat.. hanya akan terlihat apabila memang halaman sebuah website menggunakan Javascript yang berlebihan. Contohnya mungkin website berbasis wordpress, atau web lain yang interaksinya kompleks &amp;amp; banyak hal yang dinamis disana.&lt;/p&gt;

&lt;p&gt;Lalu bagaimana solusinya ? Disinilah alasan mengapa &lt;strong&gt;Astro&lt;/strong&gt; hadir. &lt;strong&gt;Astro&lt;/strong&gt; menawarkan berbagai efisiensi yang bisa dilakukan untuk menghindari rendering javascript yang tidak dibutuhkan. Berikut adalah beberapa klaim mereka :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftvp9smg1q7qjpspmom76.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftvp9smg1q7qjpspmom76.png" alt="Image description" width="800" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Melalui postingan berkala ini saya akan menguak apakah memang &lt;strong&gt;Astro&lt;/strong&gt; memiliki performa sesuai dengan apa yang sudah mereka klaim.&lt;/p&gt;

&lt;p&gt;Mari kita lanjut ke part 2 :D&lt;/p&gt;

&lt;p&gt;Thank You~&lt;/p&gt;

&lt;p&gt;Yumna Pratista Tastaftian&lt;br&gt;
Product Engineer, Indonesia&lt;/p&gt;

&lt;p&gt;ref : &lt;br&gt;
&lt;a href="https://docs.astro.build/en/concepts/why-astro/"&gt;https://docs.astro.build/en/concepts/why-astro/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://softwaremill.com/astro-island-architecture-demystified/"&gt;https://softwaremill.com/astro-island-architecture-demystified/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.astro.build/en/concepts/why-astro/"&gt;https://docs.astro.build/en/concepts/why-astro/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>astro</category>
      <category>indonesia</category>
    </item>
  </channel>
</rss>
