Don't hardcode. Sure there are cases where hardcoding is acceptable/ideal but for the most part (i.e. the majority of web and client applications), there's not a whole lot of need for hardcoding and in fact, hardcoding can get you into a world of pain down the road. Sure, you might think you just finished coding whatever it was you were doing and it's perfect -- until someone comes along and says "hey, can you change to ? thanks!" -- that's when you realize you were dumb and you now have to do do a search/replace-all. Naturally it gets more complicated/tedious if it's not longer a case of search/replace-all. So yeah, hardcoding is generally, non-ideal (besides, it's far more elegant to have a universal algorithm that handles a wide range of scenarios than to be stuck in the box of only one scenario).
Of course there is balance -- we can't go around defining constants for everything (or can we?). That balance is determined by the complexity of the application as well as the expected amount of maintainance to be done down the road. Generally speaking, the complex and/or maintainance-intensive the application, the more likely one will want to make use of abstraction and patterning. After all, what's easier to understand:
or
At a quick glance, they look about the same and without experience, the latter might look more straightforward but in fact, it's the former that is far more easily maintained and built up upon -- not to mention I've simplified the second scenario by not defining exactly what those conditions are. The step that is left out of the first one is the actual translation of the conditions to which scenario it falls under (i.e., such and such results in scenario1, such and such results in scenario2 etc -- this is what the second one only does). The basic principle here is to make your code readable and maintainable down the road when you look at it and you have no idea what you were thinking about when you wrote it the first time. This very basic principle is fairly well observed in application development -- yet when you take the step to web development, it is almost virtually abandoned.
Specifically I'm ranting about ASP.NET development but I imagine a similar rant applies to all the other web application development languages. As I understand it (and I am, by no means, a pro at this), there are three separate areas where we can code.
- The client-side scripting. Generally Javascript, this is the cohesive goo that gives a website it's 'interactive feel'. Coincidentally, it's also Javascript that gave us the retarded concept that is the popup ad. I have my bias: I hate javascript. Hate it, hate it, hate it (mostly due to popups and the zillions of really stupid scripts out there that totally bog down the Internet -- which is painfully slow enough as it is). Javascript is a huge rant of rage and anger in and of itself. But that's another day.
- The back-server side (aka codebehind). This is where you will find your VB.NET/C#.etc code. This is, for all intents and purposes, real code. You define your methods, enumerations, constants, classes what-have-you. This is where work gets done.
- The front-server side (aka forms). This is where you find the markup for the site. Anyone who has passing familiarity with HTML will feel more at home here. And this is where I have a giant cannon of rage pointed at.
Before moving on, it is definitely worth noting that the back-server side elements can be placed on the front-server side file. In fact, you can essentially copy-paste the codebehind into the front-server file! You do need to encase it with the appropriate tags mind you. Now, excluding obvious scenarios like HelloWorld and your very first ASP.NET homework assignment or something that is exceptionally primative, this is an absolutely retarded notion.
Because noooooo, we couldn't possible have a reason to put VBNET/C# code in a .VB/.CS file like every other VBNET/C# developer on the planet, we need to mash it into .ASPX file. Sure I expect this to happen for basic scenarios but I was particularly ticked when I came across an endless supply of examples of this in a professionally (and I mean that with the greatest respect) example pack ever. Specifically, I'm talking about Obout. Their control pack's example set is phenomenal. It's exceptionally well put together. Except for the insistance of putting back-server code in the front-server file.
Sure. They have a passibly legitimate reason -- when you have a gazillion sample files, you kind of want to keep it at that -- no point doubling the file count for a few lines here and there. Sure. I'll accept that for now: so we'll add "if you have a gazillion files that are all relatively simple, we'll excuse you for not separating the front from the back" to the list of exceptions. So why is it when I search for something to do with ASP.NET and download something to look at it, there is extensive use of this mashing concept? And it's not just "examples" I'm talking about, I'm referring to full out, production oriented thing too!
So what's horribly wrong with the front-end markup? Three things
- It's friggen HIDEOUS. Have you seen markup? It's a damn mishmash of { } < > % $ and @. Yeah. way to be readable. Oh wait, it's not intended to be readable -- which is why it's markup to begin with! So why the hell would you want to look at it? Let's also not forget that people forget (or don't care to) do spacing and indenting -- you know, to improve readability and all, so you do end up looking at a bunch of random crap.
- It's unecessarily complicated. Let's say we have a paragraph of text, say 1000 letters long. Our boss has decided on the following color scheme
- Even numbered under 500 characters will be #ff4689
- Odd characters under 500 will be #5836FE
- Prime numbered characters will be #1234EE
- Even numbered over 499 characters will be #FEED86
- Odd numbered over 499 will be #DEADED
- The background color of the text for any given letter will be the complementary color.
- Now let's say our boss decided to change colors :P Or add new rules. Enjoy.
As for front-side coders -- programming in a static world must be easy. After all you don't ever have to worry about non-hardcoded scenarios.
No comments:
Post a Comment