stevedoria.net

Real-World Computing Experience Distilled

Write Less Code: C# Properties

February 15th, 2018 Posted in C#, Refactoring | Comments

I inherited the responsibility of maintaining an internal development tool implemented in C#. I recently added new classes to extend the tool’s functionality. Several of the new classes uses several properties, and I found myself writing code similar to the following:

class Shape
{
    private int _width;
    private int _height;
    private bool _isVisible;

    public int Width {
        get {
            return _width;
        }
    }

    public int Height {
        get {
            return _height;
        }
    }

    public bool IsVisible {
        get {
            return _isVisible;
        }
        set {
            _isVisible = value;
        }
    }

    /* ... more code, including fields,
              properties, and functions ... */
}

Implementation of properties as is done in the above source code can fill whole screens. I found a concise way to write the above code while browsing references, and I present it here:

class Shape
{
    public int Width { get; }
    public int Height { get; }
    public bool IsVisible { get; set; }

    /* ... more code, including fields,
              properties, and functions ... */
}

According to Microsoft’s C# Programming Guide1, with auto-implemented properties, “the compiler creates a private, anonymous backing field that can only be accessed through the property’s get and set accessors.” Separate fields for auto-implemented properties are not used because of private, anonymous backing fields. Setting property values for auto-implemented properties without set accessors can be done by providing an initialization value when creating the auto-implemented property or by assigning a value to the auto-implemented property within the constructor. A reduction of lines is evident in the above source code.

How to Install GCC 7 on CentOS 7

February 14th, 2018 Posted in C++, IT Administration | Comments

Interested in keeping up with the latest C++ standard, I noticed that CentOS 7.4.1708 uses gcc version 4.8.5 20150623. This version of gcc defaults to ISO/IEC 14882:1998. Updated C++ standards have been released in 2003, 2011, 2014, and 2017. Although gcc developers are still implementing the 2017 standard, it is good to use a recent version of gcc that defaults to the 2014 standard. Installing centos-release-scl and devtoolset-7-gcc-c++ packages allows me to use a recent version of gcc.

To determine the default C++ standard used by g++:

1. $ g++ -dM -E -x c++ /dev/null | \
   grep -F __cplusplus

I did the following to install devtoolset-7:

1. # yum install centos-release-scl
2. # yum install devtoolset-7-gcc-c++

Every time I want gcc to default to using GCC 7, I run the following:

1. $ scl enable devtoolset-7 bash

Hello, Responsive Web Design

February 11th, 2018 Posted in Web Development | Comments

This blog’s customized WordPress theme is finally updated with responsive web design elements. Reading a tutorial at w3schools.com, Responsive Web Design Introduction, encouraged me to implement responsive web design this week. The introduction is concise and actionable for someone with previous experience using HTML and CSS. Seeing their simple implementation using HTML div tags and CSS media queries to implement a responsive web page gave me the needed confidence to start updating this blog.

My customized theme is based on Michael Heilemann’s Kubrick. I wanted to adopt responsive web design techniques to allow this blog to be mobile-friendly or presented naturally by web browsers running on mobile phones or tablet devices. I also wanted to keep many of Kubrick’s design elements, such as the centered column for content and sidebar with a blue header containing the blog title and footer containing centered statistics, when the page is viewed from desktop browsers. According to Heilemann, Kubrick “may be the most widely used template ever designed, having been in used [sic] by millions of sites and ported to over thirty completely different platforms.” I desired maintaining such a recognizable theme for my blog. Implementing responsive web design techniques was a great exercise in adapting and reusing this site’s design.

The Intelligent Investor

August 31st, 2017 Posted in Personal Finance | Comments

Picture of book cover for The Intelligent Investor by Benjamin GrahamBenjamin Graham’s The Intelligent Investor motivated changes to my personal investment operation. My copy of the book was published around 2006 and contains Jason Zweig’s commentary relating Graham’s observations about the stock market to the then recent dot-com crash. Having had some exposure to the dot-com crash and being an active participant in securities markets through the Great Recession up until today, I see in current market conditions parallels with those Graham and Zweig observed. Seeing these parallels pushed me to adjust my portfolio.

In a small section of his book, Graham discusses a company named Ling-Temco-Vought, Inc. The company grew through acquisitions funded by debt. Its market value increased as its size increased. With the increased market value, the company was able to borrow even more. In 1969, its long term debt was ten times earnings before interest and taxes (EBIT), and its interest charges were 98% of EBIT. This arrangement left very little gain for common stockholders. Market participants later realized this and adjusted the company’s market value accordingly.

I found a similar holding in my portfolio: Valeant Pharmaceuticals International, Inc. (VRX). Like Ling-Temco-Vought, VRX grew through acquisitions funded by debt. At the end of 2015, VRX had an EBIT of $1.4B and interest expense of $1.6B. Recognizing their struggle with debt, VRX has been focusing lately on divesting their acquisitions. Reading about Ling-Temco-Vought and seeing a parallel with VRX motivated me to immediately remove VRX from my portfolio.

In another gem found in his book, Graham also notes that a great number of initial public offerings (IPOs) are made at bull market peaks. This is a time when the market has a high demand for new offerings. He observes that IPOs during bull market peaks are of inferior quality, and he questions whether the investment bankers should be frowned upon for bringing securities of questionable value into the market.

There recently have been a flurry of IPOs as the S&P500 hovers at all-time highs. I was interested in IPOs by Blue Apron (APRN) and Snap (SNAP) before thoroughly reading Graham’s book. According to Snap’s Form S-1 registration statement, Snap has “incurred operating losses in the past, expects to incur operating losses in the future, and may never achieve or maintain profitability.” On its second day of trading, Snap peaked at $29.44 per share and a market valuation of $35.3B. The frenzied market valued Snap, a company that has never achieved profitability, above consistently growing market stalwarts. Since its peak about six months ago, Snap has declined to around $14.96 per share. Benjamin Graham kept me away from IPOs and prevented me from experiencing catastrophic losses.

Thanks to Benjamin Graham, I look forward to finding securities with good value underappreciated by the market. I hope to preserve capital while gaining a satisfactory, not exceptional, return on my investment.

Aiming Higher

May 7th, 2015 Posted in Personal Development | Comments

Many people interpret the phrase, “aiming higher,” as setting goals that are 110% of what is desired. A trivial example of this interpretation’s absurdity is setting the goal to fit one gallon of water into a cup. Aiming for an unattainable goal is aiming for failure. The trivial example shows it also wastes resources.

“Aiming higher” is better interpreted as setting higher level goals. We set goals at different levels everyday. Figuratively, the goals at 0ft determine what should be done right now while those at 35,000ft determine what should be done over a lifetime.

There is a popular anecdote in which of three workers, each performing the same task, a traveler asks, “What are you doing?” One reports he is cutting stone, the other is earning food for his family, and the last is building a cathedral.

It is easy to become too focused on goals for the day, week, or quarter. We should frequently refocus our goals for career, relationships, and life. We should reflect on why we are doing what we do. Through such reflection, we can properly aim higher, achieve, and succeed.