Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Please 'hack' nicely...

by vladb (Vicar)
on May 23, 2002 at 13:07 UTC ( [id://168755]=perlmeditation: print w/replies, xml ) Need Help??

When I first started writing simple Perl programs, I should admit I didn’t take Perl all too seriously. In the back of my mind I used to associate Perl heavily with the word ‘hack’. Here’s one of the ways in which this word is being defined at http://www.dictionary.com:

1. To cut irregularly, without skill or definite purpose; to notch; to mangle by repeated strokes of a cutting instrument; as, to hack a post.

Having originally come from C++/Java roots, to uninitiated self programming Perl in fact meant “hacking”. I had friends (err.. still do) who used to apply the word “hack” in a similar context too. Hack! There are a number of websites dedicated to nerds/programmers such as SlashDot.org and codingstyle.com where the word is a common occurrence.

I recall back the days when writing a true “hack” made me somewhat proud of myself. I would feature the word prominently in my code just before where the true ‘masterpiece’ (aka ‘hack’) was placed. For example, take a look at this snippet:
# HACK # see if current time field value exceeds its maximum (reminder of % o +peration is greater than 0); # if true, add reminder to ($time_date[$i+1], $time_date[$i]) = ($time_lim != -1 && ($rem = $time +_date[$i]%$time_lim)) ? (($time_date[$i] - $rem) / $time_lim, $rem) : + ($time_date[$i+1],$time_date[$i]); # equivalent (not HACK)... # if ($rem = $time_date[$i] % $time_lim) { # $time_date[$i+1] += ($time_date[$i] - $rem) / $time_lim; # $time_date[$i] = $rem; # }
Compare the ‘HACK’ portion of the code with an equivalent and render your own judgment. Well, revisiting my old code, sticking with the community of fellow monasterians, and reading various articles/books on proper software coding practices made me rethink a lot of old notions and ideals I once cherished and now came to despise. Having had to maintain my old code and deal with seemingly childish ‘hacks’, I became increasingly frustrated by any mentioning of the word ‘hack’ in the context of programming (especially Perl). In my conversations with fellow developers, I try to use the word with extreme caution and very very seldom. I’m trying to be careful not to excite any other developer (especially junior type) into writing a lot of ill hacks that I might eventually have to deal with later in my work.

Have you had similar bad experiences in your career as a Perl programmer? Did you ever have to deal with stubborn ‘hackers’ or haunted by the shadows of certain ‘hacks’ you had implemented in the past?

UPDATE: Or is my interpretation of 'hacking' invalid? ;)

_____________________
$"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/; $_=<a HREF="/index.pl?node=%22ps%20-e%20-o%20pid%20"> "," $2 </a>;`@$_ +`?{print"+ $1"}:{print"- $1"}&&`rm $1`; print$\;}

Replies are listed 'Best First'.
Re: Please 'hack' nicely...
by Biker (Priest) on May 23, 2002 at 13:36 UTC

    When someone is paying me for a piece of software, then I make sure it's not a 'hack', but a 'clean' solution. (Of course, the definitions are not 100% objective to what is what there, it's my conscious guiding me.)

    When I write something for myself on my free time (and I'm not working at the same company as poor Tilly for those who remember the story) then I reserve myself the right to code as I want to. If I feel for doing it sloppy, then I do. If I'm having a good day, I may do it more properly.
    It's my stuff. I do what I want with it. If someone uses it, because I gave it to them, then they use it 'as is'. They'd better live with it. Or re-write it.


    Everything went worng, just as foreseen.

Re: Please 'hack' nicely...
by Molt (Chaplain) on May 23, 2002 at 13:24 UTC

    I hack, and I program. I hack when I want it done now, the one-off script, when I'm not quite sure what I'm doing but not letting that hinder me, and when seeing what can be done. I program when I want stuff I'm going to reuse, stuff others may need to maintain, and when I have that tremendous inner calm that makes me program when I've set out to hack.

    Some of my programs have ended up with hacks in them, this is regretable and I've clearly marked them with comments like 'This is the largest kludge I've seen since the last one'. I then go back and fix them, some may call it refactoring but when it's so ugly I don't think it deserves such pretentious description.

    Equally, some of my best hacks have become programs. Quite often when I'm beginning a program there'll be two or three areas which I'm not experienced in which are needed, and so I'll start off writing a small script to try out each area. Sometimes I'll then think 'But how will this X script interface with feature Y?' and build that on there too, and before I know it the script has organically grown to become a full program.

    I think my last three large projects here all germinated from a hack. The first being a hack to test something regarding it's data structures, the second a hack concerning connection Perl to Java via the command line in a nice way, and the third controlling the number of processes running doing a certain thing- irregardless of which user was running them.

    I consider this to be an okay thing, assuming I don't mind going back and tidying up any bits that even marginally resemble hacks. Sweep ugly hacks under the carpet of good code. When I read about the 'write a bit, test a bit, rinse and repeat' of XP I ended up realising that was pretty much what I did.

    Oh, I also prefer the definition of hack from the Jargon file. It's a nicer definition, and may eventually make you happy to say 'Yes, I'm a hacker'. Then again, people who go by the 'Beautiful hack' kind of definition may want to meet me in a dark alley whilst they're carrying a selection of sporting equipment beginning with a reasonably sturdy baseball bat for what I just said.

      when I have that tremendous inner calm that makes me program when I've set out to hack.

      Uh, OK...

      While I may lack any desire to set upon you with my collection of medieval weaponry, I must be one of the 'beautiful hack' people you're talking about. To my mind, "tremendous inner calm" is a good place for a hack to begin and almost certainly where it will leave you afterwards. (Well, either tremendous inner calm or "Oh my god... How did I ever manage to come up with something that elegant?" (Note that, in my world, clarity is a prerequisite for elegance. Unless you're working on an obfu.))

      But am I happy to say, "Yes, I'm a hacker"? No. I'm not nearly that good. (Yet?)

Re: Please 'hack' nicely...
by webfiend (Vicar) on May 23, 2002 at 13:43 UTC

    I'll agree with your definition of "hack", with a side comment. Perl is such a rich language that I am learning new features every time I start a project. Because of that, yesterday's "wonderfully clean code" becomes today's "childish hack".

    For example: I've recently gotten serious about using map and grep, and suddenly all those awkward foreach loops I've written look like Fred Astaire in Frankenstein boots. Sure, they work, but it's a long way from the most graceful solution!


    "All you need is ignorance and confidence; then success is sure."-- Mark Twain

        That said, guess who wrote:

        The argument against using an operator for other than its primary purpose strikes me the same as the old argument that you shouldn't have sex for other than procreational purposes. Sometimes side effects are more enjoyable than the originally intended effect.

        See answer here

Re: Please 'hack' nicely...
by Aristotle (Chancellor) on May 24, 2002 at 05:10 UTC

    "A hack" in my book is a solution that works but is ugly, should probably be done better, but for one of various reasons (no time, no motivation, alternatives are awfully verbose and/or resource intensive) isn't. I tend to view these as sometimes necessary and always evils, that should be rooted out unless the hack in question is the lesser of evils.

    I draw a very clear line between hacks and JAPHish/golfish trickery though. I have no fear of using the latter when it really does shorten the code by a noticable amount. Since I am generous with comments, I feel entitled to such tomfoolery.

    Interestingly enough, I always have trouble bending my mind anew around a long forgotten hack that I have to take a second look at, but never any when trying to grok my long burried Golfish constructs.

    ____________
    Makeshifts last the longest.
Re: Please 'hack' nicely...
by mpeppler (Vicar) on May 24, 2002 at 00:58 UTC
    vladb++

    I find myself thinking the same thing very often - in particular in a multi-programmer project, where the level of understanding/experience can vary quite a bit.

    Michael

Re: Please 'hack' nicely...
by hakkr (Chaplain) on May 24, 2002 at 11:28 UTC

    Why does hack mean something completely different to non techies?

    Surely this causes a lot of confusion and even suspsion when the rest of the world thinks 'to hack' is to break in a system with malicious intent.

    I thus never use the phrase 'I've been hacking some code' to avoid giving the impression I am some sort of virtual cyber criminal.

    Seeing as the rest of the world is unlikely to start calling 'hackers' 'crackers' maybe techies should change their terminolgy for munging quick fix solutions. <\p> Hack is a negative word, it means to commit a dirty foul in football(soccer) or can be used in referance to machetes for instance. You will find no hacks in good code and if you do they won't stay there for long.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://168755]
Approved by bmcatt
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-19 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found