Book Critic

I don’t like to step on anyone’s work. Having taught university computer science for a decade, I can tell you that it is difficult to present technical material in a way that is both correct and engaging. My students’ surveys remind me every semester that 100% satisfaction is elusive. But I want to critique the writing style of a book on Swift that I just browsed at Barnes and Noble. If you are easily tripped up on vague or ambiguous descriptions, please know that we share this obsessive trait.

See the section subtitled Building Blocks of Swift, in bold? It establishes the very basic programming concept of named storage, called variables in most material even if their values never change. The unchanging named values are also called constants. In Swift, these named values are declared with the syntax var variableName=value for variables and with let constantName=value for constants.

These are my complaints :

  • The first paragraph explains variables and constants, but only gives the keyword var for variables. What about let for constants? This leaves me hanging.
  • The sample code below P1 is obviously not illustrative of the example described, even though it prefaces the code with “as shown in this example:”.
  • The sample code below P1 contains errors! The reassignment of the constant will break at compile time. Syntax errors, while useful for examples, must be flagged clearly so that the reader does not struggle trying to figure out the code!
  • The last example in that section says that you can omit the var keyword, yet we clearly see the var keyword in the example code given. It is more clear to say that we can omit additional var keywords when declaring multiple variables.

So, buyer beware.

CERT, C, and Swift

Swift does not do implicit type conversions between integers of different sizes and signages. By contrast, Java does implicit conversion, but with less risk of unexpected effects due to the lack of unsigned integer types. C, that dinosaur, has a strange and secret show behind every elementary school math operation.

Why is type conversion confusing? I’ll use C as my example. CERT has some of the best low-level down-and-dirty descriptions of the language, so I’ll be borrowing heavily from their examples.

We’ll start with an easy one: Promotion. During arithmetic, everything is automatically promoted up to int or uint (32 bits on most platforms) in order to prevent overflow. It is curious that they don’t promote to something bigger (long), but x86 assembly uses 32-bit registers most of the time, so it’s a natural fit.

        // Example Apple : Integer Promotions
        unsigned char appleCharOp1, appleCharOp2, appleCharOp3;
        unsigned char appleCharResult;
        unsigned char appleCharWrongResult;

        appleCharOp1 = 80;
        appleCharOp2 = 70;
        appleCharOp3 = 100;
        appleCharResult = appleCharOp1 * appleCharOp2 / appleCharOp3;
        appleCharWrongResult = (unsigned char)(appleCharOp1 * appleCharOp2) / appleCharOp3;

If we run this in the debugger, using the lldb command type format add –format “unsigned dec” “unsigned char” for clarity, we see the following:

(lldb) frame variable

(unsigned char) appleCharOp1 = 80

(unsigned char) appleCharOp2 = 70

(unsigned char) appleCharOp3 = 100

(unsigned char) appleCharResult = 56

(unsigned char) appleCharWrongResult = 2

The correct answer is the result of being able to hold the intermediate product, 5600, in a 32-bit variable. If you truncate the intermediate product to 8 bits, you get 0xE0, which is 224. Divide this by 2 gives you after rounding.

Mind you, the end result is that you get the expected answer as long as you don’t stick a weird cast in the middle like I did there. But here is an example where you get the wrong answer without any extra work:

        // Example Cantaloupe
        unsigned int cantaloupeInt1 = UINT_MAX / 4;
        unsigned int cantaloupeInt2 = UINT_MAX / 8;
        unsigned int cantaloupeInt3 = UINT_MAX / 16;
        unsigned long long cantaloupeLongLong1 = cantaloupeInt1 * cantaloupeInt2 / cantaloupeInt3;

Results are

(unsigned int) cantaloupeInt1 = 1073741823

(unsigned int) cantaloupeInt2 = 536870911

(unsigned int) cantaloupeInt3 = 268435455

(unsigned long long) cantaloupeLongLong1 = 10

That’s the wrong answer. You get the right answer if you cast each term to (unsigned long long).

There are many other factors to C integer conversion, such as precision and rank, but these examples suffice to show why it’s a tricky subject.

Enter Swift’s design choice. If you try the same basic code in Swift :

let cantaloupeOp1 : UInt32 = UInt32.max / 4;
let cantaloupeOp2 : UInt32 = UInt32.max / 8;
let cantaloupeOp3 : UInt32 = UInt32.max / 16;
var cantaloupeResult : UInt64 = cantaloupeOp1 * cantaloupeOp2 / cantaloupeOp3

It simply refuses to compile. The operations on the right are legal, but the assignment is not.

Swift is Here to Stay

There are a few pockets of the internet complaining that Swift is not ready for production yet. I dive into these half expecting to see complaints about the changes in Swift 2.2, the rapidly changing standards, etc. Instead, those professional developers who are complaining are warning about lack of binary compatibility between old and new frameworks.

I am not deterred. I’m into Swift now.

This post is short on content. The plan is to follow Apple’s guide to grok basic syntax, followed by Ray’s guide on style to get the latest and greatest best-practices.

After that, the pickin’s get slim because I’m jonesing for Metal tutorials. Good news is that Jacob Bandes says that it makes more sense than OpenGL or CUDA. We shall see, we shall see.

Giving Hipwords some GPL Love

GPL has not always been my favorite license. This feeling got worse when my daughter’s Minecraft server got shot down by a DMCA takedown of the server code she was running. If you have never read about this, you probably should.

But I do respect copyright and licensing, big-time. And since my favorite-at-the-moment WordPress theme is licensed under GPL, I should probably do the right thing and post my minimal PHP changes here. And that also gives me a chance to test out Crayon Syntax Highlighter!

<h1 class="site-title"><?php bloginfo( 'name' ); ?></h1>
<?php if (is_front_page()) { ?>
<h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
<?php } ?>

<?php if ((!is_home()) and !(is_archive())) { ?>

<?php the_title( sprintf( '<h2 class="entry-title site-title"> Talks about ', es
c_url( get_permalink() ) ), '</h2>' ); ?>
<?php if ( 'post' == get_post_type() ) : ?>
<div class="entry-meta">
	<?php the_time('l, F jS, Y') ?>
	<?php the_category(', ') ?>
</div><!-- .entry-meta -->
<?php endif; ?>
<?php } ?>
<?php if (is_archive()) { ?>
<?php
		the_archive_title( '<h2 class="site-title"> Thoughts on ', '</h2
>' );
the_archive_description( '<div class="taxonomy-description">', '</div>' )
;?>
<?php } ?>

What you’re looking at are the modifications I made to header.php in order to change the site titles of all archive and search pages to something more egotistical. header.php and footer.php were the only pieces of the Hipwords theme that I had to override directly, rather than using the preferred WordPress theme extension mechanism : child themes.

Child themes are amazingly simply to use, and they quite frankly do more than I had a right to expect them to do. You simply create your own folder and create a functions.php and style.css in a way that has been described well in other spaces.

I exploited a surprising feature when I wanted to change just a small bit of the header and the footer : It turns out that if you copy any PHP file, like one corresponding to archive or static pages, from the parent theme into the child folder, the WordPress engine loads that file preferentially. I utilized this behavior to copy-and-modify the footer to make one final change…

<a href="<?php echo esc_url( __( 'http://wordpress.org/', 'hipwords' ) ); ?>"><?php printf( __( 'Powered by %s', 'hipwords' ), '<b>WordPress</b>' ); ?></a>
<span class="sep"> | </span>
<a href="<?php echo esc_url( __('http://forbetterweb.com/', 'hipwords') ); ?>" title="<?php esc_attr_e('forbetterweb.com', 'hipwords'); ?>">
<?php printf( __('Theme: <b>HipWords</b> by %s.', 'hipwords'), 'ForBetterWeb.com' ); ?>
</a>

/* ... That was the old, here is the new ... */

> &copy; <?php echo date("Y"); ?> Michael Johnson

That’s right. I replaced their credits with the copyright notice. Well, it’s not like I’ve been quiet about how awesome Hipwords is!

Parting thoughts: I would really love to pass on this child theme, small though it may be, on to other software developers in need of a blogging platform. This would involve some up-front work and maintenance commitment on my part. Up front, I would need to finish customizing the theme and create a downloadable package to submit to the free WordPress repository. In maintenance mode, I would need to at least monitor security issues and make sure I did my part to keep the web safe. I’m not sure if it is worth the effort.

Have thoughts? Leave them in the comments!

The WordPress Ecosystem

WordPress is amazing!

I spent a lot of time over the past few months trying to settle on a blogging platform. For me, it is not a trivial matter of looking up features and reading opinions. I try before I buy. On my local drive there are many prototype sites written in more than a few languages slash frameworks. Over at http://jameecreations.com I exercised my raw HTML and CSS skills. Now that I have a WordPress site going, I find that those skills are helping me customize my theme just as I like it.

But before I get lost in code logistics, let me say what I came here to say: WordPress is awesome because of the amazing community of feature contributors. Between the plugins and the themes you have everything you could ever want from a modern, responsive site. No longer do I search for “how to do cool animated transition XYZ”. Now I search for “plugin for cool animated transition XYZ”, and there it is. Install it, configure it, I’m done.

http://wordpress.org and https://wordpress.org/themes/hipwords/ have made this blog a pleasure to create, and I cannot thank their creators and the creative community around them enough.

Now for some minimal coding logistics : A Child Theme (see https://premium.wpmudev.org/blog/how-to-create-wordpress-child-theme/ for great tutorials) enabled me to create a beautiful module loosely-coupled to WordPress and the parent theme. I was able to get a fun font from my name in the form of Lobster from Google Fonts. Another stylesheet resource, hooked into functions.php as with the previous, comes from Fontawesome. Using the :before pseudo-class along with the font-family: CSS property and the icon hex codes, I added cool icons to my menu for social links. This plus some other formatting choices in style.css make for a nice, modern looking software developer blog.

Finally(for now), I fixed a headshot on my page with minimal pain using an Image Widget (https://wordpress.org/plugins/image-widget/). Since the Hipwords theme comes with a widget-based sidebar, the process was easy and largely driven through the browser.

That’s it for now; more to come as I feature-up the site. I would include code, but I have yet to install that plugin. 🙂 Wait for a future post.