Browse Source

New chapter: use structures

master
Julio Biason 5 years ago
parent
commit
ce46fb87fd
  1. 3
      content/books/things-i-learnt/_index.md
  2. 2
      content/books/things-i-learnt/data-types/index.md
  3. 2
      content/books/things-i-learnt/languages-are-more/index.md
  4. 58
      content/books/things-i-learnt/use-structures/index.md

3
content/books/things-i-learnt/_index.md

@ -30,6 +30,7 @@ template = "section-contentless.html"
* [Beware of Interface Changes](interface-changes) * [Beware of Interface Changes](interface-changes)
* [It's Better To Let The Application Crash Than Do Nothing](crash-it) * [It's Better To Let The Application Crash Than Do Nothing](crash-it)
* [If You Know How To Handle It, Handle It](handle-it) * [If You Know How To Handle It, Handle It](handle-it)
* [Types Say What You Data Is](data-types) * [Types Say What Your Data Is](data-types)
* [If Your Data Has a Schema, Use a Structure](use-structures)
* Community/Teams * Community/Teams
* [A Language Is Much More Than A Language](languages-are-more) * [A Language Is Much More Than A Language](languages-are-more)

2
content/books/things-i-learnt/data-types/index.md

@ -37,4 +37,4 @@ performance out.
Fortunately, new languages are based on ML, which wouldn't allow this kind of Fortunately, new languages are based on ML, which wouldn't allow this kind of
stuff. stuff.
{{ chapters(prev_chapter_link="/books/things-i-learnt/handle-it", prev_chapter_title="If You Know How To Handle It, Handle It", next_chapter_link="/books/things-i-learnt/languages-are-more", next_chapter_title="A Language Is Much More Than A Language") }} {{ chapters(prev_chapter_link="/books/things-i-learnt/handle-it", prev_chapter_title="If You Know How To Handle It, Handle It", next_chapter_link="/books/things-i-learnt/use-structures", next_chapter_title="If Your Data Has a Schema, Use a Structure") }}

2
content/books/things-i-learnt/languages-are-more/index.md

@ -39,4 +39,4 @@ surface of what the whole of a language encapsulates and if you ignore the
other elements in it, you may find yourself with a cute language in a other elements in it, you may find yourself with a cute language in a
community that is always fighting and never going forward. community that is always fighting and never going forward.
{{ chapters(prev_chapter_link="/books/things-i-learnt/data-types", prev_chapter_title="Types Say What You Data Is") }} {{ chapters(prev_chapter_link="/books/things-i-learnt/use-structures", prev_chapter_title="If Your Data Has a Schema, Use a Structure") }}

58
content/books/things-i-learnt/use-structures/index.md

@ -0,0 +1,58 @@
+++
title = "Things I Learnt The Hard Way - If Your Data Has a Schema, Use a Structure"
date = 2019-06-25
[taxonomies]
tags = ["en-au", "books", "things i learnt", "data classes", "structs"]
+++
You may be tempted to use a list (or tuple, if your language allows) to keep
your data if it's simple -- like, say, only 2 fields. Don't.
<!-- more -->
Some languages allow unstructured data to be kept in the format of tuples:
They act like lists, but you can use to store heterogeneous data (which is a
cute way of "it stores fields of different types").
This languages also allow you to "destructurize" them, so you can extract
elements from them without directly accessing them by index.
For example, in Python, you can have a tuple with:
```python
a_tuple = ('A String', 1, 7.5)
```
And you can destructure it with
```python
some_string, an_integer, a_float = a_tuple
```
See? It's simple! You don't need to create a whole structure if you're just
passing a string, an integer and a float around.
Except, you do.
Tuples and destructuring should be used only when you need to pass data from
one function to another -- and barely that. When you have this tuple being
passed around, being destructured and created again -- say, you are adding one
value of the tuple to another value and producing a new tuple in the same
format -- then you have a structured data.
And when you have a structured data, you must use a data class or a struct (or
even
[NamedTuples](https://docs.python.org/3/library/collections.html?highlight=namedtuple#collections.namedtuple),
if you're using Python).
Although it may look way more simpler to keep destructuring and building the
tuple over and over, in the long run you'll end up with a mess 'cause a simple
change -- like adding a new field -- will require checking every destructuring
and every creation of the tuple to make sure if will stay in the same shape
every time.
So: You data has a schema? Use a Data Class or Class or Struct. Only if it is
schemaless, then you can use a tuple.
{{ chapters(prev_chapter_link="/books/things-i-learnt/data-types", prev_chapter_title="Types Say What Your Data Is", next_chapter_link="/books/things-i-learnt/languages-are-more", next_chapter_title="A Language Is Much More Than A Language") }}
Loading…
Cancel
Save