Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Monday, 6 April 2015

Polymorphism with Go

A few days ago I started making my first steps with the Go language. As I am a real newbie in Go, take everything I am going to write here with a (big) grain of salt.

To a large extend, this (and any next posts on Go) reflect my personal journey with the language, from the very basics, to the (hopefully) mastering a new language.

Although Go takes a (few) step(s) away from the traditional object-oriented languages (think Java, Ruby, C#), so far I find it totally possible to write good object-oriented code.

Take for instance polymorphism. The program below show how you can define and execute code that acts much like overridden polymorphic methods. This code also show one very, very nice Go feature -- strong support for composition. Take a close look at how Rect composes a Square within itself, which is basically how we say that a Rect is a Square (well, you can argue that mathematically it's more like the opposite, but it was easier for me to structure the code this way) and as such, all Square's members are automatically accessed via Rect with no call forwarding. This lets us using simply
this.a * this.b
and not
this.Square.a * this.b
although this would also work fine. In other words, syntactically and semantically, it's very similar to what we achieve with inheritance in traditional OO languages.
package main

import (
	"fmt"
	"math"
)

func main(){
	cir := new(Circle);
	cir.rad = 10;
	calc_area(cir);

	square := new(Square);
	square.a = 100;
	calc_area(square);

	rect := new(Rect);
	rect.a = 10;
	rect.b = 50;
	calc_area(rect);
}

// common interface for all shapes
type Shape interface {
	area() float32
}

type Circle struct {
	rad float32
}

// just for convenience square and rectangle are inverted 
// i.e. a rectangle is a square which is probably not that 
// right from purist OO standpoint but makes the code more clear 
type Square struct {
	a float32
}

// rectangle is a square with additional piece of information 
// regarding side
type Rect struct {
	Square
	b float32
}


// 'polymorphic' methods in action
func (this *Circle) area() float32{
	return math.Pi * this.rad * this.rad;
}

func (this *Square) area() float32 {
	return this.a * this.a;
}

func (this *Rect) area() float32 {
	return this.a * this.b;
}

func calc_area(this Shape){
	fmt.Println(this.area());
}


This code produces the following output:


Saturday, 30 March 2013

i += ++i + ++i

Dr. Starke has a great post of what happens if you try i = 1; i += ++i + ++i; in different languages.

It is definitely worth reading if you are a programmer. 
It is also quite amusing.

One thing that puzzles me, though, is that he says 8 is 'a bit off' as a result (he even goes on to poke some fun at PHP: "Eight again. So many commercial websites are build with php - and they all calculate like that? re-consider your e-commerce behaviour..."

Well, let's give it a thought:

i = 1;
i += ++i + ++i;

Depending on the language semantics and implementation, you end up with different results.
PHP logic obviously goes like this:

1. Calculate the right side of the expression;
2. Add whatever the result from step 1. is to the value stored in i;

So initially, i is 1. The first ++i makes it 2. Ah, 2, PHP says, let's remember that. Then the second ++i increments i to 3. PHP then adds 2 to 3 and then adds the result to the current value of i (which is 3). So here we go:

i += 2 + 3 => 8

So eight seems like a legit answer to me.

What about the other answers? 
Let's start with C / Objectvie-C first. The result produced by C is 9 which seems quite puzzling at first. But consider that C is very low-level and close to the metal language. While the general logic stays the same as we outlined with PHP, C implementation obviously has a twist. And the twist is that the second ++i will also update the result from the first one as it works over the same memory location.
You see, when PHP calculates the first ++i, it updates the value in i to store 2 and then remembers the outcome (also 2) in a separate variable. Thus the second increment of i has no effect over it.

C in contrast remembers not the outcome itself, but the address in the memory, where the outcome is stored. And as the memory location is one and the same for the two ++ operations, C ends up performing 3+3 and then adds the result to the contents of the same memory address (again 3). 

As for C#, Java and JS, which all end up with 6, it seems that they simply translate the i +=... operator to i = i +... And so they end up with an expression like i = i + ++i + ++i which translates to i = 1 + 2 + 3.


The bottom line, of course is that it is amusing and somewhat bizarre (scary?) how differently languages can behave in situations when you expect them all to do the same thing. But as one commenter puts it: Whoever writes such code deserves whatever answer they get.


True words.

Thanks for the link, @softmodeling


Update: Note that most of the above explanations actually describe the behaviour of the compiler. For instance, when I write 'C in contrast remembers not the outcome itself, but the...' it is actually the compiler (gcc) that does that.

Thursday, 27 September 2012

Code Monkeys

Quite too often I see companies developing business software overusing external parties (business consultants, analysts call them what you will) to deal with all aspects of communications with end users.

When you are working in the corporate world, -- the thinking goes -- you must show the clients how great you are in their domain. You need the expertise, the knowledge! Developers are experts in dealing with code, right? Everyone should do what he's best at.

While there is nothing wrong with working with business consultants (it is inevitable), there is often a trend in such environments to delegate all the communications with the users to them. Developers then become just code monkeys (no offense intended, I consider myself being a code monkey) that are only occasionally contacted to explain or validate some difficult technical aspect of the picture.

At best.

After all, developers are supposed to do that coding thing, right? Hey, why they have to even be on this continent to do the job?

Right?
Wrong.

For thousands of reasons. Let's go through each one of them.

Just joking.
Everyone could have his or her own opinion on that, but for me one of the most important things for a developer, designing and coding business software is the direct contact with the users. For at least two reasons:
  1. You must see how your code is doing in real life. And by 'see' I don't mean digging through the logs or reading emails with user feedback. You must be there, with the person who will use the code you've written in his day-to-day work.When you see him struggling, when you see your work through his eyes, you gain a priceless perspective. Priceless. Let's face it: You are not going to spend your days (and sometimes nights) in front of the screens you just developed. But your users will. And how they feel in 5 p.m. every day is largely up to you. They can be happy or they can rush into the nearest pub for a few bourbons just to forget. You can make a person alcoholic. Just because you've messed up the tab order. Seriously.
  2. Your users must see you. They must understand your perspective. Chatting for a few minutes does  much better than a 60+ messages (hate-)mail thread. Even the most stubborn and unreasonable people tend to change or at least soften their opinion when you explain them why something won't work or another thing was done in that way. Because, we, developers have the unique ability to detect flaws and potential problems much quicker than the other people. Your perspective is unique. The users need it. Plus, hopefully, they will also see you are not some sort of a hairy bad-smelling monster eating chips over his laptop. Unless you are  (in which case just ignore this whole post and just keep doing what you're best at, whatever it is).
Business consultants are (as a rule) great experts and (well, sometimes) fine people. But they cannot replace the direct contact between the guy writing the code in Eclipse /or Emacs or VS  or Xcode or Vi -- no need to start IDE war here/ and the one spending his days working with whatever functionality that code produces.