Drawing Blanks

Premature Optimization is a Prerequisite for Success

Archive for the ‘programming’ Category

Non-numbers

leave a comment »

Here is how double.IsNaN() implemented, it’s funny:

/// Returns a value indicating whether the specified 
/// number evaluates to a value that is not a number 
public static bool IsNaN(double d)
{
   return d != d;
}

The double type can get you in trouble because it can store things like NaN and Infinity. It will silently divide by zero, take square roots and logarithms of negatives, and keep calculating, and you will have a hard time finding out where exactly your math is failing. It gets particularly funny when your formulas are right, but because of loss of precision your value under Math.Acos() becomes slightly greater than 1…

Sometimes you’d get a number when you would expect a NaN, e.g.

Math.Pow(0, double.PositiveInfinity) //is zero and not NaN
Math.Pow(1, double.PositiveInfinity) //is one and not NaN

 

And there is no such thing as double.IsNumber(), so if you want to check whether your calculation succeeded you need to check .IsNaN() && .IsInfinity()

So it’s a lot of fun…

Written by bbzippo

07/19/2012 at 4:10 am

Posted in programming

GoDaddy, 302 random redirect and Google

leave a comment »

Xworder used to hold the top (2nd-3rd place) position in Google search results for find words from letters. It’s still at the 3rd place on Bing, but Google now only mentions Xworder at the bottom of the 2nd page.

It took me a while to understand what’s going on. Apparently, the GoDaddy server that hosts Xworder issues “random redirects”:

302

Google hates those redirects and when it sees them often, it stops indexing the pages and demotes their search ranking.

You can find lots of blog posts describing this issue and blaming GoDaddy; many people are abandoning GoDaddy because of it. 

Those redirects are part of implementation of a threat management system. They are used to analyze traffic patterns in order to automatically detect DDOS and malware probe traffic from botnets. The redirects are issued whenever the site is first visited from the particular IP within a certain time interval (“once per session”).

Most likely, the TMS is being implemented by a 3rd party vendor, not by GoDaddy internally. There are some reports that the same redirect behavior has been observed at other hosting providers too.

Of course it sucks that the people who designed that TMS never considered its impact on SEO.

But what really sucks is Google. A chain of 2 redirects breaks Google!? What’s up with that?

I’m guessing it’s because of the way Google implements redirect loop detection. When they see a redirect back to where they’ve been before, they consider it a loop. But it’s not a loop.

GoDaddy says they are working on this (no ETA), but shouldn’t Google be working on this too?

Written by bbzippo

01/10/2012 at 10:12 pm

Posted in programming

Zen and the art of batch files

leave a comment »

I often use batch files for quick and dirty scripting tasks. Much more often than the modern scripting tools like Windows Scripting Host and Powershell. Because I’m familiar with the concepts of batch scripting since DOS 3.0. Figuring out how to accomplish a simple task using the modern tools would take me longer than writing it as a bat file. Even though bat files are outdated they have been evolving. The capabilities of cmd scripting and the command line utilities in Windows 7 and Windows Server are way superior compared to DOS.

I’d like to share some bat file tips and tricks using this example (provided as is, for demo purpose, not intended for production use):

@echo off
rem Recursively Deletes all files older than the specified age
rem from folders specified in the folderList file and logs output to a file.
rem folderlist file format: "<path>",<mask>,<maxAgeInDays>
rem e.g.: "C:\aaa",*.tmp,30

(set folderList=%~dp0folderList.txt)
(set logpath=%~dp0)
(set datestamp=%date:~10,4%-%date:~4,2%-%date:~7,2%)
(set log=%logpath%\%datestamp%.log)

echo %datestamp% %time% >> %log%
for /F "tokens=1-3 delims=," %%f in (%folderList%) do (
echo %%f %%g %%h >> %log%
forfiles /p %%f /m %%g /d -%%h /s /c "cmd /c if @isdir==FALSE echo @file >> %log% & del @file >> %log%"
)

Things to notice:

  1. The parenthesis around (set var=val). I use them to avoid the blank space issue.
  2. What is %~dp0 ? It is the path where the batch file is located. %0 is the full path to the current script, and the ~dp parses the drive and path out of it. This variable is especially useful in Windows Vista and higher since when you run the script “as administrator” it is not started in the folder in which it’s located.
  3. Parsing the %date% variable using ~Pos,Len to create the log file name. Note that this method relies on the date format set in the system locale.
  4. Using the for /f operator to parse a text file.
  5. The forfiles command.

Written by bbzippo

11/26/2011 at 2:52 am

Posted in programming

The place of HTML5 in Windows 8

with 2 comments

First, a rant. “HTML5” is a buzzword. When you hear people talk about “HTML5”, what they talk about is:

Canvas and WebGL, Plugin-free video, Excessive JavaScript, CSS3, HTML5

Most people don’t care about business app development. They mostly get excited about the ability to present graphics and video and to program simple games without plugins like Flash or Silverlight.

And those who do care about business app development got excited when they heard from Microsoft that HTML5 will be the language of choice in the WinRT (Metro App) framework. Well, people always get excited when someone promises them PORTABILITY. They just can’t stop believing in the Portability Myth.

Folks,

PORTABILITY DOES NOT EXIST. 

In particular, HTML5 in Windows 8 will NOT be a tool for developing portable applications. In fact, HTML5 is NOT going to be a Windows 8 app development tool at all. Take a look: http://msdn.microsoft.com/en-us/library/windows/apps/br229565(v=VS.85).aspx . Do you see “HTML5” mentioned anywhere in the documentation? Is this HTML5?:

<div style="display: -ms-box;">
     <div data-win-control="WinJS.UI.DatePicker"></div>
</div>

Folks,

The so called “HTML5 applications” on Windows 8 will in fact be developed using JavaScript, PROPRIETARY HTML EXTENSIONS that will allow you to use the WinRT PROPRIETARY controls and APIs, and some CSS3 for layouts (although you will mostly be using PROPRIETARY layout containers).

So don’t get excited about portability.

In fact, it’s going to be much easier to port applications between WinRT XAML, Silverlight and WPF than between WinRT “Html5” and in-browser Html5.

MS is introducing and hyping HTML5 only to attract developers who are used to JavaScript/DOM/CSS coding.

Written by bbzippo

11/20/2011 at 11:39 pm

Posted in programming

The Flyweight Design Pattern

leave a comment »

I think that the Flyweight Pattern is underrated. It is not all about saving memory. In many cases, Flyweights also add convenient syntax and clear semantics to the code. They are almost like “enums on steroids”. Here is a coding idiom that I frequently use:

class Flavor
{
    private Flavor(string displayName, Color colorCode)
    {
        this.displayName = displayName;
        this.colorCode = colorCode;
    }

    private string displayName;
    public string DisplayName { get { return displayName; } }
    private Color colorCode;
    public Color ColorCode { get { return colorCode; } }
    
    public static readonly Flavor Vanilla = 
        new Flavor("Vanilla", Color.FromRgb(0xF4, 0xE7, 0xC6));
    public static readonly Flavor Chocolate = 
        new Flavor("Chocolate", Color.FromRgb(0x8B, 0x45, 0x13));
    public static readonly Flavor Pineapple = 
        new Flavor("Pineapple", Color.FromRgb(0xEE, 0xEE, 0x00));

    public static IEnumerable<Flavor> AllFlavors
    {
        get { yield return Vanilla; yield return Chocolate; yield return Pineapple; }
    }

    public static Flavor Parse(string displayName)
    {
        return (from f in AllFlavors 
                where string.Compare(f.displayName, displayName) == 0 
                select f).First();
    }
}

 

Now we can do tons of convenient things like:

icecream.Flavor = Flavor.Chocolate;
if (icecream.Flavor == Flavor.Vanilla) {…}
comboBox.DataContext = Flavor.AllFlavors;
productPanel.Background = new SolidColorBrush(icecream.Flavor.ColorCode);

Written by bbzippo

10/26/2010 at 5:18 am

Posted in programming

Pretty Orbits

leave a comment »

I’d like to share some “computer-generated art”. Below are images generated by a program that I wrote in 1997. It was written in Delphi 1 under Windows 3.1 on a 25MHz 386 PC (yes, one with a “turbo” button). The executable still runs just fine (but not much faster than it used to) under Win 7 on a modern PC.

The program is really simple, it just plots a discrete R^2 map (i.e. a sequence of points). The formulas are very simple too. Most of them are based upon the Gingerbreadman map http://en.wikipedia.org/wiki/Gingerbreadman_map (btw, “gingerbreadman” has tons of stupid anagrams)

qmw1 qmw2

Read the rest of this entry »

Written by bbzippo

10/20/2010 at 3:42 am

Posted in fun, math, programming

The illusion of reuse; the fallacy of misnomer

with 2 comments

Udi Dahan expressed many interesting thoughts here http://www.udidahan.com/2009/06/07/the-fallacy-of-reuse/ . Those thoughts strongly resonate with some of mine, although I don’t fully agree with everything he says. I just want to rant and whine a bit share some of my observations on this subject.

Someone writes a component that performs a complex business function and has a very simple interface. You just call Machine.Work() – it’s that easy. It’s extremely easy to reuse, and this reuse saves you tons of lines of code. So it’s claimed to be highly reusable. But it’s not. Under the hood of this Machine there are so many responsibilities and dependencies, that make it too specialized. So the reuse is really limited to a handful of scenarios. No problem – we can parameterize the machine at the cost of just a few extra lines of code for the caller. Machine.Mode=a; Machine.Kind=b; Machine.BypassUnneededStuff=true; Machine.Work(). Well, you see, in my particular case the resulting behavior is not precisely what I expect, in addition you introduced bugs because you didn’t cover all m to the power n combinations of parameters in your unit test. And the next set of parameters that you add will likely break other use cases.

This is how in my opinion extra dependencies are introduced and the SRP and loose coupling are sacrificed in the name of illusory reuse. But real Reuse is sacrificed too! “Illusory reuse” means that reuse was the intent, but it’s not the result. (Note: When Udi writes “dependencies multiply by reuse”, he means something very different. But I’m not arguing)

Building blocks are more reusable than the machine that is built out of them. So please give me access to those blocks and I’ll just build the machine that I need. This is what Design Patterns are all about. Loosely couple reasonably sized blocks. Reassemble them to solve the task at hand.

A couple of real-world examples.

Read the rest of this entry »

Written by bbzippo

10/01/2010 at 8:23 am

Posted in programming