terça-feira, outubro 30, 2007

Why do we need www?

/RANT ON
I'm not talking about the world wild west, I'm talking about why the hell do we need to type www to enter a website?

Try acessing: acer.com.

It didn't work did it?

Now try www.acer.com.

So you ask yourself what's so complicated about it?

Well, I dare to say lazyness, yes when you try acessing something without www and it doesn't work, you can call the webmaster a stupid lazy bastard, because that's what he is.

So if you are a webmaster, I'll teach you how to solve it, just change your Virtual Host * section on apache.conf or httpd.conf from this:
  DocumentRoot /home/www/web
ServerName www.example.com

to this:
  DocumentRoot /home/www/web
ServerName www.example.com
ServerAlias example.com

Did you notice that I just added this line: ServerAlias example.com that's everything you need to solve this stupid problem and spare your users of typing 3 useless letters to access your website.

I still don't understand why there are some many sites out there that aren't accessible without www, it's so stupid.
/RANT OFF

Tool: Delete Files Older than X days in Windows

Here is a little tool I wrote to delete files older than X days in windows, in linux we have several ways for doing that but I couldn't find any easy solutions on windows, so I opened up my Visual Studio and it took me about 5 minutes to write it.

As I think this tool can be useful, I'm sharing it, use it at your own risk.

I'm also sharing the source code, it is stupid but feel free to improve it, just remember to send me a copy of the improved version.

DelOldFiles.exe (requires .NET 2.0)
Source Code (Visual Studio 2008 project)

segunda-feira, outubro 22, 2007

Howto: Schedule SQlServer Express backup

I was assigned with the task of maintaining a VPS server that hosts our bugtracking system and internal management software.

As there was a new version of our bugtracking system (fogbugz) available I was asked to update it.

The update process is very simple, it has an installer and you just have to click, next, next and finish, but as I don't like doing anything without backing up first, and as our SQLServer doesn't have a daily backup set up (aside from the full system backup, which I particularly don't trust), so I decided to set up a daily backup.

The first problem was figure out how sqlserver backup works, I found this nice article: Automating Database maintenance in SQL 2005 Express Edition Part II.

It worked flawlessly, but there was a problem, each backup took more than 1.5GB, we don't have much disk space and in my opinion keeping just one daily backup is as useless as no backup at all. I had to get my hands dirty and write a batch script to compact the backup and delete the backup files older than 7 days.

Compacting the files wasn't a problem, as winrar comes with a command line utility (rar.exe) it was easy to set up a command line to compact the backup files, just using rar.exe /? is enough to find out how to compact a set of files in command line, which was basically:

rar.exe a file.rar c:\FilesToAdd\*.* -r


But the real problem was how to delete the backups older than 7 days, I found several solutions in google, but none worked as I expected, some used vbscripts, other used batch scripts (which proved to be very dangerous), but none solved my problem, so I decided to write my own application to delete files older than 7 days, and in less than 10 minutes my problem was solved.

The application to download files older than X days can be found here.

quarta-feira, outubro 17, 2007

Howto: Write unmaintainable code

This post is inspired by How To Write Unmaintainable Code Ensure a job for life ;-) by Roedy Green.

But rater I'd call it: How To Write Unmaintainable Code ensure headaches for life.

Well, I started programming when I was 15 years old, and since then I've learned a lot, and almost all I learned since then (except the very basics, which I learned at school) I learned by myself, I read a lot and I really enjoy programming, which helps, because as I code for fun I learn and have fun at same time.

And I lost almost all of my oldest codes. I wrote I game like space invaders using clipper once, and for what I can remember it's the code I regret most to have lost.

But these kind of code you write for fun, you don't have to maintain, even if it is a piece of shit, who cares? it's just dead code.

But the real problem starts when other people starting using your code, because you can't just trash it, and that's a big problem.

And 4 or 5 years ago, I dropped clipper and start with php, even php being a modern language it still have many problems, and one of these problems is that it is easy too learn, and it attracts a lot of newbies, like me, who you should have guessed got attracted by it's simplicity (please forgive me for using this word to describe php) and easy learning.

And what happens when a newbie join forces with other newbies to learn a language? Nothing good, I'm sure.

As clipper was a procedural language and I didn't have a clue what OOP was at that time, I started writing spaghetti code at speed of light, and that was not a good idea.

1 or 2 years after that I found myself a full time job as a php developer, or maybe you should call it, php code typer.

And man, I'm fucking proud of it, because even with spaghetti code I was able to write a web based software by myself which works flawlesslly and also was a passport to my new job.

But when you write unmaintainable code, you can be sure that it will be back to hunt you, and it started hunting me as soon as I quit that job, and till now (8 months later) I am the only one maintaining it, not that it is impossible to maintain, but it is so freaking complex, that every new php programmer (aka newbie) that looks at it says, holy shit!

Why do I think this code is unmaintainable? Well, I spent more that one hour reading my own code trying to understand what that fuck some piece of code did, and after understanding it I asked myself, why the fuck I wrote that?

And as the spaghetti code quantity grows, it becomes I snow ball, if you try to refactor it, you will spend a lot of time with the risk of breaking what is working, if you just keeps writing code crazily you will end up in a dead end some day, and if you try to write the new code in a better way, well it will help a little, but your software will be like when you make a collage with letters cut off from a newspaper, b-e-a-u-t-i-f-u-l.

Anyway, don't blame for skipping the topic, this is a blog damnit! So I got carried away. Sorry :)

The purpose of this post was to give a simple advice, before writing code, please take your time, hold your breath, and read something about coding quality, design patterns, or general programming books, and be sure to at least read the Pragmatic Programmer.

I'm sure it will open your eyes and make you write better code, because I knew (and Know) a lot of potencial programmers that get stuck in a company which doesn't care about code quality, and thinks that code quantity is equals code quality, and it's a shame that these companies sometimes make these good programmers rot into oblivion.

I left my last company and moved to another one, starting with a lower salary, the new company is in another city so I had to move and I spend money with rent and households stuff, but man, it was the best thing I did. I've learned a lot in this past 8 months, I can use my full potential here to learn new stuff and write better software, I don't have to be stuck 8 hours a day writing spaghetti code, because I can spend half hour researching and write a 10 line code in some minutes that will do the same work, and better.

If advice was good, we would sell it, not give, but here is a little piece of advice.

If you spend more than some minutes to understand some code (specially if it's your code), it's got to have something wrong, so think carefully about it, can it be better? Will you understand it if you read it again after 2 months? Can you really copy and paste code without any punishment? Will you be able to reuse the code?

Well, if you ask this kind of questions to yourself, I can asure you that you will not regret it in the future, I wonder what my code would be like if I had asked these questions to my self in the first place... I'm sure that I wouldn't spend 1 hour to understand it and also I wouldn't be here writing this stupid post.

Anyway, if you read everything till here, thank you, but if you just read this last line, go to hell, because this is the first sin of "spaghetti code writers", they don't like reading.

Conditional Formatting on Google Docs, farewell excel.

At last conditional formatting was added to Google Docs, if you don't know what I'm talking about, conditional formatting is used to change the formatting of a certain cell based on some user defined conditions, for example, you can make a cell's text automatically become red when the number in another cell is negative.

ScreenShot

This is one of excel's features I missed most in Google Docs, but now I think it's time to say farewell to excel, for good.

You can read more about this here.

sexta-feira, outubro 12, 2007

Writing a Text Files Line Counter in C#

A friend asked me today if I knew an application that could count all the lines in all .php files in a directory, and my answer was no, but I can make one in five minutes, and thats all it took.

Bellow is the code, it's so simple that I'm lazy to explain, enjoy:


DirectoryInfo dInfo = new DirectoryInfo(args[0]);
FileInfo[] Files = dInfo.GetFiles("*.php");
int i = 0;
foreach (FileInfo fInfo in Files)
{
string[] temp = File.ReadAllLines(fInfo.FullName);
i += temp.Length;
}
Console.WriteLine("Total Lines: " + i);
Console.ReadLine();


It's dirty but it works, if you have a better solution, please comment, I'll be pleased to learn a better way to do this.