Think Before Coding

To content | To menu | To search

Monday, December 29, 2008

Asp.Net authentication cookie oddities...

Back to low level considerations.

In order to use a specific and shareable encryption scheme between sites, we had to bypass the FormsAuthentication.Encrypt method an write a new one.

Something strange happened then. When using our implementation, the cookie disappeared !

The cookie was added to the Response.Cookies, but was not present in the Request.Cookies of the following request.

After deep search, it appeared that the cookie was actually sent to the browser. Why wouldn’t it be sent back ?

The really weird thing was that the cookie data was in the Response.Headers, but not in Response.Cookies !!!

It seems that when parsing the Cookie header, Asp.net strips off the .ASPXAUTH cookie if it cannot be decrypted by FormsAuthentication.Decrypt !

The solution was to use another cookie name, and everything was working again !

If it can save 2 hours of your precious time…

Wednesday, December 24, 2008

Data Structures and Algorithms

I had a peek through Jon Skeet’s blog this morning at a free eBook called Data Structures and Algorithms by Granville Barnett and Luca Del Tongo.

The book is clear and presents the usual linked lists, trees, sets structures in a concise yet precise way.

There’s something new I had not seen in other algorithm books before. The algorithms are written in pseudo code, but there is a chapter about unit testing your implementation.

If the writers read this, I just would like to share a little tricks that make linked list algorithms easier to write..

Linked list algorithms are always bloated with tests like this :

        if (head == null)

            head = ...;

        else

            node->Next = ...;

Actually the content of head and then content of node->Next are both pointers on next node. But the way to reference those two locations is different, ending in a lot of if statements.

If the language supports reference variables or pointer, you can use a double pointer to hold the current position :

Node** next = &head;

This way there is no more difference between the head (*next) and nodes Next pointers. The little tricky thing is to move to next location :

next = &((*next)->Next);

With this you can consider every ‘Next’ pointer including head as equivalent. No more if statement !

By the way, I was trying to find out how to do this on C#, but is it possible without going unsafe ?

Monday, December 22, 2008

DDD: Specifications

There's an interesting post by Greg Young about Specifications.

I'm currently refactoring some code to move constraints and validation in specification classes, and it really gives a better insight at what is constituting entities and what's not, especially when there are multiple level of constraints (persistence constraints, business constraints...)

Friday, December 19, 2008

Coding and writing - The form

There is a number of common points between coding and writing

Spelling

The most basic mistake... Oops I wrote retrun instead of return... hopefully modern text editors check it on the fly.

Syntax

Correct sentences are made of word in a correct order. Here again, on the fly checking helps a lot.

Presentation

Is your code visibly clear... Enough space between parts, but not too much. Is you code clearly indented.

Style

Style is even more subjective. When coding, usually stay concise, but there are a lot of parameters here.

You can :

  • choose if statements or ?: alternating operator
  • define fluent interfaces to do things on several lines
  • use linq to express your intent more clearly,
  • use var implicit type or not
  • use anonymous types or not
  • use lambdas or functions

The important thing is that your style should fit some purpose, the style should emphasis important parts of your primary intent.

Structure

A book is divided in parts, chapters and paragraphs. The code is divided in files namespaces and classes. The structure of the code should help the reader go straight to what is the most important to him and understand how things are related.

 

Next time, we'll talk about substance. Tell me if you see more analogies !

Wednesday, December 10, 2008

Wow - Book review - Domain Driven Design!

Ok, I finished Domain Driven Design yesterday. I think I had no such Aha moment since I read Design Patterns eleven years ago...

I thought I was prepared for it, I had read many things to get maximum info...

I had :

I had practiced knowledge crunching on my own for long.

But the book go far beyond all that. It has a deep vision of programming on large scale projects, and give really good insight on difficulties that arise when working on complex systems with several teams. These are really precious pieces of advice.

The pattern presentation of the different chapters, even when those patterns are not implementation patterns but team organization patterns is really helpful and clear.

There is not much about implementation details, but it is not a problem. The principles are not tied to a specific technology or framework. Actually this is even a good point, it leaves the book clean from a specific implementation. Implementation books are aging faster.

You definitely should have this book on your shelf.

Tuesday, December 2, 2008

Domain Driven Design

I had read a lot of stuff about it these last months, and I think I got the essence and the principles.

I've received the blue book today.

I'm sure I'll have matter for blogging these days...

Monday, December 1, 2008

Problem with the Select N+1 problem

Ayende proposes solutions for the Select N+1 problem in NHibernate.

Some will say it is a sign that lazy load is the work of the devil. But I can remember the time when people were saying that garbage collectors are evil.

We all would like to have non intrusive data fetching strategy that work 99% of the time !

The problem is not about lazy load or not. It is that you must take care about your data fetching strategy whenever you make a data access.

There is currently no technology smart enough to infer the right fetching strategy.

If someone builds it, this won't be a problem anymore.

If you know about it, please tell the world !

Ubiquity of language... lost in translation ?

I found two collapsing problems :

  • You should use the same language between domain experts and development teams (DDD)
  • You should use English for naming in your code (Design guidelines)

What if my domain experts don't speak English ?

Tuesday, November 25, 2008

CSS is too Tricky

I found through Alvin Ashcraft's Morning Dew a post about CSS tricks Six indispensable CSS tips and tricks I use on every project. Very interesting since it gives some ways to make cleaner CSS integration (I really like the Overclear one, don't need the clear both anymore !!).

But while reading this question came to my mind:

Why do we need to find crazy tricks like this for any single thing we want to do with HTML + CSS ?

This is the sign that HTML + CSS is a complete design failure !

When you want to make something as simple as putting several blocks on the same line... you must use float left, but it does not behave exactly has you expect it to do... When using Silvelight and WPF I had none of these problems. There are surely other problems with theses technologies but I have found an important difference :

  • In CSS, the elements decide how the fit in their parent
  • In Silverlight, parents decide the layout of their children

And it makes a huge difference. Because in CSS, the interactions between sibling children using very different placement models make it a n*n combination problem. For sure it's not manageable and no browser can handle every case gracefully.

In Silverlight, you obtain the layout you want by composition, and each rule remains simple. No need to use Jedi tricks to obtain what you want.

Monday, November 24, 2008

Coincidences just happen...

It's quite strange how ideas can appear at the same time in different places. This morning while take a shower I was thinking about a future post series about programming and building architecture and how things had changed in the last few years. Only 1h later I was reading this post by Justin Etheredge, that matches exactly this subject.

I'm still preparing the posts however.

Do you understand what it is all about ?

I want to be sure the title of the blog will not be misunderstood: when I say "Think before coding", I don't mean you should prepare everything before and the start coding like a robot. This is what is called waterfall model, and  and it has always been considered as as non-working model, even by its author. NotUnderstand

Programming is not applying a technique. When confronted twice to a problem, the programmer should not use the same solution, but understand what make both problems so close and build something that solve this class of problems. It can be a simple helper function, a small library or a big framework. This is the DRY (Don't Repeat Yourself) principle.

We all learnt how to implement a Quicksort algorithm at school. When was the last time you had to implement it? Personally I use the Linq Sort() extension method and it does the trick for me.

If you have to implement it for a specific reason, it should be because you understood that your problem was not exactly the same as the one handled by existing implementations.

This is why programming is not about technique or algorithm, if you're smart enough, you can adapt almost anything to any language or technology.

There is still one thing that tools and technology can't do for you :  understanding.

Programming IS about understanding

  • Understand the problems
  • Understand the processes,
  • Understand the people,
  • Understand the other programmers,
  • Understand yourself!

 

You should have a deep understanding of the problems and processes.

This is the base of modeling, wether it is domain models as in Domain Driven Design or infrastructure framework creation, through Separation of Concerns.

You should understand people.

Because they are the one that ultimately will use what you're building. People are usually hard to understand. This is all the matter about User Experience (UX), but even when programming a framework, you should understand how other programmer will use it and understand it, and how they'll build on it software for other people with their one expectations.

You should understand programmers.

You rarely write your code only for you, and you should write your code so that other programmer have the better understanding of what you intended to do, and what you understood about the problem you're solving by writing this code.

You should understand yourself.

Even when you write code only for yourself, you should understand what you wrote in the first place, as Andy Hunt and Dave Thomas say :

All Programming is Maintenance Programming
Bill Venners: You say in your book, The Pragmatic Programmer (Addison-Wesley, 1999), that "programmers are constantly in maintenance mode." Why?

Dave Thomas: All programming is maintenance programming, because you are rarely writing original code. If you look at the actual time you spend programming, you write a bit here and then you go back and make a change. Or you go back and fix a bug. Or you rip it out altogether and replace it with something else. But you are very quickly maintaining code even if it's a brand new project with a fresh source file. You spend most of your time in maintenance mode. So you may as well just bite the bullet and say, "I'm maintaining from day one." The disciplines that apply to maintenance should apply globally.

Andy Hunt: It's only the first 10 minutes that the code's original, when you type it in the first time. That's it.

But you should also understand how you do things, how you did things, how you changed, and how you will change in the future.  Things are moving so fast that you can not simply stay with your current knowledge and expect it to be sufficient in two years.

Friday, November 21, 2008

Must know English to be a programmer ?

I found through Scott Hanselman's blog a conversation starting from

"If you don't know English, you're not a programmer."

I was 8 when I started playing with BASIC on the TI-99/4A my father bought in the early 80's, and at that time I had never learnt an English word... But that was not a problem as long as the documentation was in my native language.

I had no need to know English since I knew what For, Load, Run, Save did. Ok my pronunciation was a bit sloppy (Load was Lo - a - d, with a big french a) but I knew what the computer would do when I used those commands.

Of course you can achieve a better style and discover libraries ways faster when knowing English, but I don't think it's a requirement. It's better though to use a widely spread language when you want to share your code. I've had already hard time reading Italian code, but this is more a problem about languages in general than a problem about programming.

Don't be stopped by your native language, programming languages are languages per  se, and you should learn a new [programming] language every year. Why not English ?

Thursday, November 20, 2008

'Save' is not a feature anymore

Pete Brown noted in this post that the save icon is still represented by a 3 1/2" floppy by most of the application.Floppy-Word

But what does the Save button really mean ?

Back in the floppy days, there was no fast persistent storage. To work at acceptable speed, you had to manage everything in memory, the take a few seconds to save at some point. At that time, most of the storage mediums were removable.

Today, things are different. You can't find a computer without a hard disk or solid state drive.

When an application proposes a save action you should understand :

  • My application stores my data in memory
  • If I forget to save it, my data will be lost !

As Chase Saunders states  in his comment this is what he calls a 'Make It Work' button !

Is this a feature ? It's a curse !

How would work a program without Save button ?

  • The document would be persisted continuously on disk.
  • It would have a default name (the first sentence ?) in a default location.
  • The program would display a list of available documents.
  • You should be able to Copy (instead of Save As) your document to another place.
  • You should be able to Delete it from its location.
  • For complex documents the program would propose versioning or labeling to mark important documents steps.

The world would then be a better place !

Wednesday, November 19, 2008

Back in the blogosphere !

I've been away from blogging for quite a long time, but here we go again !

You can find preceding posts on my previous coding blog. I will probably republish the most useful ones here.

page 3 of 3 -