Skip to main content

VS Code Extension of the Day: Settings Sync

**More Information**
  • This is the 7th post in a multipart series.
    If you want to read more, see our series index

Settings sync is the first extension I always install as it allows me to restore my settings AND extensions. It uses GitHub gists to store the config, so you have a slightly annoying setup process initially but once done, each time you change a setting or extension it updates it to the gist. Then when you get a new install, it pulls down the settings and installs all the extensions you had and you can get everything setup really easily and quickly.

Learn more and download it

VS Code Extension of the Day: Editor Config

**More Information**
  • This is the 6th post in a multipart series.
    If you want to read more, see our series index

If you work in a team where choice is important, you may find everyone has a different editor. Today our team uses VSCode, Atom & IntelliJ. Editor Config is a set of extensions for many editors which tries to unify things like tab vs. spaces, trailing spaces, empty lines at the end etc… Think of this as your editor linting as you go. Unfortunately, support is limited for what can be done, but a lot of editors and IDEs are supported.

Learn more and download it

VS Code Extension of the Day: Dracula Official

**More Information**
  • This is the 5th post in a multipart series.
    If you want to read more, see our series index

So not an extension so much as a theme, Dracula is a great dark theme for Code. It is a little more playful in its colours too which is a plus but what makes it stand out is the Dracula community

There are tons of extensions to add Dracula to everything. I have my slack, terminal.app and IntelliJ all configured as well. It is really great to have the consistency everywhere.

Learn more and download it

VS Code Extension of the Day: Code Runner

**More Information**
  • This is the 4th post in a multipart series.
    If you want to read more, see our series index

Code Runner is a lightweight code execution tool. I think of it as the middle ground between a REPL environment and actually running code normally. So you can execute a single file or even highlight specific lines and execute just them. It supports an amazing array of languages C, C++, Java, JavaScript, PHP, Python, Perl, Perl 6, Ruby, Go, Lua, Groovy, PowerShell, BAT/CMD, BASH/SH, F# Script, F# (.NET Core), C# Script, C# (.NET Core), VBScript, TypeScript, CoffeeScript, Scala, Swift, Julia, Crystal, OCaml Script, R, AppleScript, Elixir, Visual Basic .NET, Clojure, Haxe, Objective-C, Rust, Racket, AutoHotkey, AutoIt, Kotlin, Dart, Free Pascal, Haskell, Nim, D

I personally use it all the time with JS & Kotlin. I haven’t needed to change any settings, though code-runner.runInTerminal sounds interesting.

Download it here

VS Code Extension of the Day: Bracket Pair Colorizer

**More Information**
  • This is the 3rd post in a multipart series.
    If you want to read more, see our series index

Initially, this extension allows your brackets, {} [] (), to be set to a unique colour per pair. This makes it really easy to spot when you are goofed up and removed a closing bracket. Behind the obvious is a lot of really awesome extras in it. You can have the brackets highlight when you click on them when you click on one the pair with bracketPairColorizer.highlightActiveScope and you can also add an icon to the gutter of the other pair bracketPairColorizer.showBracketsInGutter which makes it trivial to work our the size of the scope.

It also adds a function bracket-pair-colorizer.expandBracketSelection which is unbound by default but will allow you to select the entire area in the current bracket selection. Do it again, and it will include the next scope. For example, you can select the entire function, then the entire class.

Learn more and download it

VS Code Extension of the Day: Bookmarks

**More Information**
  • This is the 2nd post in a multipart series.
    If you want to read more, see our series index

The bookmarks extension adds another feature from Fat VS to code, the ability to bookmark to a place in a document/file/code and be able to quickly navigate backwards and forwards to it. One important setting that I think you should change is bookmarks.navigateThroughAllFiles - set that to true and you can jump to any bookmark in your project, with false (the default) you can only navigate to bookmarks in the current file.

Learn more and download it

Learning Kotlin: Operators don't need to mean one thing

**More Information**
  • This is the 18th post in a multipart series.
    If you want to read more, see our series index

Following on from the previous post we looked at operators and being able to use them yourself by implementing the relevant operator methods. The first part I want to cover in this second post is the Unary operators +, -, and !.

When I was learning this, the term unary jumped out as one I did not immediately recognise, but a quick Wikipedia read it became clear. For example, if you use a negative unary with a positive number, it becomes a negative number… It is primary school maths with a fancy name.


One thing to remember about operators is it is totally up to you what they mean, so, for example, let’s start with a simple pet class to allow us to define what type of pet we have.

package blogcode

enum class animal { dog, cat }

data class pet(val type: animal);

fun main(args: Array) { val myPet = pet(animal.dog) println(myPet) }

this produces pet(type=dog)

Now, maybe in my domain, the reverse of a dog is a cat, so I can do this to make this reflect my domain: package blogcode

enum class animal { dog, cat }

data class pet(val type: animal) { operator fun not() : pet = when(this.type) { animal.cat -> pet(animal.dog) animal.dog -> pet(animal.cat) } }

fun main(args: Array) { val myPet = pet(animal.dog) println(!myPet) }

This produces pet(type=cat)

And this is the core thing, that while a Unary has a specific purpose normally you can totally use it the way that makes sense. This is really awesome and powerful but it doesn’t stop there.

Normally when we think of something like the Unary not with a boolean, it goes from true to false (or vice versa), but it remains a boolean. There is nothing stating it has to be that way:

package sadev

enum class animal { dog, cat }

data class pet(val type: animal) { operator fun not() :String = “BEN” }

fun main(args: Array) { val myPet = pet(animal.dog) val notPet = !myPet println(“myPet is ${myPet::class.simpleName} with a value of ${myPet}”) println(“notPet is ${notPet::class.simpleName} with a value of ${notPet}”) }

In the above, the output is

myPet is pet with a value of pet(type=dog)
notPet is String with a value of BEN

In short, the not of a dog pet is actually a string with the value of ben. I have no idea how this is useful to real development, but it is amazing that Kotlin is flexible enough to empower it.

VS Code - Extension of the Day

Over at the ZA Tech Slack I have been posting an extension of the day in the #VSCode channel. It is my way of sharing cool things with the group and also forcing me to spend a bit of time reviewing my extensions and figuring them out a bit better and since not everyone follows that, here is the more permanent home for these mini posts on cool VS Code extensions.

  1. Better comments
  2. Bookmarks
  3. Bracket Pair Colorizer
  4. Code Runner
  5. Dracula Official
  6. Editor Config
  7. Settings Sync
  8. Paste JSON as Code

Learning Kotlin: Operators

**More Information**
  • This is the 17th post in a multipart series.
    If you want to read more, see our series index

This next post is an introduction to operators in Kotlin, not the basic the “use a plus sign to add numbers together” stuff (you read this blog, you got to be smart enough to figure that out). No, this post is about just how amazingly extensive the language is when it comes to support for allowing your classes to use them.

Adding things together

So let’s start with a simple addition things together with plus I said we wouldn’t do. In the following example code, we have a way to keep track of scoring events in a rugby game and I would like to add up those events to get the total score:

enum class scoreType {
    `try`,
    conversion,
    kick
}

data class score(val type:scoreType) {
    fun points() = when(this.type) {
        scoreType.`try` -> 5
        scoreType.conversion -> 2
        scoreType.kick -> 3
    }
}

fun main(args: Array<String>) {
    val gameData = listOf(score(scoreType.`try`), score(scoreType.conversion), score(scoreType.`try`), score(scoreType.kick))
    var totalPoints = 0
    for (event in gameData) {
        totalPoints = event + totalPoints 
    }

    println(totalPoints)
}

This obviously won’t compile, you can’t += an Int and my class? Right?!

We could make this change, which is probably the better way but for my silly example, let us say this isn’t ideal.

totalPoints = event.points() + totalPoints

So to get our code to compile we just need a function named plus which has the operator keyword and while I could call this myself, the Kotlin compiler is smart enough to make now it just work:

data class score(val type:scoreType) {
    fun points() = when(this.type) {
        scoreType.`try` -> 5
        scoreType.conversion -> 2
        scoreType.kick -> 3
    }

    operator fun plus(other:Int) = this.points() + other
}

How cool is that?!

If I wanted to take it further and support say totalPoints += event then you would need to add a function to Integer which tells it how to add a score. Thankfully that is easy with extension functions:

operator fun Int.plus(other:score) = this + other.points()

Extensive

While the above is a bit silly, imagine building classes for distances, time, weights etc… being able to have a kilogram and a pound class and add them together! What makes Kotlin shine is how extensive it is, just look at this list!

Class Operators Method Example Expression
Arithmetic +, ‘+=’ plus first + second
Augmented Assignments += `plusAssign first += second
Unary + unaryPlus +first
Increment & Decrement ++ inc first++
Arithmetic -, -= minus first - second
Augmented Assignments -= minusAssign first -= second
Unary - unaryMinus `-first
Increment & Decrement -- dec first--
Arithmetic *, *= times first * second
Augmented Assignments *= timesAssign first *= second
Arithmetic /, /= div first / second
Augmented Assignments /= divAssign first /= second
Arithmetic %, %= rem first % second
Augmented Assignments %= remAssign first %= second
Equality ==, != equals first == second
Comparison >, <, <=, >= compareTo first > second
Unary ! not !first
Arithmetic .. rangeTo first..second
In in, !in contains first in second
Index Access [, ] get This returns a value
Index Access [, ] set This sets a value
Invoke () invoke first()

I am going to go through some of these in more detail in future blog posts, but one I wanted to call out now:

Augmented Assignments vs. Plus or Minus

You might wonder why when we just implemented plus above we got both support for + and += and the table lists += under both plus and augmented. Why both - because you may want to support just += without supporting +.