Thursday 4 April 2019

Can we please stop using switch most of the time

I am not talking about a light switch here
Allow me to elaborate, the switch statement I am referring to is in Java, it is the same switch statement you will have seen in C (or B for that matter if you're old enough).

The switch statement is a very simple representation of an indirection vector table we've all seen it for example
String reportNumBytes(int size, int modifier) {
  switch(modifier) {
    case 0:
        return String.valueOf(size);
        break;
    case 1:
        return String.valueOf(size/1000) + " K";
        break;
    case 2:
        return String.valueOf(size/1000000) + " M";
        break;
  }
  return String.valueOf(size);
} 
There are many things wrong with this code, the arguments aren't final, the method name is poor the modifier is a cause for magical numbers to be used, there are multiple return points but the main point is it uses a switch statement. Also try not to get too hung up on the fact that I am dividing by decimal devisors rather than binary divisors, this is largely irrelevant for the discussion and I don't want to get involved in the endless KB KiB debate. Let's remove the noise items from this list first
enum SizeModifier {
    BYTE, KILOBYTE, MEGABYTE;
}

public String reportNumberOfBytesUsingModifier(final int size, final SizeModifier modifier) {
  String result = String.valueOf(size);
  switch(modifier) {
    case BYTE:
        result = String.valueOf(size);
        break;
    case KILOBYTE:
        result = String.valueOf(size/1000) + " K";
        break;
    case MEGABYTE:
        result = String.valueOf(size/1000000) + " M";
        break;
  }
  return result;
} 
Now we can remove the magic numbers, or rather we can move them to the enum and it all begins to look far better.
enum SizeModifier {
    BYTE( 1, ''), 
    KILOBYTE( 1000, 'K' ), 
    MEGABYTE( 1000000, 'M' );
    final int divisor;
    final char identifier;
    SizeModifier(final int d, final char id) {
        divisor = d;
        identifier = id;
    }
    String toShortHand(final int size) {
        return String.format( "%d %s", size/divisor, identifier );
    }
}

public String reportNumberOfBytesUsingModifier(final int size, final SizeModifier modifier) {
  return modifier.toShortHand( size );
} 
Of course I do still rather like the creation of a command dictionary :) but...
private Map> commands;
...
commands.put( "byte", (v) -> return String.valueOf( v );
commands.put( "kilobyte", (v) -> return String.format( "%d %s", v/1000, "" ) );
commands.put( "megabyte", (v) -> return String.format( "%d %s", v/1000000, "" ) );

public String reportNumberOfBytesUsingModifier(final int size, final String modifier) {
  return commands.getOrDefault( modifier.toLowerCase(), (v) -> return String.valueOf( v ) ).call( size );
} 
in this instance I think it makes it less readable

Why clever people are dumb

why are smart people dumb?

There's someone that I know is a very clever person, at least they're very clever within the confines of their chosen academic area, however he displays so many right wing views that it has led me to reconsider my current understanding of people.

I have never understood why people believe in things that are proven to be wrong by the facts that surround them.  One such undeniable truth is that countries that don't allow guns simply have fewer gun related deaths.  It isn't even a question of corrolation vs causation you need only look to Australia for evidence of that one gun crime high, then they ban guns, gun crime drops play around with the figures all you like that's the basic run of things.

So a very clever person believes that "my safe place is where my gun is" that oh so brilliant stroke of genius spoken by the NRA.  Just take a look at the statistics of the number of people killed by their own weapons to know this is a silly statement to make.

Look at the source, the NRA, an organisations whos power is derived from gun sales, is promoting the ownership of guns well durrrr.  A business is in the business of making money and the NRA's mission is to keep power.  I must admit they're very clever, they've managed to alter the wording of a text otherwise considered to be of almost religious content the US constitution so that "A well regulated Militia, being necessary to the security of a free State, the right of the people to keep and bear Arms, shall not be infringed." simply ignores the bit about 'a well regulated militia' because it doesn't suit their puspose.

The clergy are responsible for a very similar thing, that time it was to keep the lower classes under control because after all they far outnumber the people in charge, as long as they don't realise that fact they'll never be in danger of an uprising.

Both the NRA and religious organisations have done their job so well that the people they seek to keep inprisoned within invisible bars of belief are the very people that defend the ideals they've been given by the bodies in power to the death believing them to be beliefs of their own reconing, if it wasn't such a sorry state of affairs it would be comical.
 
Stack Overflow profile for Richard Johnson at Stack Overflow, Q&A for professional and enthusiast programmers