Software : 10 mistakes every programmer makes |
10 mistakes every programmer makes Posted: 28 Nov 2010 02:00 AM PST When you start programming, you get disillusioned quickly. No longer is the computer the allinfallible perfect machine – "do as I mean, not as I say" becomes a frequent cry. At night, when the blasted hobgoblins finally go to bed, you lie there and ruminate on the errors you made that day, and they're worse than any horror movie. So when the editor of PC Plus asked me to write this article, I reacted with both fear and knowing obedience. I was confident that I could dash this off in a couple of hours and nip down to the pub without the usual resultant night terrors. The problem with such a request is, well, which language are we talking about? I can't just trot out the top 10 mistakes you could make in C#, Delphi, JavaScript or whatever – somehow my top ten list has to encompass every language. Suddenly, the task seemed more difficult. The hobgoblins started cackling in my head. Nevertheless, here goes… 1. Writing for the compiler, not for people When they use a compiler to create their applications, people tend to forget that the verbose grammar and syntax required to make programming easier is tossed aside in the process of converting prose to machine code. A compiler doesn't care if you use a single-letter identifier or a more human-readable one. The compiler doesn't care if you write optimised expressions or whether you envelop sub-expressions with parentheses. It takes your human-readable code, parses it into abstract syntax trees and converts those trees into machine code, or some kind of intermediate language. Your names are by then history. So why not use more readable or semantically significant identifiers than just i, j or x? These days, the extra time you would spend waiting for the compiler to complete translating longer identifiers is minuscule. However, the much-reduced time it takes you or another programmer to read your source code when the code is expressly written to be self-explanatory, to be more easily understandable, is quite remarkable. Another similar point: you may have memorised the operator precedence to such a level that you can omit needless parentheses in your expressions, but consider the next programmer to look at your code. Does he? Will he know the precedence of operators in some other language better than this one and thereby misread your code and make invalid assumptions about how it works? Personally, I assume that everyone knows that multiplication (or division) is done before addition and subtraction, but that's about it. Anything else in an expression and I throw in parentheses to make sure that I'm writing what I intend to write, and that other people will read what I intended to say. The compiler just doesn't care. Studies have shown that the proportion of some code's lifecycle spent being maintained is easily five times more than was spent initially writing it. It makes sense to write your code for someone else to read and understand. 2. Writing big routines Back when I was starting out, there was a rule of thumb where I worked that routines should never be longer than one printed page of fan-fold paper – and that included the comment box at the top that was fashionable back then. Since then, and especially in the past few years, methods tend to be much smaller – merely a few lines of code. In essence, just enough code that you can grasp its significance and understand it in a short time. Long methods are frowned upon and tend to be broken up. The reason is extremely simple: long methods are hard to understand and therefore hard to maintain. They're also hard to test properly. If you consider that testing is a function of the number of possible paths through a method, the longer the method, the more tests you'll have to write and the more involved those tests will have to be. There's actually a pretty good measurement you can make of your code that indicates how complex it is, and therefore how probable it is to have bugs – the cyclomatic complexity. Developed by Thomas J. McCabe Sr in 1976, cyclomatic complexity has a big equation linked to it if you're going to run through it properly, but there's an easy, basic method you can use on the fly. Just count the number of 'if' statements and loops in your code. Add 1 and this is the CC value of the method. It's a rough count of the number of execution paths through the code. If your method has a value greater than 10, I'd recommend you rewrite it. 3. Premature optimisation This one's simple. When we write code, sometimes we have a niggling devil on our shoulder pointing out that this clever code would be a bit faster than the code you just wrote. Ignore the fact that the clever code is harder to read or harder to comprehend; you're shaving off milliseconds from this loop. This is known as premature optimisation. The famous computer scientist Donald Knuth said, "We should forget about small efficiencies, say about 97 per cent of the time: premature optimisation is the root of all evil." In other words: write your code clearly and cleanly, then profile to find out where the real bottlenecks are and optimise them. Don't try to guess beforehand. 4. Using global variables Back when I started, lots of languages had no concept of local variables at all and so I was forced to use global variables. Subroutines were available and encouraged but you couldn't declare a variable just for use within that routine – you had to use one that was visible from all your code. Still, they're so enticing, you almost feel as if you're being green and environmentally conscious by using them. You only declare them once, and use them all over the place, so it seems you're saving all that precious memory. But it's that "using all over the place" that trips you up. The great thing about global variables is that they're visible everywhere. This is also the worst thing about global variables: you have no way of controlling who changes it or when the variable is accessed. Assume a global has a particular value before a call to a routine and it may be different after you get control back and you don't notice. Of course, once people had worked out that globals were bad, something came along with a different name that was really a global variable in a different guise. This was the singleton, an object that's supposed to represent something of which there can only be one in a given program. A classic example, perhaps, is an object that contains information about your program's window, its position on the screen, its size, its caption and the like. The main problem with the singleton object is testability. Because they are global objects, they're created when first used, and destroyed only when the program itself terminates. This persistence makes them extremely difficult to test. Later tests will be written implicitly assuming that previous tests have been run, which set up the internal state of the singleton. Another problem is that a singleton is a complex global object, a reference to which is passed around your program's code. Your code is now dependent on some other class. Worse than that, it's coupled to that singleton. In testing, you would have to use that singleton. Your tests would then become dependent on its state, much as the problem you had in testing the singleton in the first place. So, don't use globals and avoid singletons. 5. Not making estimates You're just about to write an application. You're so excited about it that you just go ahead and start designing and writing it. You release and suddenly you're beset with performance issues, or out-of-memory problems. Further investigations show that, although your design works well with small number of users, or records, or items, it does not scale – think of the early days of Twitter for a good example. Or it works great on your super-duper developer 3GHz PC with 8GB of RAM and an SSD, but on a run-of-the-mill PC, it's slower than a Greenland glacier in January. Part of your design process should have been some estimates, some back-back-of- the-envelope calculations. How many simultaneous users are you going to cater for? How many records? What response time are you targeting? Try to provide estimates to these types of questions and you'll be able to make further decisions about techniques you can build into your application, such as different algorithms or caching. Don't run pell-mell into development – take some time to estimate your goals. 6. Off by one This mistake is made by everyone, regularly, all the time. It's writing a loop with an index in such a way that the index incremented once too often or once too little. Consequently, the loop is traversed an incorrect number of times. If the code in the loop is visiting elements of an array one by one, a non-existent element of the array may be accessed – or, worse, written to – or an element may be missed altogether. One reason why you might get an off-by one error is forgetting whether indexes for array elements are zero-based or one-based. Some languages even have cases where some object is zero-based and others where the assumption is one-based. There are so many variants of this kind of error that modern languages or their runtimes have features such as 'foreach loops' to avoid the need to count through elements of an array or list. Others use functional programming techniques called map, reduce and filter to avoid the need to iterate over collections. Use modern 'functional' loops rather than iterative loops. 7. Suppressing exceptions Modern languages use an exception system as an error-reporting technique, rather than the old traditional passing and checking of error numbers. The language incorporates new keywords to dispatch and trap exceptions, using names such as throw, try, finally and catch. The remarkable thing about exceptions is their ability to unwind the stack, automatically returning from nested routines until the exception is trapped and dealt with. No longer do you have to check for error conditions, making your code into a morass of error tests. All in all, exceptions make for more robust software, providing that they're used properly. Catch is the interesting one: it allows you to trap an exception that was thrown and perform some kind of action based upon the type of the exception. The biggest mistakes programmers make with exceptions are twofold. The first is that the programmer is not specific enough in the type of exception they catch. Catching too general an exception type means that they may be inadvertently dealing with particular exceptions that would be best left to other code, higher up the call chain. Those exceptions would, in effect, be suppressed and possibly lost. The second mistake is more pernicious: the programmer doesn't want any exceptions leaving their code and so catches them all and ignores them. This is known as the empty catch block. They may think, for example, that only certain types of exceptions might be thrown in his code; ones that they could justifiably ignore. In reality, other deadly runtime exceptions could happen – things such as out-of-memory exceptions, invalid code exceptions and the like, for which the program shouldn't continue running at all. Tune your exception catch blocks to be as specific as possible. 8. Storing secrets in plain text A long time ago, I worked in a bank. We purchased a new computer system for the back office to manage some kind of workflow dealing with bond settlements. Part of my job was to check this system to see whether it worked as described and whether it was foolproof. After all, it dealt with millions of pounds daily and then, as now, a company is more likely to be defrauded by an employee than an outsider. After 15 minutes with a rudimentary hex editor, I'd found the administrator's password stored in plain text. Data security is one of those topics that deserves more coverage than I can justifiably provide here, but you should never, ever store passwords in plain text. The standard for passwords is to store the salted hash of the original password, and then do the same salting and hashing of an entered password to see if they match. Here's a handy hint: if a website promises to email you your original password should you forget it, walk away from the site. This is a huge security issue. One day that site will be hacked. You'll read about how many logins were compromised, and you'll swallow hard and feel the panic rising. Don't be one of the people whose information has been compromised and, equally, don't store passwords or other 'secrets' in plain text in your apps. 9. Not validating user input In the good old days, our programs were run by individuals, one at a time. We grew complacent about user input: after all, if the program crashed, only one person would be inconvenienced – the one user of the program at that time. Our input validation was limited to number validation, or date checking, or other kinds of verification of input. Text input tended not to be validated particularly. Then came the web. Suddenly your program is being used all over the world and you've lost that connection with the user. Malicious users could be entering data into your program with the express intent of trying to take over your application or your servers. A whole crop of devious new attacks were devised that took advantage of the lack of checking of user input. The most famous one is SQL injection, although unsanitised user input could precipitate an XSS attack (crosssite scripting) through markup injection. Both types rely on the user providing, as part of normal form input, some text that contains either SQL or HTML fragments. If the application does not validate the user input, it may just use it as is and either cause some hacked SQl to execute, or some hacked HTML/JavaScript to be produced. This in turn could crash the app or allow it to be taken over by the hacker. So, always assume the user is a hacker trying to crash or take over your application and validate or sanitise user input. 10. Not being up to date All of the previous mistakes have been covered in depth online and in various books. I haven't discovered anything new – they and others have been known for years. These days you have to work pretty hard to avoid coming into contact with various modern design and programming techniques. I'd say that not spending enough time becoming knowledgeable about programming – and maintaining that expertise – is in fact the biggest mistake that programmers make. They should be learning about techniques such as TDD or BDD, about what SLAP or SOLID means, about various agile techniques. These skills are of equal or greater importance than understanding how a loop is written in your language of choice. So don't be like them: read McConnell and Beck and Martin and Jeffries and the Gang of Four and Fowler and Hunt & Thomas and so on. Make sure you stay up to date with the art and practice of programming. And that concludes my top 10 list of mistakes programmers make, no matter what their language stripe. There are others, to be sure, perhaps more disastrous than mine, but I would say that their degree of dread is proportional to the consequences of making them. All of the above were pretty dire for me the last time I made them. If you have further suggestions or calamities of your own, don't hesitate to contact me and let me know. |
15 best places to download free PC wallpaper Posted: 28 Nov 2010 12:00 AM PST The average adult spends around eight hours a day staring at a screen, but if you take a second to think about what's on display, all too often it's framed by a bland background. Windows and Linux make it easy to display something more aesthetically pleasing. Indeed, Windows 7 comes with a wider selection of gorgeous images than ever before. However, this means the art of original desktop decoration is being lost. Customising your desktop with a picture of the family cat is all well and good, but few of our own digital photos are actually suitable for the desktop – they're either too busy and distracting to have icons splattered on top, or not high enough resolution, so the result is grainy. And forget dedicated websites too – many wallpaper sites simply offer tasteless images of sunsets and models, and some are even designed to spread malware. That's why we've found 15 of the best places to get wallpapers on the web, whatever your tastes may be. Some are a little left-field and some are plain unconventional, but we're confident that whatever your taste in art, you'll find something that will make working on your PC much more enjoyable. 1. Wikimedia Commons Wikimedia is a free online community made up of millions of photos, movies and sounds that are free to use. There's a myriad of images, some of which aren't suitable for life on the desktop, but some are beautiful shots that are crying out to be displayed. One of the best ways of hunting down great desktop images on the site is to use the Photo of the Day feature, and going back through the site's long history, where there are some great images on offer. When you find one you like, download the full-resolution version rather than the preview, as these will be much higher quality and will look better on your desktop. 2. Hamad Darwish Windows Vista may have been disappointed in nearly every respect, but one aspect that did impress us was the great-looking desktop wallpapers that came included. Those masterpieces were the work of Hamad Darwish, who travelled the world for Microsoft, taking wonderful snaps for our enjoyment. Upon his return, he put all of the wallpapers that Microsoft rejected on the internet for free, and there are some fantastic vistas that will really brighten your desktop. Just head onto his website, click the Windows Vista tab and download the 30MB folder. While you're there, check out Hamad's other work, which is really rather good. 3. Dual-Monitor Backgrounds If you have a dual-monitor setup then Dual Monitor Backgrounds is an essential wallpaper resource. All the wallpapers on the site are 2,560x1,024 or 3,840x1,200 and look truly stunning when spread out. One of the best features on the Dual Monitor Background site is the ability to set your monitor resolution by clicking the link at the top. All the results will then be filtered to your specific needs, so you'll only see the wallpapers that suit your desktop. There are loads of categories to choose from, but just remember to click on the preview shot first to get the full-resolution image before you save it to your desktop. 4. Space The Windows desktop: the final frontier. Well, not quite. But those with a passion for space, stargazing and sci-fi are well served for quality wallpapers. Head onto the Space Views part of www.space.com and click the Wallpapers tab to get an array of out-of-this-world shots. They come in a range of sizes specifically designed for life on your desktop. Admittedly, many are mock-ups by artists whose impression of space, it seems, has been altered by hallucinogens. However, we love the Earth shots – some of them are truly inspiring. 5. National Geographic When it comes to incredible images, you can rely on National Geographic. It has an array of the world's most gorgeous vistas on offer, and what's more, they're specifically designed for the desktop, and free to download. From the National Geographic homepage, just go to 'Photography | Wallpapers' and start browsing the best that nature has to offer. When you click on an image in the Wallpapers section, you'll see a 'Get wallpaper' link. The image will resize and you can right-click to apply, or save the image using the link at the top. 6. Gigapixel If your digital camera is measured in megapixels, just imagine what a gigapixel camera can do! Miracles, that's what. The images are stunning, and they can be enlarged to such an extent that you can make out the smallest details in huge vistas, as if looking through a telescope. The Gigapxl Project has a huge collection of shots that show off the impact of gigapixel images, and they offer wallpaper-sized versions of most of their photos. 7. NASA NASA may be hard at work discovering the secrets of the universe, but it's nice to see that between deep space explorations, they've found the time to put a couple of wallpapers up as well. There are close-up images of the darkest and weirdest corners of the galaxy, taken by space telescopes and satellites. Interplanetary wonders on offer include the Orion Nebula, Lulin Comet and the Martian surface, to name but a few. If there's a set of images guaranteed to give you a self-worth complex, it's NASA's, and we salute them for it. To find the images, go to 'View Image Gallery' and scroll through the sets. The download links are in the right-hand panel, with a variety of sizes to choose from. 8. Porsche There are few sights as pleasing as one of the most expensive cars on the planet, and for fans of Porsche, these wallpapers are an absolute must. Porsche has gone to the trouble of taking scores of pictures of its cars bathed in sunlight, dripping with decals and racing mods, and they're guaranteed to get fans of the supercar excited. The Boxster, 911, Panamera and every middle-class parent's favourite, the Cayenne, are all available, and there are multiple sizes to suit every desktop. 9. Bing Bing may be struggling to gain ground in the battle of the search providers, but it beats Google hands down when it comes to amazing pictures. Photos are so important to Bing that every country has someone in charge of selecting the images, so it would be a shame to let that go to waste without making some fantastic desktops. 10. Microsoft Official Themes One of the best parts of Windows 7 is its desktop themes. These enable you to select sets of photos and colour schemes with which to decorate Windows and change them on demand. While previous editions of Windows were fairly static in terms of appearance, Microsoft has been updating wallpapers and themes for Windows 7 via its Personalisation Gallery. These range from country-specific scenery to the best of Bing. To access the gallery, right-click the desktop and go to 'Personalize | Get more themes online'. All of the wallpapers and themes plug straight into Windows 7, so there's minimum fuss and maximum enjoyment. 11. Enigma desktop One of the most dramatic ways of changing your desktop's appearance is the Enigma Desktop, which is easier than ever to set up. The Enigma mod is now fully featured in Rainmeter, and turns your desktop into a blank canvas for everything from storing icons to implementing a fully integrated panel that can alert you to new emails, RSS feeds and much more. Just head over to the website to download the program, which now comes with a fantastic set of wallpapers by Kaelri from Deviant Art. Enigma is a great mod for anyone looking for a unique and functional desktop, and the wallpapers make it well worth the time investment alone. 12. Deviant Art For fans of independent digital art, Deviant Art is a great place to look. It's a huge melting pot of creativity, with a mixture of graphically altered images and amazing digital drawings. While Deviant Art is by no means designed to cater specifically for wallpaper hunters, there are some stunning images on offer that make fabulous backgrounds. You can download the original versions of the images for use on your desktop, although there isn't universal support for every screen resolution. 13. Windows 7 Themes Windows 7 Themes is so popular, it has a higher Google rank than the official Windows Personalization Gallery that's linked to from every copy of Windows 7. Don't be misled: there's nothing official about this site, which even lists Windows 8 wallpapers among its wares! That's to take nothing away from its vast selection of wallpapers, though. If you like the swirls and colours of the official Windows backgrounds, then you'll love the selection of tribute images on the site. 14. Flickr Wallpaper Group Flickr is fast becoming the best online source of images in the world, so it's no surprise that there's a section dedicated to wallpapers. The pool is contributed to by members of the Flickr community, many of whom are professional and semi-pro photographers, and the quality of the photos reflects that. Many are arty, most are striking, and they're all designed with the desktop in mind, with every image clocking in at over 1,024x768 pixels. To download, simply click on an image and then click the 'All sizes' button located at the top left. The next screen will offer a list of all the available resolutions, so pick the one that suits your display, or just click 'Download original size'. 15. Wallpaper Stock Wallpaper Stock is an apt name for a site that offers such a huge range of desktop images. They're all free to download, and while they might be slightly generic and unimaginative in places, there's a massive range of resolutions available, and a genre of images to suit every taste. In a bizarre twist, you need to tweet about the site in order to download an image. Once you've spammed your followers with a tweet about how great Wallpaper Stock can be, you're free to download. The full-sized image is opened in a new window, so just right-click to save it. |
You are subscribed to email updates from TechRadar: All Applications feeds To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
No comments:
Post a Comment