Wednesday, February 15, 2012

Functions with Multiple Exits

Dr Bob suggests that multiple exit points make a function harder to maintain.

With respect this is one of the few times when I completely disagree.

For any function call the best practice is to validate all inputs, do some calculations, and then do the main event. At each step in the process we have a potential for an exit.

Is the input invalid? Exit
Is the calculation out of range? Exit.
Is there a trivial answer? Exit.

The goal of an early exit is to make code run faster. Test for the simplest possible answer and take that exit if you can. When I teach this I always tell people the first and most important question we ask ourselves all the time is "Are we done yet?"

Some people like to set a flag in the code. Loosely pseudocoded as:
if (some dumb stuff) then
Error:=1;
End if;
More code
if (Some more dumb stuff) then
Error := 2;
end if;
More code
if (error > 0) Then
result := Error;
else
result:= myResult;
end if;
Return result;
End function;

Some how this is said to be less clear than
if (some dumb stuff) then
Exit(1);
End if;
More code
if (Some more dumb stuff) then
Exit(2);
end if;
More code
Return myResult;
End function;

Well, I think the answer is clear just from the pseudocode. But the discipline to code that way takes some practice. In this example it isnt really all that bad, but in practice all those ifs get pretty crazy. We end up deep inside a loop or some calculation, find an error somewhere in left field and then have to code a bunch of flags from deep deep. The flags usually end up making a mess of the code, and so instead programmers throw lots of exceptions when they should just return a valid value. I cant remember how often in code reviews I have punted a function definition because it needed to return zero or something as a valid result instead of throwing an exception. I am a sticker for results and wonky function results are not acceptable even if the pain of making them is quite large. Now we have a language construct to help us. Will it get abused? Ofcourse. But it will also make code clearer when it is applied well. At least the intention is clear with Exit(x); There is no where else to go but out of the function call. If we need to play with variables, flags and conditionals to force execution stoppage that will definitely add to the fray.

Now I need to figure out what happens in a try .. finally block that has an exit. I hope the cleanup code runs. Pretty sure it will.


Components: install: reinstall: and again.

The problems with installation of third party components begin when the premises of Delphi are forgotten, or when too much help is offered. TMS Software offers an amazing collection of stuff. They are not the only component set I bought but they are the biggest.

Components have 2 parts. Design time, and runtime. The design time stuff usually works flawlessly, but somehow runtimes are more problematic. The library has to find the path to the runtimes, but the design time stuff is managed by the compiler. As soon as people have options, the fun starts. Adding Microsoft benevolent dictatorship to the mix with ever changing rules about what can be installed where and the proverbial dog's breakfast ensues.

Delphi XE2 is cross platform, multiple compilers with multiple targets. It is also multi-lingual with a lot of stuff in several languages, not just english.

Is that a challenge to keep straight? You bet it is.

Evening three of the installation has me gasping for breath. There are so many things to install and configure.

I started out putting stuff on the network drive. Turns out that is a no-no.

Uninstall number two happened when I tried to use the setup.exe that came with programs. Some stuff breaks when it is not installed in the default location.

Uninstall number three happened when I read the directions for manual installation wrong, and just ended up with the runtimes some how. Still not sure what went wrong there.

But forth time lucky. Now I just have the specialized packages for CE and the web stuff to go.

I got a bit upset for nothing when I found that TMS Unicode was 32 bit only. Then I realized that I probably dont need them because CE2 has it already. Oh well, if I dont use them it is simple to uninstall.

The process continues. TMS Instrumentation Workshop looks so cool. The components are sweet too. Smooth animations are something I want to play with.




Delphi XE2 Enterprise

I am jumping back into Delphi again after years. I decided to blog the experience.

History: My story When I left San Diego more than ten years ago I had a fresh copy of Delphi 5 in my truck load of household belongings. The language was fresh and exciting, even though that version was a bit old, and I had not touched it in a year and a half as I moved more into architecture and database work.

Good intentions to go back and dig out the CD, install it and resurrect my old passion for coding met with no real success. Oh I did a lot of coding in other languages. But my focus remained on Architecture and in large measure it still does. But when I looked over at the latest and greatest Delphi offering I saw a jumbled mess of dot net and windows coding that seemed to try to go in every direction at once, but failed to be convincing in any of them. In short, the team had lost it's focus. They didnt really know how to drive the ship any more, and it was a mess.

History: Wandering around a bit This was not a compelling story for me. Distracted with many responsibilities and digging deeper into ERWin, data modeling, and deeply understanding client's needs took all my energy. Someone wrote a book about it. As Eric Evans worked on his great lakes shipping project, I sat in San Diego and worked on a few projects. We had a few developers in common as people came and went and I heard a lot about this guy from the east who thought a lot like I did about software design. At the time I was famous for 'taking too long to get started' on every project. A few years later Evans wrote his classic “Domain Driven Design” that explains why. Everyone else was exploring design patterns and talking about the roots of architecture at the time. The thing that impresses me the most about DDD over any other approach is that it deeply understands how people work. So it doesnt try to put peoples round heads into square boxes that dont fit.

Soap Box Sermon: Architecture Nothing about software design and development will ever be really easy. Most real developers dont care about how hard it is. They are excited to know it is systematically possible given a vast amount of effort. Mere mortals who need a social life need not bother. There is far more sweat and pain involved in programming than in any other intellectual discipline. Some people think lawyers have it hard. Law is a trivial joke in comparison, Programmers know everything about everything, or they know where to look. There are no appeals. Even mathematics allows for an occasional jump to conclusions.

Taking a look at the meandering romp of a path my so-called career has taken you will find that it covers a lot of ground. So far what you have seen of my writing style will lead you to a perception that I am at once reflexive in thought, and all together all over the map all at once in my dialogue. Does it make it hard to follow? A lot of people will have given up already just because the train has wandered so. The people who read and love the organic; casual but deeply thoughtful consciousness are the ones who will persist through these musings any way so there is little point in creating a pedanticly structured analytically dry-as-dust academic tome. Although I have been persuaded to write that way on occasion by persistent co-authors.

Soap Box Sermon: Delphi And yes, Delphi was a mess in those days. This can sometimes be helpful if it is an organic mess of comfort and ease, where the clutter has a familiar nested feel and the ritual routines are purposeful, non-taxing and sociable. But in those days none of that was true of Delphi. Dot net is constitutionally a valley of pain and arbitrary big brotherishness borne of too many egos, too many compromises, and just plain bigness of britches. Even the 4.0 offering is barely good enough. It is a world for IT egos that need entire valleys full of digital serfs to bend, staple and mutilate long long long toilet rolls full of code just to put a few controls on a form. Architects who insist on dot net, or the even bigger dirtier toilet rolls of Java mumble incantations about security, cross platform, multi-tier (or multi-wet bawling agonies in Java) Enterprise design patterns, and Ood. All of this was simple stuff in Delphi 5. In fact we could do it without writing much code. And it worked pretty much flawlessly. The only problem was Borland couldnt leave dot net out of the picture. They had to muck in the latest and greatest. And much ado was made about … nothing good anyway.

Viva la difference; eventually All this mess lead me to dismiss Delphi. Then 3 years ago the light began to appear as Borland gave up the code. Embarcadero did not endear me at all. The business plan was and is tough stuff. They want to get paid. Well so do I. And then they did something crazy that I loved. They promised to deliver on it, to make it worthwhile to live with the tough business plan and make a deep investment again.

Return to the main point: Clarity. Focus. Vision. A plan. And no more mucky dot net. That was 2010. I was seriously thinking it would be great to go play in my old sand box again but the price was a bit high. Well, that was then and now FireMonkey. A friend of mine who once worked on the deep core of Crystal Reports (Hi Drew) said any product that combines the words Fire and Monkey has to be great stuff for programmers. Well yeah, but sexy cross platform development that lets you do good MVC design but doesnt INSISIT on it also great for programmers.

Ubbersmartness culture Delphi was always a great patient teaching language because it made stupid hard to do, and smart was always easier. Really clever and really cool is part of the culture of ubersmarts that I love. Or at least a few emails and 3 tricks that got turned into a component eventually smart. And that was and is fantastic. Now cross platform at the bare metal is killer if it works. So what do I think so far? Were back!

The pain of commitment I laid out four grand on XE2 Enterprise, component packs from TMS etc. and I am jumping back in head first with money out of my own pocket. I dont expect anyone else to get it yet. I cant expect bosses who have never heard of it or seen it go do much to help out. As it is I consider myself fortunate to be able to buy now and pay for it on an employee purchase deal. It might seem crazy but I love programming, and I hate writing the same code over and over and over. I can do SO MUCH STUFF with this thing...


Tribute to my bud N8 So as a few Canadian friends say at such an inspiration 'Lock and load baby, were goina hunt us some bear.' In case you are not inclined to regard such low brow enthusiasm with encouragement, I can only offer a simple but equally cultured epithet. Build a bridge and get over it.

About Me

Winnipeg, Manitoba, Canada
Messianic Jewish adherant Software architect music enthusiast