Pages

Thursday, August 3, 2006

Attempt at pretty code

I don’t think I’ve got it shorter, it does look slightly different.
I was aiming for something clean, but there is a small problem about renaming the first version.
This also doesn’t leave an original someName. Everything would return someName_1 and so on.
It probably needs a check incase there is no . in the file name.
[php]
$VERSION_SEPARATOR = ‘_’;
$EXTENSION_SEPARATOR = ‘.’;

function getNextFileVersion( $fileName, $toDirectory )
{
$toDirector = dirname($toDirector) . DIRECTORY_SEPARATOR;

$versionPos = strrpos( $fileName, $VERSION_SEPARATOR);
$extensionPos = strrpos( $fileName, $EXTENSION_SEPARATOR, $versionPos);

if( $versionPos > 0 )
{
$version = substr($fileName, $versionPos + 1, $versionPos + $extensionPos - 1);
}
$iCantThinkofABetterNameForARandomHack = ‘’;
if( false == is_numeric( $version ) )
{
$version = 1;
$versionPos = $extensionPos;
$iCantThinkofABetterNameForARandomHack = $VERSION_SEPARATOR;
}
$name = substr($fileName, 0, $versionPos);
$ext = substr($fileName, extensionPos) . $iCantThinkofABetterNameForARandomHack;

while( file_exists( $toDirector . $name . $version . $ext )
{
$version++;
}
return $name . $version . $ext;
}

- Edit a little bit later
I was hoping to add a track back to Mark, but it didn’t work the first time….

-= Comments
1. Mark | August 3rd, 2006 at 10:53 am
hmm, so that’s what a real programmers code looks like..

I still prefer the explodes / implodes to break into arrays.. rather than using substrings - I guess it’s just personal preference.

I really like this part.. It makes the looping part of the code very small. It’s a little more readable too with good variable names as opposed to my obscure arrays $file_name_parts and $file_name_inner_parts

while( file_exists( $toDirector . $name . $version . $ext )
{
$version++;
}

Will your version handle a filename with other ‘_’s in it.. e.g. my_broken_filename_with_too_many_underscores.jpg?

2. pimaster | August 3rd, 2006 at 4:15 pm
I think so. Didn’t get around to testing it. If I wrote it in java I would have included JUnit tests.

Have they come out with PUnit framework yet?

I only used substrings because I thought it would have made it shorter.
I was thinking of using a regular expression, but my mind was wandering
implode vs substring could be personal. I was thinking that substring might be quicker. (But I might be splitting hairs)

Sunday, July 30, 2006

Broken Chair take 2

Some of you may remember my original complaint about a broken chair.
No problems, next door neighbor helped in fixing it.

Chair broke again. This time one of the legs decided it just wanted to give way. It happened when I was trying to turn my computer on, so of course I fell of it rather awkwardly.
So I went out and bought another one. It’s currently sitting a bit higher than the previous chair, but I’ll make it comfortable.
Also have to get my "groove" back into the chair

Wednesday, July 26, 2006

Word on Generics

Had another java programmer here at work point me at a page complaining about generics
Ken Arnold’s Blog

And because I was interested in the possible performance issues that might be associated with generics, I wanted a read.

But it turned out to be a discuss about the difficulties of using generics. I read the article and I didn’t understand what his problem was. It seemed like he was using generics wrongly.

I though generics where meant to be an aid to prevent typecasting from items. A way to gaurantee that when you put something in of a certain type, it will come out as that type without typecasting.

There was one line in there “Right now, there are some idioms that you cannot code without a type-safety warning, such as creating arrays of generic types”
Unless I’ve missed something, isn’t

List<Thing>[] myList = new ArrayList<Thing>[5] ;// *

All you need to do?

*Code may not compile. I don’t think I’ve set Eclipse for 1.5

-= Comments
1. Matthew Delves | July 25th, 2006 at 12:28 pm
Whilst I am not a big fan of idiot proof code, it does make for more stable programs if and when it is used properly. Surprisingly someone who would be assumed to know Java would not be able to grasp the concept of Generics and convey it to others.

Simplifying code

It has been a while since I have written a good simplify post. It has also been a while since I’ve posted several entries end to end
[java]
String fileName = file.getName();
if (fileName.endsWith(someValue)) {
return true;
} else {
return false;
}

[java]
String fileName = file.getName();
return fileName.endsWith(someValue));

Now I know it could be shortened to one line, but then things start looking a little long. If it was c# with properties, I might have gone with it.

-= Comments
1. Lucien | July 26th, 2006 at 11:07 am

The one liner for that isn’t so bad.

return file.getName().endsWith(value);

At least you didn’t start with:
[java]
String fileName = file.getName();
if (fileName.endsWith(someValue) == true) {
return true;
} else if (fileName.endsWith(someValue) == false) {
return false;
}



That’s the kind of newbie code I *love*

2. Mark | July 26th, 2006 at 12:24 pm
This one with css is a classic

border-top-color: #000000;
border-right-color: #000000;
border-bottom-color: #000000;
border-left-color: #000000;
border-top-style: solid;
border-right-style: solid;
border-bottom-style: solid;
border-left-style: solid;
border-top-width: 1px;
border-right-width: 1px;
border-bottom-width: 1px;
border-left-width: 1px;

The amount of times I see the above code is stunning..

border: 1px solid #000;

3. Matthew Delves | July 26th, 2006 at 12:27 pm
There is something always beautiful about simple code. It is easy to follow and as long as you know exactly what it does there can be less bugs.

4. Lucien | July 26th, 2006 at 12:43 pm
With the CSS example, I expect it’s auto generated by some tool (not the tool sitting in front of teh keyboard).

doesn’t make it any more acceptable. And since humans inevitably have to go in and tweak it when it’s not working, it just makes out job harder.

5. Clinton | July 26th, 2006 at 1:17 pm
I’ve seen a few tools "expand" css rules because they internally expand them to implement each feature (aka firefox+web developer -> css view etc).

A neat css tidy tool at http://csstidy.sourceforge.net/i but the informed human is still the elegant solution…

6. Lucien | July 26th, 2006 at 2:17 pm
What about an elegant human designing an informed solution?

7. Mark | July 27th, 2006 at 10:54 am
No, an informed designer building an elegant solution.

Friday, July 21, 2006

Windows XP Pro Software Raid 1

Somehow at work, I managed to let myself assist in the management of the hardware at Hyro. Hooray for me.
The last guy was a windows only man. When a computer died the other day, and chckdsk /f didn’t work he was out of options. I come in with a linux cd and hey presto, some data is readable (It seems that it is a nasty disk).

He is an artist, and I know his plight. He needs the speed of a local disk, but probably should back it up somehow. Things change that often that keeping tabs on backups is a pain.

So I thought, haha, give the man two disks and see if Windows will do a software raid. Both disks are probably crap, but at least they shouldn’t fail at the same time.

Spent most of this morning googling. Came across this interesting forum:
short-media forum

It was a kind of yes / no thing. The only way I was going to get an answer was to actually do it.
Fired up VMWare, worked out how to add a second disk, shrunk the first partition with some linux tools and loaded up disk management. I was able to create a striped volume, not much else.

Maybe it was because I was running without any service pack. Fired that on and tried again, still no luck.

I remember reading that it may not be possible with a disk that holds the operating system: a limitation of the software perhaps, so I bung in a third disk and give it another go, carefully reading all of the screens again.

[hmmm, seems I no longer have a copy of the image that was here]

Here is the magical screen where 5 = 3.
I don’t mind sticking up for Microsoft sometimes, but it’s this sort of thing that makes it hard.

Just about to try the hack over here, but I think sleep might get the better of me.

Funny how sometimes you can go months without posting, and then two big things come across your lap.

-= Comments
1. Clinton | July 21st, 2006 at 2:44 pm

When i realised i couldn’t (easily) do software RAID (2-disk mirror) on a non-server addition of XP, I purchase an Apatec SATA RAID (1210SA) card ~$80 AUD and it’s been really good. Yeah - i know it feels crazy to buy hardware when the software should be able to just *do* this (stupid policy for this feature I feel)… but using a hardware solutions has actually been a good result.

It was also pretty easy to plug-in the existing hdd (with XP pro installed) into the card etc. and not lose anything (i had backed up everything as that was a real concern Second disk got mirrored up and apart from some extra heat from two drives me checking on the disk health (SMART hidden from the direct interface now) I’m happy.

Thursday, July 20, 2006

Unfriggin believable - Source Game

http://storefront.steampowered.com/v2/index.php?area=game&AppId=922

Check out the new portal game that is to come with Half Life 2: Episode 2
Drool.

Now that is game play that might make me go out and get a credit card so I can have this game as soon as it is released.

I waited before DOD:S came out in a box, I’m waiting for Episode 1 to be boxed, but this just looks to good to pass up.

Sunday, June 4, 2006

Syn

Ack

Just a quick post to say that I’m still alive, just overly busy.
I’m still working long hours at work to get things done.

My birthday just went past (30th May) so I’m now a young 22 (Everyone that wishes me a happy birthday is older than me and wishes they were 22 again).
I got my Graduation Certificate back with a beautiful frame and Red Dwarf 8 which had me in stiches last Sunday.

I went to a karaoke deal Thursday night to celebrate and Friends 22nd birthday, and stunned everyone with a solo to the Time Warp. I know have to work out whether I want to go again.

Looked at Clintons new blog role page. Still a little dissapointed that everything seems to be on the one page. Other than that it is looking good.

Going out this evening to see the Gondoliers. Should be interesting.

Just a note on my programming skills. I haven’t done any hardcore programming for so long. I did a small development change and completely feked it up.

[java]
if(something.length > 25)
something = something.substring(0,25);
if(null == something)
something = “”;


The thought was there to build a nice solution, but it would appear as though my brain was on holiday.

-= Comments
1. Andrew B Coathup | June 5th, 2006 at 7:07 pm
Nothing like checking for a condition after an exception would be raised. :p

2. Clinton | June 5th, 2006 at 11:15 pm
Happy birthday! (well wishes *thrown* after the event… :~)

…as for my test blogroll… sorry!, (bit it is “work in progress”! - aka dump everything, badly, and start culling back to what we *really* want… feedback welcome.)

i’ll see if i can do some improvements tonight (after some AI assignment marking and after digesting all the blogroll comments - which just look like a brewing browser war… ;~)