A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label number. Show all posts
Showing posts with label number. Show all posts

Wrap those magic data structures with a class

You understand the the dangers of using magic numbers in your code. And magic strings are another face of the same issue. When you use a lot of strings that have special meanings, it makes your code smell bad, i.e. it's an indication of low quality. These are not user messages or log messages, but rather a fixed string that if mis-typed will break the systems functionality. But it's not just the basic variable types that are magic. Arrays and hashes can also be magic, in the worst possible way.

If you find yourself using a hash in a lot of different places, this can be thought of as a magic hash. It may be a simple hash or it may contain many nested arrays and other sub-hashes. Every operation on the hash has to be done in exactly the right way or it won't work. It's very easy to perform a operation wrong and get unexpected results that won't be detected immediately. You're writing a significant amount of code to read and write the data within the hash, and to catch errors, and you're likely to be creating bugs too. The more code you write, the more bugs you create. This similar, duplicated and boring boilerplate code is spread around all over the application wherever the hash is used. It's also likely that you will need to use magic strings for the hash keys, with all the problems they bring. Even if you use an array at the top level, there may be hashes within it.

The solution is to put a class around the hash, so that you only need to write the hash manipulation code once, and can thoroughly unit test it. All the code related to this data is encapsulated in one place. All calling code will interact with the class interface instead of the hash directly. This is an example of object-oriented development, where objects are passed around and manipulated instead of raw data structures.

P.S. Even without using a class, replacing magic strings with constants would be a serious improvement. Maintainers will be unable to accidentally get a string wrong without seeing an error message that makes it very obvious what is wrong. It's a more foolproof way to develop.

Hide number sending on Blackberry Bold

  • Open phone application
  • Press menu
  • Choose 'options'

XSL data types

http://www.zvon.org/xxl/XSL-Ref/Tutorials/Built-in-Types/bt1.html

<xsl:stylesheet
xmlns:sch="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
exclude-result-prefixes=" sch">

<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

<xsl:variable name="a" as="sch:integer" select="12"/>
<xsl:variable name="b" as="sch:float" select="12.123456789123456789"/>
<xsl:variable name="c" as="sch:double" select="12.123456789123456789"/>
<xsl:variable name="d" as="sch:decimal" select="12.123456789123456789"/>

</xsl:stylesheet>

Parents node's position in XSL

1. Count parents using <xsl:number>
Number can be extracted:
($n)=$c=~/(\d+)\.\d+\.\d+\.\d+$/;
and adjusted:
$n=((($n/2)-5)+1)

<xsl:number count="." level="multiple" from="/page" format="1"/>

Or with format="a", interpret like j=1, l=2, n=3, p=4, etc.

2. Count parents using position()
Simpler -- but apparently this is a weird way to do it. Probably inefficient too.

<xsl:value-of select="count(parent::*/parent::*/parent::*/preceding-sibling::*) - 3"/>

seq on Mac?

Mac doesn't have seq like Redhat does.

instead of: seq 1 10
seq [first] [increment] [last]

try: jot 10 1 10
jot [reps] [start] [finish] [step]

Combinations, permuations and iterations

To generate all combinations or permutations of a fixed list, use Math::Combinatorics

To generate sequential values for a set of slots, e.g. all binary numbers, or the number of 'max_results' for several modules do this:

  1. add one to the first slot
  2. if that takes it over the maximum, set it to zero and add one to the next slot
  3. repeat step 2 until you reach all zeroes
  4. if all zeroes, you are finished.

Perl sprintf

        # Format number with up to 8 leading zeroes
$result = sprintf("%08d", $number);
        # Round number to 3 digits after decimal point
$rounded = sprintf("%.3f", $number);

Perl's sprintf permits the following universally-known conversions:

   %% a percent sign
%c a character with the given number
%s a string
%d a signed integer, in decimal
%u an unsigned integer, in decimal
%o an unsigned integer, in octal
%x an unsigned integer, in hexadecimal
%e a floating-point number, in scientific notation
%f a floating-point number, in fixed decimal notation
%g a floating-point number, in %e or %f notation