Chapter 8

What are the benefits of developing assessor functions?

Assessor functions are a great way to provide an official API to users of the particular object. The underlying implementation is therefore decoupled from the interface. Should there be any changes to the implementation, there will be zero impact on users of the object as long as the contract of the assessor functions is unchanged.

What would be an easy way to discourage the use of internal fields of an object?

The easiest way to discourage the use of internal fields of an object is to have a special naming convention. A commonly used convention is to have an underscore as the prefix of the field name. If the programmer tries to use the field, then they are reminded that the field is supposed to be private.

Which functions may be extended as part of the property interface?

There are three functions from the Base package that can be extended to provide specific functionalities to the dot notation for field access. The functions are getproperty, setproperty!, and propertynames. An important point to remember is that once these functions are defined, all direct field access has to be changed to getfield and setfield! to avoid the recursion problem.

How can we capture the stack trace from a catch-block after an exception has been caught?

Once an exception is caught, we can use the catch_backtrace function to capture the stack frames right before the exception was caught. We can then pass the result to the stacktrace function to retrieve an array of StackFrame objects.

What is the best way to avoid the performance impact of a try-catch block for a system that requires optimal performance?

The best way to avoid the performance impact of a try-catch block is to not use it at all. We should find other ways to handle exceptions. For example, we can check for any condition that might cause a subsequent function to fail. In that case, we can proactively handle such a scenario. Another option is to catch the exception outside of a loop; hence, we would handle the exception at a higher level.

What are the benefits of using the retry function?

The retry function is a great way to automatically repeat an operation that may fail. Doing this ensures that important tasks are guaranteed to be completed, barring other types of unrecoverable exceptions.  

How do we hide away global variables and functions that are used internally in a module?

We can use a let-block so that global variables are bound as part of the let-block and not exposed to the global scope of the module. Functions that are defined inside the let-block can be declared as global when we need to expose them to the module.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.130.13