From ce46fb87fdf38852289699dff8d8e824c30bf58e Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 25 Jun 2019 11:57:05 -0300 Subject: [PATCH] New chapter: use structures --- content/books/things-i-learnt/_index.md | 3 +- .../books/things-i-learnt/data-types/index.md | 2 +- .../languages-are-more/index.md | 2 +- .../things-i-learnt/use-structures/index.md | 58 +++++++++++++++++++ 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 content/books/things-i-learnt/use-structures/index.md diff --git a/content/books/things-i-learnt/_index.md b/content/books/things-i-learnt/_index.md index 7eeb593..e4b2edc 100644 --- a/content/books/things-i-learnt/_index.md +++ b/content/books/things-i-learnt/_index.md @@ -30,6 +30,7 @@ template = "section-contentless.html" * [Beware of Interface Changes](interface-changes) * [It's Better To Let The Application Crash Than Do Nothing](crash-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 * [A Language Is Much More Than A Language](languages-are-more) diff --git a/content/books/things-i-learnt/data-types/index.md b/content/books/things-i-learnt/data-types/index.md index 8994c85..178d4f8 100644 --- a/content/books/things-i-learnt/data-types/index.md +++ b/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 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") }} diff --git a/content/books/things-i-learnt/languages-are-more/index.md b/content/books/things-i-learnt/languages-are-more/index.md index 8a3404d..856df73 100644 --- a/content/books/things-i-learnt/languages-are-more/index.md +++ b/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 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") }} diff --git a/content/books/things-i-learnt/use-structures/index.md b/content/books/things-i-learnt/use-structures/index.md new file mode 100644 index 0000000..5883d21 --- /dev/null +++ b/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. + + + +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") }}