This is perhaps the greatest feature of modern programming languages.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the 2010s was an interesting decade for software development some of the highlights include the Gold Rush of mobile apps the move towards cloud services such as AWS and a number of programming languages that many of us would end up falling in love with baked into these new languages was a greater emphasis on runtime safety and for good reason runtime errors are pretty much the bane of software developers with a large amount of time being spent to both prevent and mitigate them in languages such as C and C++ the most common runtime error is the Sig segv which stands for a segmentation violation or segmentation fault this error occurs whenever a program attempts to access memory it's not permitted to such as indexing an array Beyond its bounds writing to a memory address that belongs to another process or the most common cause attempting to dreference a null pointer in fact this last cause is so common that a number of solutions have been created to solve this problem these Solutions are known as null safety and I'd argue they're one of the greatest features of modern programming languages the most common implementation of n safety has involved the use of a special type inspired from functional programming languages such as hcll now hcll is a bit of an edgelord and calls this type the maybe type however in other languages it's simply referred to as an optional the implementation of this type is actually rather simple and is basically a union or enum to represent either one of two values the first of these values is sum also called something and represents the existence of an underlying value which is also contained within the type the second of these values is none or nothing and represents the absence of a value similar to null the benefit of using an optional is that it pushes the handling of the none or null value into the type system by doing so it moves the error handling out of the runtime into a compile time check this requires developers to explicitly handle the N case before their code can even be compiled and deployed moving errors from the runtime into compile time is a very good thing and because of this optionals can be found in a number of different languages newer languages such as rust cotlin and Swift provide the option nullable and optional type respectively even traditional languages such as Java and C++ support optionals as well with Java adding the optional type in 2014 and the king of runtime errors provides STD optional since C++ 17 notably absent from from this list however is go which does not provide any real null safety something I personally have a problem with however that's a video for another time to gain an understanding of how the optional Works under the hood I've created a rather simple implementation in Rust which is just an enum with a generic type this enum provides the two values we mentioned earlier sum and none the main thing to note is that our sum value has Associated data which matches our generic type in order to use this type without any methods the compiler requires us to use pattern matching in order to handle either of the states that the optional can be in whether it has a value or not this implementation whilst naive is actually very similar to how rust implements option under the hood orbe it with a few more macros and methods despite being simple this implementation shows us that there are two features a language needs in order to support the optional type the first feature is generics which allows the optional to wrap the associated value keeping it type safe although not strictly necessary I haven't found an implementation that doesn't use them the second requirement is for the language to be compiled otherwise that defeats the purpose of a compile time check so given these requirements how then do interpreted languages such as JavaScript provide n safety well they do so using a feature called safe navigation safe navigation also known as optional chaining first appeared in The Groovy language back in 2011 and provides a means to safely interact with a potentially null value at runtime to do this in JavaScript you simply use the question mark operator before calling an object's method or property if the object is null or undefined then the operator will short circuit and returned an undefined value rather than throwing an error given this Behavior it makes it possible to chain this operator as you cool nested properties of an object and whilst it's not as effective as compile time safety it certainly reduces the boiler plate of having to write if statements with a lot of Boolean ands additionally the use of linters can also help to reduce the risk of error one such option is the no unsafe optional chaining rule in eslint which can be used to reduce any type errors when using safe navigation as well as JavaScript this feature is also found in Ruby which instead uses the aasan symbol given that the question mark is already used by methods to Mark a Boolean return value whilst both JavaScript and Ruby support safe navigation one language that does not is Python and despite a proposal being raised in 2015 doesn't look like it will any anytime soon as well as interpreted languages safe navigation has also made its way to compiled ones Swift added the feature the same time that it did optionals requiring the use of the operator in order to unwrap an optional safely rust also provides safe navigation as well when inside of a function that returns an option any internal options can be unwrapped using the safe navigation operator which if the option is none will Short Circuit the function and return none early this Behavior again is inspired by functional programming languages in fact the maybe type in hasco that we mentioned earlier is actually a monad which provides this chaining feature ultimately the inclusion of n safety in many modern and traditional languages is something I'm happy to see newer languages such as rust Swift and cotlin put a lot of emphasis on runtime safety and by doing so means we get to spend less time fixing bugs and more time writing code that we can feel just that little bit more confident about
Info
Channel: Dreams of Code
Views: 106,178
Rating: undefined out of 5
Keywords: null safety, runtime, runtime safety, coding, code, rust, swift, c++, java, kotlin, ocaml, javascript, ruby
Id: XIhQYRNBAYs
Channel Id: undefined
Length: 5min 55sec (355 seconds)
Published: Tue Jan 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.