Newsletter # 9 - Creating a state machine in Swift Nov 15 2019

Hello,

Are you excited for the new 16" MacBook Pro? For me, it's much more than I would use. I'm happy with my 13" MBP, and I haven't had any problems with the keyboard, so I'll keep my current machine. But if I were to get one for free, I wouldn't be mad at all.

Also, some people are excited about the return of the Esc key. It seems to be missed by many people. So much so that it even made it to the promotional video.

Anyways, let me know if you are getting a new one, and if it's worth it when you test it :).

This week's post was about state machines, one of the most common design patterns I've encountered when modelling systems. The state pattern is used to model systems that are represented by the combination of states and events. For example, game elements are usually represented via states/events.

The post also included a little opinion section, where I discuss my take on design patterns and why they are important. Also, I mention the importance of making information accessible to everyone. We sometimes use jargon that makes the topics seem complicated, while the basic idea is simple. This complexity scares people off. And it even backfires. When we are in a culture where the standard is to write the information in the most challenging way possible, it is hard to understand new topics. So I encourage you to simplify and make your code accessible to everyone.

Ok, here is the link for the post if you are interested:

https://rderik.com/blog/creating-a-state-machine-in-swift/

Let me know what you think.

Ok, that's it for this week. Have a good weekend!

Derik

Tip of the week

ASCII characters from 0 to 31, were reserved for Control characters. These characters don't represent a printable character on-screen but can represent an action.

For example, to transmit a Backspace from a terminal, you would send the ASCII code 8. Keyboards used to have a specific key to send the control codes. The key was (you probably already guessed) the Control key :).

Ok, the tip is this, we still have access to sending those Control characters! Let me explain. Let's use back-space as our example.

Now our keyboard sends directly the control character 8 when we press backspace, so we don't need the Control key for backspace.

But just for fun, let's have a look at part of the ASCII table (in hex):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
           The hexadecimal set:

         00 nul   01 soh   02 stx   03 etx   04 eot   05 enq   06 ack   07 bel
         08 bs    09 ht    0a nl    0b vt    0c np    0d cr    0e so    0f si
         10 dle   11 dc1   12 dc2   13 dc3   14 dc4   15 nak   16 syn   17 etb
         18 can   19 em    1a sub   1b esc   1c fs    1d gs    1e rs    1f us
         20 sp    21  !    22  "    23  #    24  $    25  %    26  &    27  '
         28  (    29  )    2a  *    2b  +    2c  ,    2d  -    2e  .    2f  /
         30  0    31  1    32  2    33  3    34  4    35  5    36  6    37  7
         38  8    39  9    3a  :    3b  ;    3c  <    3d  =    3e  >    3f  ?
         40  @    41  A    42  B    43  C    44  D    45  E    46  F    47  G
         48  H    49  I    4a  J    4b  K    4c  L    4d  M    4e  N    4f  O
         50  P    51  Q    52  R    53  S    54  T    55  U    56  V    57  W
         58  X    59  Y    5a  Z    5b  [    5c  \    5d  ]    5e  ^    5f  _
         60  `    61  a    62  b    63  c    64  d    65  e    66  f    67  g
         68  h    69  i    6a  j    6b  k    6c  l    6d  m    6e  n    6f  o
         70  p    71  q    72  r    73  s    74  t    75  u    76  v    77  w
         78  x    79  y    7a  z    7b  {    7c  |    7d  }    7e  ~    7f del

When we press the Control + any key, the ASCII value of the key will have its first two bits converted to zero.

Let's use the letter h (ASCII value 68) as an example.

Remember, ASCII characters are represented in 7 bits. This means that the ASCII value of h (68h, you can check the table) in binary is = 110 1000 (110b is 6h and 1000b is 8h). What happens if we zero out the first 2 bytes? We get: 000 1000 that is 08h, and that is?

If you check the ASCII table, that is backspace! So if we press Ctrl+h, we should get the same result as pressing backspace. Try it out :).

That takes me back to the issue of the Esc key in the new MacBook Pros. There was no problem. Some programmers already knew this, and press Ctrl+] instead of Esc. That is why I have my Caps-lock key mapped to Control.

Not sure if its a tip, but I hope you liked it.


** If you want to check what else I'm currently doing, be sure to follow me on twitter @rderik or subscribe to the newsletter. If you want to send me a direct message, you can send it to derik@rderik.com.