Skip to main content

Posts

How to setup GPG keys in windows and configure the public key in github

GPG In this post today, we are going to look into the steps that we need to follow to generate and use a gpg  key with github Below given are the steps Installation Install  GnuPG for windows Validation of Installation Run the below command to verify if gpg is installed and working fine gpg --version Generate GPG key gpg --full-generate-key Running the above command will start running a command line wizard or sequence of steps, Choose the right length of the key Provide your username and email address Also provide a comment (Optional) Once done, approve with "O" Then the system will prompt for Key phrase, which will be used to secure the keys, so please remember and provide a secure value Listing the key List the keys that were generated as a result of the previous command using the below command gpg --list-secret-keys --keyid-format=long This command will produce a similar output gpg --list-secret-keys --keyid-format=long -------------------------------- pub   2048R/35F5FFB2

Range check in Kotlin

Introduction In many cases, we might be interested in checking dates to see if they fit in the range or numbers to see if they fall between the given range of numbers. Kotlin has a short hand way to represent the range which can be quite handy fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean {     return date in first..last } The above snippet of code is used to check the date (1st argument) lies between the first and the last date ranges.

Operator Overloading in Kotlin

Introduction The operator overloading feature let's us to build custom operators. In The below example, we provide a compareTo which can help us in evaluation of the <, >, = functions in the custom object of type MyDate data class MyDate( val year: Int , val month: Int , val dayOfMonth: Int ) : Comparable<MyDate> {     operator override fun compareTo(other: MyDate)= when {          year != other.year -> year - other.year         month != other.month -> month - other.month         else -> dayOfMonth - other.dayOfMonth     } } In the above snippet, we are implementing the Comparable<T>   for our MyDate type so that we can compare two instances of type MyDate as given below     println(date1 < date2) Capabilities This helps us in implementing custom operators to ease the use of business objects throughout the code rather than repeating code or using lots of Helpers in the code base.

Extension Functions in Kotlin

Introduction Extension functions help us a lot when implementing re-usable code. In many cases where we are required to provide re-usable code, but unable to add the function to the class may be because it was from a 3rd party library or from an external module, we end up adding a pile of helpers in getting the job done. In Java, this feature is not supported unlike in C# and other languages. The drawback of this is we have to create a helper class and create static methods which are not tied to the types. This results in helper classes being used and is not more readable than having the method as an extension or part of the type. data class RationalNumber( val numerator: Int , val denominator: Int ) fun Int.r(): RationalNumber = RationalNumber( this , 1 ) fun Pair< Int , Int >.r(): RationalNumber = RationalNumber( this .first, this .second) The above snippet explains the use of extension functions  which help us in extending the Integer and the Pair Types. After building t

Renaming types on Import

The following snippet of code illustrates how the rename functionality works in Kotlin after importing a type. import kotlin.random.Random as KRandom import java.util.Random as JRandom fun useDifferentRandomClasses(): String {     return "Kotlin random: " +             KRandom.nextInt( 2 ) +             " Java random:" +             JRandom().nextInt( 2 ) +             "." } In the above snippet of code, we see that we are importing two Random types. We are able to use KRandom for the type imported from Kotlin and JRandom for the type imported from java.util.Random

varargs and array destructuring in Kotlin

Introduction The following post illustrates the use of varargs (variable number of arguments) and array destructing feature in Kotlin langauge. varargs This feature allows multiple arguments of a same type to be passed to a method and not using a collection like Array or list. This feature in java is implemented using the 3 consecutive dots notation given below ... Java Example public String getByValues (String... values) { // ... } Array destructuring With array destructuring, we can be able to use the elements of the array without looping through or pulling out by index which needs careful access as we might get array out of bounds exceptions if not checked. Sample Code fun main() {     val c1 = Color( "red" )     val c2 = Color( "Blue" )     val c3 = Color( "Green" )     val colors = arrayOf(c1, c2, c3)     val manyColors = arrayOf(*colors, c2.copy())     printColors(c1, c2, c3, message = "The color is: " ) // variable number of arg

Kotlin Data Classes and Smart Casts

Introduction Kotlin is a language that is built by the Jetbrains team and opensource contributors. The following are the goals that this language helps us achieve (not full set, for more details please visit kotlinlang.org Modern, concise and safe programming language A productive way to write server‑side applications (still using the frameworks that target JVM) Cross-platform layer for native applications Big, friendly and helpful community Kotlin Data Classes Below I am giving some of the kotlin snippets and short descriptions on what they can perform. Keep in mind that kotlin achieves very concise code data class Person ( val name: String , val age: Int ){     } fun getPeople(): List <Person> {     return listOf(Person( "Alice" , 29 ), Person( "Bob" , 31 )) } fun comparePeople(): Boolean {     val p1 = Person( "Alice" , 29 )     val p2 = Person( "Alice" , 29 )     return p1 == p2   // should be true } In the snippet above, we

The Key from Stackoverflow

Introduction For many years, we have been dealing with memes like try{ doSomething(); }catch(Exception ex) { println(getSolutionFromStackoverflow(ex)) }finally { testAndApplyThefix() } fun getSolutionFromStackoverflow(val ex:Exception) { curl get stackoverflow.com?q=ex.message ... } where the developers run into an error, they immediately lookup the exception in stackoverflow.com and get the solution and validate and close the error. This is a real one that is available from drop.com The original stackoverflow blogpost is here:  https://stackoverflow.blog/2021/09/28/become-a-better-coder-with-this-one-weird-click/ Looks like these will be part of a developer toolset in the near future