How to resolve the algorithm Gotchas step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Gotchas step by step in the Wren programming language

Table of Contents

Problem Statement

In programming, a gotcha is a valid construct in a system, program or programming language that works as documented but is counter-intuitive and almost invites mistakes because it is both easy to invoke and unexpected or unreasonable in its outcome. Give an example or examples of common gotchas in your programming language and what, if anything, can be done to defend against it or them without using special tools.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Gotchas step by step in the Wren programming language

Source code in the wren programming language

class Rectangle {
    construct new(width, height) {
        // Create two fields.
        _width = width
        _height = height
    }

    area {
       // Here we mis-spell _width.
       return _widht * _height
    }

    isSquare {
        // We inadvertently use '=' rather than '=='.
        // This sets _width to _height and will always return true
        // because any number (even 0) is considered 'truthy' in Wren.
        if (_width = _height) return true
        return false
    }

    diagonal {
        // We use 'sqrt' instead of the Math.sqrt method.
        // The compiler thinks this is an instance method of Rectangle
        // which will be defined later.
        return sqrt(_width * _width + _height * _height)
    }
}

var rect = Rectangle.new(80, 100)
System.print(rect.isSquare) // returns true which it isn't!
System.print(rect.area)     // runtime error: Null does not implement *(_)
System.print(rect.diagonal) // runtime error (if previous line commented out)
                            // Rectangle does not implement 'sqrt(_)'


  

You may also check:How to resolve the algorithm Find the missing permutation step by step in the Lua programming language
You may also check:How to resolve the algorithm Fork step by step in the Oz programming language
You may also check:How to resolve the algorithm Egyptian division step by step in the jq programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the Groovy programming language
You may also check:How to resolve the algorithm Wieferich primes step by step in the C programming language