<?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: Rishikrishna</title>
    <description>The latest articles on DEV Community by Rishikrishna (@rishikrishna_r).</description>
    <link>https://dev.to/rishikrishna_r</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%2F779650%2F8552fd51-a1e6-4a57-ae3d-f509260cff15.jpg</url>
      <title>DEV Community: Rishikrishna</title>
      <link>https://dev.to/rishikrishna_r</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rishikrishna_r"/>
    <language>en</language>
    <item>
      <title>Method References in Java</title>
      <dc:creator>Rishikrishna</dc:creator>
      <pubDate>Thu, 26 May 2022 13:35:54 +0000</pubDate>
      <link>https://dev.to/rishikrishna_r/method-references-in-java-fl4</link>
      <guid>https://dev.to/rishikrishna_r/method-references-in-java-fl4</guid>
      <description>&lt;p&gt;Before we get to method references, I would highly encourage you to read my &lt;a href="https://dev.to/rishikrishna_r/lambda-expressions-in-java-2b0c"&gt;lambda expressions&lt;/a&gt; article.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Method References?
&lt;/h2&gt;

&lt;p&gt;When a lambda expression does nothing but call an existing method, it's often clearer to use that method by name. Method references are compact, easy-to-read lambda expressions that already have a name.&lt;/p&gt;

&lt;p&gt;Consider this class Car:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Car{
  String modelName;
  int modelNum;
  LocalDate registrationDate;
  String vehicleNum;             

  public String getModelName(){..}

  public void printCar() {..}

  public static int compareByModelName(Car c1, Car c2){
    return c1.getModelName().compareTo(c2.getModelName());
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose you want to sort an array of car instances by the modelName, you could define a class that implements the comparator interface and then sort like so,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class CarModelNameCompartor implements Comparator&amp;lt;Car&amp;gt; {
  public static int compare(Car a, Car b) {
    return a.getModelName.compareTo(b.getModelName());
  }
}

Arrays.sort(cars, new CarModelNameComparator());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or you could make use of the simple lambda expression:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Arrays.sort(cars, (a, b) -&amp;gt; 
  a.getModelName().compareTo(b.getModelName());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, our class has already implemented a method that can compare cars by modelName.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Arrays.sort(cars, (a, b) -&amp;gt; Car.compareByModelName(a, b));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a lambda expression has only one statement that calls to another method, we can directly use or &lt;code&gt;refer&lt;/code&gt; that method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Arrays.sort(cars, Car::compareByModelName);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Method References and Lambda expressions
&lt;/h2&gt;

&lt;p&gt;Method references are semantically the same as the method being called inside lambda expression body &lt;code&gt;(a, b) -&amp;gt; Car.compareByModelName(a, b)&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The parameter list is copied from the Comparator compare method.&lt;/li&gt;
&lt;li&gt;Its body calls the compareByModelName method passing in those parameters.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Types of Method References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Static method reference&lt;/li&gt;
&lt;li&gt;Instance method reference&lt;/li&gt;
&lt;li&gt;Instance method reference of an arbitrary object of a particular type&lt;/li&gt;
&lt;li&gt;Constructor reference
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class MethodReferencesExample {
  public static &amp;lt;T&amp;gt; T merge(T a, T b, BiFunction&amp;lt;T,T,T&amp;gt; merger) {
    return merger.apply(a, b);
  }

  public static String appendStringsStaticMethod(String a, String b) {
    return a + b;
  }

  public String appendStringsInstanceMethod(String a, String b) {
    return a + b;
  }

  public static void main(String args[]) {
    String one = "Hello";
    String two = "World";

    // Referring a static method
    System.out.println(merge(one, two, MethodReferencesExample::appendStringsStaticMethod));

    //Referring an instance method
    MethodReferencesExample obj = new MethodReferencesExample();

    System.out.println(merge(one, two, obj::appendStringsInstanceMethod));

    //Referring an arbitrary object method of a particular type
    System.out.println(merge(one, two, String::concat));
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In all of the method references above, the same &lt;code&gt;HelloWorld&lt;/code&gt; output will be generated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reference to a Constructor
&lt;/h3&gt;

&lt;p&gt;A Constructor can be referenced the same the way as referencing a static method, like &lt;code&gt;HashSet::new&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static List&amp;lt;Car&amp;gt; getObjects(int length, Supplier&amp;lt;Car&amp;gt; supplier){
  List&amp;lt;Car&amp;gt; list = new ArrayList&amp;lt;Car&amp;gt;();

  for(int i = 0; i &amp;lt; length; i++){
    list.add(supplier.get());
  }

  return list;
}

List&amp;lt;Car&amp;gt; cars = getObjects(5, Car::new);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similar lambda expression for &lt;code&gt;Car::new&lt;/code&gt; would be &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;() -&amp;gt; { return new Car(); }&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;As mentioned above, if a lambda expression only calls an existing method then using method reference can make code more readable and clear. &lt;/p&gt;

&lt;p&gt;Thanks for reading the blog. Feel free to provide inputs and suggestions for any areas of improvement. :)&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Lambda Expressions in Java</title>
      <dc:creator>Rishikrishna</dc:creator>
      <pubDate>Mon, 23 May 2022 12:42:40 +0000</pubDate>
      <link>https://dev.to/rishikrishna_r/lambda-expressions-in-java-2b0c</link>
      <guid>https://dev.to/rishikrishna_r/lambda-expressions-in-java-2b0c</guid>
      <description>&lt;p&gt;In Java, we all know we can pass data as arguments, but what about passing functionality as an argument? In this tutorial, we'll see just how to do that. Let me explain that with an ideal use case.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ideal use case
&lt;/h2&gt;

&lt;p&gt;Suppose you are building a car tracking application for the country, and you want to display all cars of a particular state.For example Tamil Nadu, the vehicle number of a car contains 'TN' in it. &lt;/p&gt;

&lt;p&gt;One would represent a Car 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;public class Car {
  String modelName;
  int modelNum;
  LocalDate registrationDate;
  String vehicleNum;             

  public String getModelName(){..}

  public void printCar() {..}

  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Approach 1: Create methods for the search criteria.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void printCarsIn(List&amp;lt;Car&amp;gt; cars, String state){
 for(Car c : cars) {
  if(c.getVehicleNum().contains(state)){
    c.printCar();
  }
 }
}

printCarsIn(cars, "TN");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You are checking each instance of the cars list to see if it's vehicle number contains 'TN' in it. If so, you print it.&lt;/p&gt;

&lt;p&gt;This approach is more likely to make your application brittle. What if you update Car class so that it contains different members or perhaps different data types for its members. Or what if the search criteria is changed. You have to rewrite a lot of code in all of the defined methods. The search criteria can instead be separated into a different class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 2: Specify search criteria in a local class.&lt;/strong&gt;&lt;br&gt;
The following class checks whether an instance of &lt;code&gt;Car&lt;/code&gt; is from Tamil Nadu.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface CheckCar {
  boolean test(Car c, String state);
}

class CheckCarsInState implements CheckCar {
  public boolean test(Car c, String state) {
    return c.getVehicleNum().contains(state);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you can create an instance of this &lt;code&gt;CheckCarsInState&lt;/code&gt; class and use it to check the criteria.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void printCarsIn(List&amp;lt;Cars&amp;gt; cars, String state, CheckCar tester){
  for(Car c : cars){
    if(tester.test(c, state)){
      c.printCar();
    }
  }
}

printCarsIn(cars, "TN", new CheckCarsInState());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although we have separated the search criteria, we have additional code for declaring a new class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 3: Using an anonymous class&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An &lt;a href="https://www.programiz.com/java-programming/anonymous-class#:~:text=A%20nested%20class%20that%20doesn,of%20the%20anonymous%20class%20%7D%3B%20%7D"&gt;anonymous class&lt;/a&gt; can be used if only one instance is required from a class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface CheckCar {
  boolean test(Car c, String state);
}

public static void printCarsIn(List&amp;lt;Cars&amp;gt; cars, String state, CheckCar tester){
  for(Car c : cars){
    if(tester.test(c, state)){
      c.printCar();
    }
  }
}

//method call
printCarsIn(cars, "TN", new CheckCar() {
  public boolean test(Car c, String state) {
    return c.getVehicleNum().contains(state);
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But still, the anonymous class looks bulky considering the &lt;code&gt;CheckPerson&lt;/code&gt; interface contains only one method. We can instead use a lambda expression.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 4: Lambda expression to pass functionality&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;CheckPerson&lt;/code&gt; is a functional interface (an interface with only one abstract method). Lambda expressions can be used against functional interfaces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface CheckCar {
  boolean test(Car c, String state);
}

public static void printCarsIn(List&amp;lt;Cars&amp;gt; cars, String state, CheckCar tester){
  for(Car c : cars){
    if(tester.test(c, state)){
      c.printCar();
    }
  }
}

printCarsIn(cars, "TN", (c, state) -&amp;gt; c.getVehicleNum().contains(state));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With lambda expressions, you specify the criteria for which you want to search when calling the function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax of Lambda expression&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parameter list: A list of parameters enclosed in parentheses. The parentheses can be avoided if only one parameter is used.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;c -&amp;gt; c.printCar();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Arrow token, &lt;code&gt;-&amp;gt;&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Body: The body can either be a block of statements or an expression. If it is a single expression, the evaluation will be returned. Alternatively, you can use a &lt;code&gt;return&lt;/code&gt; statement like so,&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;c -&amp;gt; {
  //...
  return c.getVehicleNum();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note: A single return statement is not an expression, so it can't be used without braces.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading the blog. Feel free to provide inputs and suggestions for any areas of improvement. :)&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>java</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Html Forms and different Form controls.</title>
      <dc:creator>Rishikrishna</dc:creator>
      <pubDate>Wed, 02 Feb 2022 09:18:04 +0000</pubDate>
      <link>https://dev.to/rishikrishna_r/html-forms-and-different-form-controls-l0e</link>
      <guid>https://dev.to/rishikrishna_r/html-forms-and-different-form-controls-l0e</guid>
      <description>&lt;p&gt;In Web development, Forms are one of the main interfaces between users and the application or website. It allows users to provide data to the website which will be stored or processed in the backend. Examples of where we can use forms are, to enter payment information, feedback forms, etc.&lt;/p&gt;

&lt;p&gt;Essentially, form element has two attributes, action and method. The action attributes contains a url to which the data must be sent for processing. The method attributes contains the http verb. About &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods"&gt;http verbs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Basic Markup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form action="/" method="POST"&amp;gt;

&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Form Controls
&lt;/h2&gt;

&lt;p&gt;Form controls are used inside the form tag, that can collect various types of data from users. Some form controls used are dropdown lists, buttons, checkboxes and mostly &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt;. They can be implemented to accept values in specific format and can be associated with labels to provide meaning to both sighted and visually impaired users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input&lt;/strong&gt;&lt;br&gt;
The input element is one of the mostly used form control. It contains &lt;code&gt;type&lt;/code&gt; attribute that denote the type of data it can accept.Examples are &lt;code&gt;text&lt;/code&gt;, &lt;code&gt;email&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;date&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label for="name"&amp;gt;Enter name:&amp;lt;/label&amp;gt;
&amp;lt;input type="text" id="name" name="user name"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a label to be associated with an input, the input's &lt;code&gt;id&lt;/code&gt; must match the label's &lt;code&gt;for&lt;/code&gt;. &lt;br&gt;
The &lt;code&gt;name&lt;/code&gt; attribute acts a reference to the data after the form is submitted. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Email address field&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;&amp;lt;input type="email" name="email" id="email"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This input with type &lt;code&gt;email&lt;/code&gt; allows user to enter an email with basic in-built validation. Additionally, &lt;code&gt;multiple&lt;/code&gt; allows to enter multiple email ids separated by commas.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Search field&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;input type="search" name="search" id="search"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The search input creates a basic search box with an 'x' at the end, when clicked clears the value entered. In most modern browsers, the values entered are saved and reused to offer auto-completion.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phone number field&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;input type="tel" name="tel" id="tel"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This field accepts only numbers so it is used for entering phone numbers. When accessed via touch device, it opens a numeric keypad. Moreover, the &lt;code&gt;pattern&lt;/code&gt; is used to give regex constraints.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Number field&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;input type="number" name="number" id="number" min="1" max="10" step="2"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The number field is similar to the phone number field, but provides arrow buttons at the end to increase or decrease the value. Minimum and maximum constraints can be set through &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; attributes.&lt;/p&gt;

&lt;p&gt;While incrementing or decrementing, you can increase the value by setting &lt;code&gt;step="2"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Slider control&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;input type="range" id="slider" name="amount" min="100" max="1000" step="100" value="500"&amp;gt;&lt;br&gt;
&amp;lt;output for="slider"&amp;gt;&amp;lt;/output&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The slider is also used to select numbers where an accurate value isn't required. The output element outputs the value selected from the slider.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Color Picker&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;input type="color" name="color" id="color"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The color picker displays browsers default color palette in which you can select a color. The value returned is a 6-digit lowercase hexadecimal color.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Date and Time Picker&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;input type="date" name="myDate" min="2013-06-01" max="2013-08-31" step="7" id="myDate"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The date picker also opens browser default date picker when clicked. &lt;code&gt;min&lt;/code&gt;, &lt;code&gt;max&lt;/code&gt;, &amp;amp; &lt;code&gt;step&lt;/code&gt; can be set like in the above example. Futhermore, you can give different types to select particular value like month or week.&lt;/p&gt;

&lt;p&gt;To select month, &lt;br&gt;
&lt;code&gt;&amp;lt;input type="month"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To select week,&lt;br&gt;
&lt;code&gt;&amp;lt;input type="week"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To select time,&lt;br&gt;
&lt;code&gt;&amp;lt;input type="time"&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;The displayed time will be in 12 hour format whereas the returned value will be in 24 hour format.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To select date and time,&lt;br&gt;
&lt;code&gt;&amp;lt;input type="datetime-local"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;More on &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#input_types"&gt;types.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;More on &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attributes"&gt;attributes&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Selection Elements
&lt;/h2&gt;

&lt;p&gt;There are two types of selection inputs. Select dropdown and datalist element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Select dropdown&lt;/strong&gt;&lt;br&gt;
This allows the user to select a value from a predefined list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;select name="book"&amp;gt;
    &amp;lt;option&amp;gt;Alchemist&amp;lt;/option&amp;gt;
    &amp;lt;option&amp;gt;Atomic Habits&amp;lt;/option&amp;gt;
    &amp;lt;option selected&amp;gt;Rich Dad Poor Dad&amp;lt;/option&amp;gt;
    &amp;lt;option&amp;gt;The Green Mile&amp;lt;/option&amp;gt;
&amp;lt;select/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A default value can be given using &lt;code&gt;selected&lt;/code&gt; on an option if required. &lt;br&gt;
Additionally, grouping of options inside a list is possible by placing a number of options together inside &lt;code&gt;&amp;lt;optgroup/&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;optgroup label="Paulo Coelho"&amp;gt;
   &amp;lt;option&amp;gt;The Alchemist&amp;lt;/option&amp;gt;
   &amp;lt;option&amp;gt;Eleven Minutes&amp;lt;/option&amp;gt;
&amp;lt;/optgroup&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Datalist&lt;/strong&gt;&lt;br&gt;
The datalist also contains option elements like select element. It is then linked with an &lt;code&gt;input&lt;/code&gt; element. As we enter values inside the input, the options that match the given text are shown. It auto-completes the text entered by the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label for="country"&amp;gt;Enter country:&amp;lt;/label&amp;gt;
&amp;lt;input type="text" name="country" id="country" list="mySuggestion"&amp;gt;
&amp;lt;datalist id="mySuggestion"&amp;gt;
  &amp;lt;option&amp;gt;USA&amp;lt;/option&amp;gt;
  &amp;lt;option&amp;gt;India&amp;lt;/option&amp;gt;
  &amp;lt;option&amp;gt;China&amp;lt;/option&amp;gt;
  &amp;lt;option&amp;gt;Canada&amp;lt;/option&amp;gt;
  ...
&amp;lt;/datalist&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: Try datalist with &lt;code&gt;&amp;lt;input type="range"&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;input type="color"&amp;gt;&lt;/code&gt;. Have it a go to see the result.&lt;/p&gt;

&lt;h2&gt;
  
  
  Radio Buttons
&lt;/h2&gt;

&lt;p&gt;Radio buttons are essentially used to select only one out of many options. If a user needs to select an option with 5 or less numbers, radio buttons are preferred rather than list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;
  &amp;lt;label for="red"&amp;gt;Red&amp;lt;/label&amp;gt;
  &amp;lt;input type="radio" id="red" name="color" value="red"&amp;gt;

  &amp;lt;label for="green"&amp;gt;Green&amp;lt;/label&amp;gt;
  &amp;lt;input type="radio" id="green" name="color" value="green"&amp;gt;

  &amp;lt;label for="blue"&amp;gt;Blue&amp;lt;/label&amp;gt;
  &amp;lt;input type="radio" id="blue" name="color" value="blue"&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value of name attribute must be same in a group of radio buttons, so that previously selected option can be disabled while selecting a new one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checkboxes
&lt;/h2&gt;

&lt;p&gt;Checkboxes are similar to radio buttons. But they allow you to select more than one option in a group.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;
    &amp;lt;label for="coding"&amp;gt;Coding&amp;lt;/label&amp;gt;
    &amp;lt;input type="checkbox" id="coding" name="hobbies" value="coding"&amp;gt;

    &amp;lt;label for="music"&amp;gt;Music&amp;lt;/label&amp;gt;
    &amp;lt;input type="checkbox" id="music" name="hobbies" value="music"&amp;gt;

    &amp;lt;label for="gym"&amp;gt;Gym&amp;lt;/label&amp;gt;
    &amp;lt;input type="checkbox" id="gym" name="hobbies" value="gym"&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: Most often, &lt;code&gt;fieldset&lt;/code&gt; elements are used to group radio buttons and checkboxes. And, a &lt;code&gt;legend&lt;/code&gt; element is represented as a caption to the &lt;code&gt;fieldset&lt;/code&gt;. Check it out &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Button
&lt;/h2&gt;

&lt;p&gt;Buttons are used as a clickable input for triggering various actions. There are three types of buttons.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Submit Button&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;&amp;lt;button type="submit"&amp;gt;&amp;lt;/button&amp;gt;&lt;/code&gt;&lt;br&gt;
This submits the form that the button is contained within.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reset Button&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;&amp;lt;button type="reset"&amp;gt;&amp;lt;/button&amp;gt;&lt;/code&gt;&lt;br&gt;
This resets the contents user entered in the form to their default.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Generic Button&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;&amp;lt;button&amp;gt;&amp;lt;/button&amp;gt;&lt;/code&gt;&lt;br&gt;
It is generally used as event listeners with the combination of javascript to trigger events in an interactive UI.&lt;/p&gt;
&lt;h2&gt;
  
  
  Textarea
&lt;/h2&gt;

&lt;p&gt;The textarea element allows you to input text in a text box that can span multiple size. The size can be given through &lt;code&gt;rows&lt;/code&gt; and &lt;code&gt;cols&lt;/code&gt;. As a user, you will able to resize it by default. However, it can be controlled with the &lt;code&gt;resize&lt;/code&gt; property in CSS. Unlike &lt;code&gt;input&lt;/code&gt;, &lt;code&gt;textarea&lt;/code&gt; requires a closing tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label for="comment"&amp;gt;Feedback&amp;lt;/label&amp;gt;
&amp;lt;textarea id="comment" name="comment" rows="4" cols="50"&amp;gt;
&amp;lt;/textarea&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Output Elements
&lt;/h2&gt;

&lt;p&gt;So far we have looked at various ways to input different types of data. Now let's take a look at few ways for outputting data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Progress Bar&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A progress bar is used to visually output what the progress is for a task. A real time scenario will be to show how much percent of files have been downloaded.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;progress max="100" value="50"&amp;gt;50/100&amp;lt;/progress&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For the record, the &lt;code&gt;min&lt;/code&gt; attribute can't be set.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Meter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Meters are used to visually display fixed values in a range. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;meter min="0" max="100" value="75" low="33" high="66" optimum="80"&amp;gt;75&amp;lt;/meter&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The range is split into three parts delimited by low and high. The preferred part, average part and the worst part. &lt;br&gt;
Then the meter is colored based on the value, green if its in the preferred part, yellow if its in the average part or red for worst part.&lt;/p&gt;

&lt;p&gt;Note: For both &lt;code&gt;progress&lt;/code&gt; and &lt;code&gt;meter&lt;/code&gt;, if the browser does NOT the support the element, the content present inside the element is considered as a fallback and for assistive technologies like screen readers.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thank you for staying and reading the entire article. If you feel like you don't understand a topic or need more information, the MDN &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/Forms#introductory_guides"&gt;docs&lt;/a&gt; are always a good resource. :)&lt;/em&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>html</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>An Introduction to Webpack</title>
      <dc:creator>Rishikrishna</dc:creator>
      <pubDate>Mon, 24 Jan 2022 06:12:20 +0000</pubDate>
      <link>https://dev.to/rishikrishna_r/an-introduction-to-webpack-406l</link>
      <guid>https://dev.to/rishikrishna_r/an-introduction-to-webpack-406l</guid>
      <description>&lt;h2&gt;
  
  
  What is Webpack?
&lt;/h2&gt;

&lt;p&gt;Webpack is a bundling tool made from javascript. For instance, It can take up a collection of javascript files and produce a single output javascript file. It can bundle images, css files, javascript files, etc to produce static assets.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why we need bundling?
&lt;/h2&gt;

&lt;p&gt;Without a module bundler like webpack, one would import packages using html script tags,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8" /&amp;gt;
    &amp;lt;title&amp;gt;Getting Started&amp;lt;/title&amp;gt;
    &amp;lt;script src="https://unpkg.com/lodash@4.17.20"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;script src="./src/index.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This type of approach is not advisable since it has few drawbacks. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;When unused scripts are included, unnecessary code is included.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When scripts are included in the wrong order, or if its missed, the application will fail.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The packages are imported in the global namespace.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, if the application code is bundled with webpack, it uses a dependency graph to find out what packages are needed to use in the application and ignores the unused packages. It then uses this dependency graph to generate an optimised and minified bundle which will be executed in the correct order.&lt;/p&gt;

&lt;p&gt;Next, a module bundler can compile languages which cannot be understood by the browser like typescript with the use of transpilers like babel. &lt;/p&gt;

&lt;h2&gt;
  
  
  How to use webpack
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;npm install webpack webpack-cli --save-dev&lt;/code&gt; &lt;br&gt;
After adding these to package.json, webpack can be used from command line.&lt;/p&gt;

&lt;p&gt;Let's consider an example of using lodash in a project. Let's install lodash from npm.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install lodash&lt;/code&gt; (A modern JavaScript utility library delivering modularity, performance &amp;amp; extras.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;index.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import lodash from 'lodash';
import printMe from './print.js'

let value = lodash.join(['a', 'b', 'c'], '~');

console.log(value);
printMe();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's have another file named print.js, which exports a function to index.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;print.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function printMe() {
    console.log('I get called in index.js');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we setup webpack.config.js file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;webpack.config.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;path&lt;/strong&gt; module provides utilities for working with file and directory paths&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require('path');

module.exports = {
    entry: {
        index: './src/index.js',
    },
    output: {
        filename: 'main.js',
        path: path.resolve(__dirname, 'dist'),
    },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This specifies basically that index.js is our entry file and we want it to be compiled. We also specify that the compiled version be renamed as main.js and placed in subfolder dist, possibly where index.html is also placed.&lt;/p&gt;

&lt;p&gt;Now, we shall use webpack from command line,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx webpack&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will run webpack that is installed in the node_modules folder, starts with the index.js file and finds all require/import statements and replaces them with their appropriate code to create a single output file named main.js inside dist subfolder.&lt;/p&gt;

&lt;p&gt;Now this main.js file can be used in index.html file as such,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script defer src="main.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;That's webpack in a nutshell. We no longer populate packages in global namespace via external scripts. Any new packages or libraries will be added using require/import as opposed to external scripts.&lt;/p&gt;

&lt;p&gt;Note: By default, webpack uses production mode, which will produce minified output file. If you want the output file to be readable to developers, you can use development mode in webpack config file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require('path');

module.exports = {
    mode: 'development',
    entry: {
        index: './src/index.js',
    },
    output: {
        filename: 'main.js',
        path: path.resolve(__dirname, 'dist'),
    },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thanks for reading the blog. Feel free to provide inputs and suggestions for any areas of improvement. :) &lt;/p&gt;

&lt;p&gt;Check out &lt;a href="https://webpack.js.org/guides/asset-management/"&gt;this&lt;/a&gt; guide if you want to learn about loading other assets like css files.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webpack</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
