Is defensive programming out?

Hi,
I wondered if I am a naturally talented software tester, because on trying some random software I easily find ways to make it segfault.
Two examples from today include rosegarden and kazehakase.
rosegarden, default configuration:
Start it, doubleclick on the first measure, choose menu "segment" -> "new layer", click the undo button, enjoy segfault.
kazehakase, default configuration:
Start it, choose menu "view" -> "ui level" -> "medium" or "expert", choose menu "view" -> "view page source" while no tab is open (as it is with default config on first start), enjoy segfault.
So first kazehakase: Yes, I know it is a very early version. But I can live with missing functionality like "view page source" doing nothing. The problem is crashing on issueing such command. Without looking at the code I bet this is a null pointer (because there is no page displayed) of which the developer thought "oh, this probably won't ever be null".
Next is rosegarden which wants to be "professional".
I believe this bug with "undo" to be similar to the one in kolourpaint (start kolourpaint, choose text tool and write a letter, press strg+a, strg+z, this should be fixed, but at least now not in the repos.). Maybe it's a problem in qt in general or a problem with implementing "undo" in general. I don't care.
When implementing an undo function I would be EXTREMELY careful to what happens in this functions.
You have to be ready to undo ANYTHING that the user can possibly do in your program which is hell of a lot.
In these programs people apparently didn't use defensive programming: I can see the programmer sitting there and think about how to undo several things but not how to handle something coming into his undo function that it cannot undo.
I also believe plasma from KDE to crash so much instead of displaying an error message because of that.
The question I ask myself is: Why do I see so many segfaults and so few error messages?
Is it time for a kernel API to inform the application that it segfaulted and give it a chance to recover, maybe even with user intervention?
Last edited by Cdh (2010-06-27 20:43:51)

bernarcher wrote:
I think defensive programming, even "simple" exception handling, never was widely used. It is by no means trivial and tends to bloat the code, esp. in languages with no exception handling mechanisms built in, like C for instance.
Even using languages which do provide fairly good mechanisms for exception handling (like e.g. Eiffel, or D) it is difficult to catch edge cases in a meaningful way (other than simply finishing program execution). More often than not exceptions do not occur locally to the program part which really needs to handle the situation. And if they do they lead to more or less combinatorial explosion if not thoroughly planned. Thus, making a programm foolproof from start as "defensive programming" mandates is a tremendous effort (and not overly exciting to do).
Even if you try to handle system signals, things tend to be involved and complicated. Just have a look at theSignal Concepts in the Open Group  Base Specification. Or have a look at man signal and related man pages.
This is not to state defensive programming to be fruitless. But the efforts in time (and money) are most often not taken. You can't easily show built-in defenses. Being able to get a program "just running" and showing some effects appears far more exciting.
It is like thorough testing (beyond running a unit test suite), and documenting (both software and user sides) - most desirable, but not many want to really do the works.
Well said. I can't really elaborate on this but I'd have to wholeheartedly agree.
(Sorry I can't elaborate I whenever i'm coding something thats one of the few things I try to look out for).
Last edited by Ari'osika (2010-06-28 09:49:05)

Similar Messages

  • Defensive programming vs exception handling

    Hi
    im new to java programming and having problems getting my head around defensive programming vs exception handling.
    i've read in my uni books about design by contract and the idea of defensive programming but im having problems with it.
    so say i write the code.
    String container = //some value
    if (container.equals.("")) // so if i am looking for a value and find a empty String
       throw new // a custom exception i have made for this error.
    else
       //do whatever i had planned with a non-empty String
    }is this Defensive programming or exception handling. Or am i complete of base.
    thanks

    jverd wrote:
    JustSomeGuy wrote:
    Don't fall into the trap of saying "Meh, the exception will handle it". If you can forsee something easily being a problem, and it can be controlled, then control it. Save the exception handling for those things you don't forsee and those you can't control. I disagree. Or at least, I disagree with what I think you're saying.
    For instance, if null is not a valid argument, then document it, and don't waste code defending against it. Either throw an explicit IllegalArgumentException, or let the NPE be thrown.
    And absolutely under no circumstances should you ever write code that will deliberately lead to an exception, Eh? If an exceptional condition occurs, you should delibrately throw an exception. Consider if you will the following code snippet
    import java.io.*;
    class ExceptionExample {
        public static void main(String[] args)
            throws IOException {
            if (args.length == 0) {
                System.out.println("Must give filename as first arg.");
                return;
            FileInputStream in;
            try {
                in = new FileInputStream(args[0]);
            catch (FileNotFoundException e) {
                System.out.println("Can't find file: " + args[0]);
                return;
            int ch;
            while ((ch = in.read()) != -1) {
                System.out.print((char) ch);
            System.out.println();
            in.close();
    } What I was saying in the first part is shown with the following part of the above code
    if (args.length == 0) {
       System.out.println("Must give filename as first arg.");
       return;
    } Would the exception handler that follows this peice of code have taken care of that? Sure! Would it be the best choice to just let the exception handler do it? No way no how!
    It is easy to forsee someone forgetting or not realizing they need to add the filename as an arg and it can be controlled, so in this and most cases just like it the best choice is to either inform the user with a more specific statement than could be used in the exceptionhandler and do nothing or fix it yourself like for instance by using a default filename.
    What I meant by deliberately throwing an exception can be illustrated with this small portion of my code above
    int ch;
    while ((ch = in.read()) != -1) {
        System.out.print((char) ch);
    } A -1 to mark the EOF is expected and reaching the EOF is expected. There is absolutely nothing abnormal about this, so it should not be handled by an exception. But far to often I have seen novice and especially "think they know it all novices" use an exception in place of exactly this. They know the file will end, they know they must do something about that, but instead of doing it right they just set it up so an exception will be thrown and they can just handle it in the exceptionhandler. This is what I meant by deliberately throwing exceptions, in that an exception must be thrown in order for their program to work as intended. Bad, very bad.
    Now that I have provided an example and further clarified my position do you still disagree? I can't imagine you would, so if you do I would be very interested in getting some further insight into your reasoning.
    JSG

  • Uninstall PC Theft Defense Program????

    Hi, I just bought this new Sony Laptop from Best Buy and installed the program PC Theft Defense program that gives you 3 years of free service in case your Laptop is stolen.   Well after installing it I realized that it wasn't  something that I needed as i am disabled with a back injury and the laptop never leaves my house/side so there would be no way it could be stolen.  
    The reason I am writing is because the icon keeps coming up where the clock is on the right and when I tried uninstalling it from the Control Panel there is "no option to uninstall" it like all the other programs that are in my Laptop.    I deleted the programs off my C:/ drive but it keeps coming up and showing in the icon area, I know I can shut it off so it doesn't show in there but it will still  be starting, at least this way I can exit it when I start my Laptop so it doesn't use up resources that don't need to be.
    So is there a way to remove this software somehow??   I'd hate to have to bring it in to Best Buy for them to remove if there is an easy out for me.   I went to the PC Theft site and signed in and it said there are no computers set up for this service so I think I cancelled it somehow the right way just want it off my Laptop.  Thanks, Jim

    harleynh wrote:
    Hi, I just bought this new Sony Laptop from Best Buy and installed the program PC Theft Defense program that gives you 3 years of free service in case your Laptop is stolen.   Well after installing it I realized that it wasn't  something that I needed as i am disabled with a back injury and the laptop never leaves my house/side so there would be no way it could be stolen.  
    The reason I am writing is because the icon keeps coming up where the clock is on the right and when I tried uninstalling it from the Control Panel there is "no option to uninstall" it like all the other programs that are in my Laptop.    I deleted the programs off my C:/ drive but it keeps coming up and showing in the icon area, I know I can shut it off so it doesn't show in there but it will still  be starting, at least this way I can exit it when I start my Laptop so it doesn't use up resources that don't need to be.
    So is there a way to remove this software somehow??   I'd hate to have to bring it in to Best Buy for them to remove if there is an easy out for me.   I went to the PC Theft site and signed in and it said there are no computers set up for this service so I think I cancelled it somehow the right way just want it off my Laptop.  Thanks, Jim
    I wonder if its controlled on a BIOS level... If disabled in  the BIOS the software won't react no more?
    *******DISCLAIMER********
    I am not an employee of BBY in any shape or form. All information presented in my replies or postings is my own opinion. It is up to you , the end user to determine the ultimate validity of any information presented on these forums.

  • Pulling a program out of the IDE

    I have been playing with pulling small progrograms out of my Integrated development environment (IDE); writing them to CD; and installing them on other desktops, just as an exercise in the case I want to deliver some applications to a friend.
    The overall requirements(steps) are not clear to me.
    1.the directory organization
    2. the file (jar) creation
    3. the D0S (java.exe) window
    Are some of the issue that come to mind.
    I have been able to use a desktop icon and set the path to a program.class file and run it--making the program window appear but the java.exe Dos window also appears.
    So far, the number of texts I have give no step by step instructions on how to configure an environment without an IDE with the java VM already intalled, or how to pull a program out of the IDE and install it along with a copy of the VM on another machine.

    Good question. Lot's of people don't know this, and I can't even say I have all the right answers. There are probably numerous ways you could do this. The best way, if you have the money, is to buy something like InstallAnywhere or Wise that provide professional installations. Not only will your install look like professional installation of stuff like Photoshop, etc but it will also handle issues like OS platform specifics, environment variables, registry entires (if possible), the proper JRE/JDK installed... if not install it, uninstallation features, and so forth. That is by far the best way to go.
    However, often you just want to bundle something up and send it out to a few friends, family, etc. You could of course look for an open source or freeware utitlity and no doubt there are some out there. Or, you could do what a lot of people do now, use the Ant tool (written in Java) and have a build script that does everything for you. I have a simple template build script that is used for various tasks. In our work setup, it does local dev (incremental) compiles, and if necessary packaging it up into .jar/.zip files. My single build script can also run junit tests and reports, builds executable packages into .zip and .jar's as necessary, will do a clean build (great for making sure any code changes don't break some code elsewhere, thus ensuring that your normal incremental build process is not missing something), it can FTP or copy files to locations, it can email results, and so forth. If you don't know about ant, I STRONGLY suggest you learn it quickly, and to me there is no better way than buy the book Java Development with Ant. Two of the main Ant developers/committers wrote the book and it does an excellent job of teaching you about all the different uses of Ant, including the template build script I now use. It is well worth the $40 or so you'll find the book for, and within a week you'll have a good build template that you can modify for specific projects.
    Now, in terms of the 3 things, well, the directory organization is always changing. Each project may have something different, but here is one that I usually use:
    <root project dir>: (below this you'll find)
    /src - location of all src files
    /src/java - the location of the project specific java code
    /src/examples - if need be (especially useful for frameworks and API's you wish to share) show example applications using the output of the /src/java code
    /src/test - location of JUnit, jfcUnit and other test files to unit test your code (very important, should be done no matter what!!)
    /lib - location of any 3rd party libraries you need, both for compilation and runtime. Keep in mind that your build script may pull only a subset of these libraries in for runtime. For example, the /src/test that uses JUnit will require the junit.jar file. You won't need this for runtime, so your build script sets up a compile classpath with junit.jar, and a runtime classpath that will exclude junit.jar, perhaps others.
    /dist - the location that the build script puts all distributable files. This would be any executable .jar file, any .zip file, perhaps other files necessary for distribution. If you were making a large application and planned to burn it to disk, instead of a single .zip file, you may actually build the sub-dirs below the /dist/<your app> type of dir, so that you can simply burn from that <your app> dir to the root of the cd.
    /build - location of all compiled classes, including from /src/java, /src/examples, /src/test.
    /build/classes - the location of compiled java code
    /build/test - the location of test results, including perhaps test data to be used by the test code, and possibly the output dir of test reports
    - /build/test/reports for example for output of test reports.
    /doc - location of any documentation, including the JavaDoc produced by the build script from your src.
    <project root> - this is the location of your build.xml ant build script, if need be. The build script, as you would learn in the book, is set up so that you can move it anywhere and it will still build your project correctly. You may opt to put it in the /scripts dir instead and keep the project root super clean.
    /misc or /etc - common location to put things like properties files, misc files used to build projects. For example, in the building of a EJB application (.jar or .ear file), you will no doubt need the META-INF/application.xml file. Or in a .jar file you need a special META-INF/manifest.mf file. The /misc or /etc dir is a good candidate for these types of things. You can even package it up in your app so that in the .jar file you have /etc/properties/somepropertyfile.properties, and in your code do a simple getClass().getClassLoader().getResource("/etc/properties/somepropertyfile.properties") to always ensure you pull it from this directory. OR, your build script can "change" the location this file goes. It is all up to you.
    That is what I use for all my projects, the dir structure anyway. What you do to build your .jar/.zip files is up to you. You usually don't need to pull the /src dir, the /dist dir, etc into your .jar/.zip files. Normally you'll have the compiled classes at the root. So in your project dir on your disk, you may have <project root>/build/classes/java/org/mycompany/ui/Main.class, and in your .jar file it would simply be <root of .jar file>/org/mycompany/ui/Main.class, and your build file can easily "change" the root path (using <zipfileset prefix=""> task) to change the path of any file. But more of that can be learned in that book and by looking up resources on the net. The Ant JavaDoc API is very good at explaining most of these as well, and there are TONS of ant scripts in most projects to look at for examples.
    The file creation, well, as I said Ant has built in tasks you can use to create .jar files, .zip files, etc. All of that is all built in to how Ant works.
    As for java.exe, well, in a professional app, you usually do one of two things. You either write a simple C/C++/Delphi/some other languages .exe wrapper (for windows) that internall executes the java.exe and runs the JVM to hide the window, as well as provide a .exe file to look more "native", or you run javaw.exe, which hides the Console output window for most apps. NOTE: if you create a .bat shortcut on your desktop to javaw.exe, you will STILL get a console window that shows up. ALL shortcuts to .bat files will always create a console window. Many java developers don't understand why they run javaw.exe from a shortcut on their desktop and still see the console window.
    Now, hopefully that helped a bit. But if not, or you still have questions, I am watching this thread so fire away and I'll help more.

  • Any Patient Tracking Program out there?

    Does anyone know if there is any simple patient tracking program out there?
    Functions that will be needed is patients demographic information, name ,age, sex, number of times they visited, patient number visits in a single day, as well as a way to bill them and invoice upon visit.

    If there is nothing specific you can research FileMaker Pro for any ready made forms they might have that would be similar in nature to what you want.

  • Contract and defensive programming

    I'm exploring the differences between Contract and Defensive programming. I'm seeking any comments from the community on the subject of these two apparently opposing paradigms. (Or am I wrong? Do they oppose?)
    Contract Programming, or Design by contract, is a programming paradigm whereby classes define their behavior and interplay by "contracts" which are defined in terms of pre-conditions, post conditions and class invariants. In this paradigm, the code must "fail hard", and resist verifying that the contract conditions are met.
    Defensive programming is an opposite strategy. In this paradigm, the assumption is made that the pre conditions of a method call ought to be tested within the method for assurance. This can lead to extra development and a potential veiling of errors elsewhere, such was where and how the call is made.
    My question is, which of these is the preferred method, or ideology of development and why?
    Thanks for any feedback and links to external sources which may be helpful.

    Just asking a clarifying question:
    I'm not sure of the difference between the 2, and my understanding is Java doesn't 'directly' support Design by Contract. I have only
    seen this in Eiffel (in school), but my question, aren't manually coded 'precondition checks' the way to support Design by Contract in Java?
    lance.walton wrote>
    <The important difference, which you've kind of alluded to, is that defensive programming tries to consider what contract violations might occur and provide <strategies for dealing with this. The result is more code that will ideally never be executed. This clutters the intent of what you're trying to achieve as well <as probably introduce more defects because of the clutter. It also suggests that the client code might make mistakes that you can help it with.
    <
    <Even if you don't do Design by Contract (hard to do properly in Java), I would not do defensive programming. It allows sloppy client code, and that <allowance then has to be maintained, which will get harder and harder as your code evolves until you start talking about a rewrite because it's such a <mess.
    If you want to protect yourself against poor client code, use precondition checks.

  • Is there a program out there that will repair .zip files in OS x?

      i have a zip file that i cant open i get a crc error.Is there a program out there that will repair .zip files in OS x?

    Klaus1 wrote:
    its a movie taken on i movie.
    iMovie projects do not like being zipped. Unless you have the unzipped original you will have to create the project again.
    Furthermore there's probably no point in trying to zip it.  Many media formats such as mp3, AAC, mp4, jpeg are already compressed and compressing again just adds overhead (making them bigger) as well as placing them in a format that is hard to recover anything from should something go wrong.

  • My PS cs6 ask me to buy the program.  I bought it several years ago.  This is the third or 4th time I have had to talk to someone to straighten the program out.  How can we remedy this problem PERMANENTLY?

    My PS cs6 ask me to buy the program.  I bought it several years ago.  This is the third or 4th time I have had to talk to someone to straighten the program out.  How can we remedy this problem PERMANENTLY?

    What steps are you following each of those times to get things working again?
    The way that Adobe activation and serialization works is kinda like signing into a website. When you log in, a cookie is left on your computer so that you don't have to sign in every time. With Adobe activation and serialization, you get similar cookie files, that way you never have to go through that process again. Adobe never touches those files again, it just checks them to make sure they are there.
    You issue you are encountering is the result of this sort of cookie file being deleted, damaged, or invalidated in some way. In most cases, it is because of cleaning software treating it like a website cookie, thinking it is unimportant, and removing it. However, there are many other possibilities including files becoming corrupted due to disk or file handling errors by the system and major system changes.
    To take care of the problem permanently, we have to find out exactly what the source of the issue is on your computer and eliminate it (or mitigate it if removing third-party software is out of the question).

  • Is there a a good cd software program out there like Jam 6.0.3 that will work on 10.7 or later OS? One that will crossfade individual songs on your playlists?

    Is there a a good cd software program out there like Jam 6.0.3 that will work on 10.7 or later OS? One that will crossfade individual songs on your playlists?
    I am happy with 10.6.8 but I have to upgrade to 10.7 or later to run a drobo set up that I need and Jam will not run on the intel.
    I have imac  2.7 i5 intel with 8 ram running 10.6.8.
    Thanks for any help.

    Hi, I'm not sure it does what you want, but only thing I can think of is Audacity...
    http://audacity.sourceforge.net/download/mac

  • How can I select the Quicktime plug-in (in Firefox) as the Helper Application, as only programs out of the web browser are shown?

    I recently updated my QuickTime Plug-in (7.7.4) on Firefox 21. Prior to the update, I could open midi files in the Firefox and other browsers with a QuickTime Player. Now, I've got my Windows 7 computer asking how it should save or open the file. The QuickTime Plug-in is enabled and works with other types of files. If I open Tools, select Options, and select Applications, I do have the option of selecting "Use QuickTime Plug-in 7.7.4 (in Firefox)" for many file types. For the "Midi Sequence" type, it isn't an option, (only programs which open the midi files outside of the browser). Presently my only option is to use "Windows Media Player" as the default program, (opening midi files outside of the browser). "Mid file" allows the selection of the QuickTime Plug-in but still goes out of the Firefox browser to use the Windows Media Player, whether the file's extension is .mid or .midi
    Is these someplace I can find the "Use QuickTime Plug-in 7.7.4 (in Firefox)" with the "Browse" within my computer's files, or am I now stuck using the "Windows Media Player" when a plug-in should be able to do this simple task? I can watch YouTUbe (in Firefox) but cannot play a midi file (in Firefox)!

    QT 7.7.4 use "audio/mid" MIME type only and does not understand now "audio/midi" and "audio/x-midi" types commonly used on server side. Blame Apple for that.

  • How to convert concurrent program out files .out file to .txt files in

    Hi
    Trying to know if there is a way to convert the concurrent programs' output files witn '.out' extension to files with extension '.txt'
    thanks
    kp

    Why you want to change the file extension to txt instead of out?
    I believe you cannot do and you are not supposed to change it. Controlling the report output to different types can be done as explained in these docs.
    How to Setup The Report Output to Different Viewer Types in Oracle Applications 11i [ID 184375.1]
    How to Control the Name and the Application that Opens the Concurrent Request Output File on the Client? [ID 316752.1]
    Thanks,
    Hussein

  • Any Better EMail Program Out There for Macs?

    I used Eudora as my email program for 10 years and loved it. Unfortunately, it became very buggy on the Snow Leopard operating system. Entire mail boxes got corrupted. So, I had to find a new mail program. I was told that Apple Mail was the best and have been struggling to have it meet my needs for the past few weeks.
    Is there any other program available out there that is more like Eudora, but still supported with a new version for Snow Leopard - or at least an be run on this operating system?
    Thanks!

    IMHO, Mail is the best. It will take a lot of getting used to for someone migrating from Eudora, though. I used to use Eudora as well, but gave it up years ago in favor of Mail.
    Thunderbird will have some features that Mail lacks, but in turn lacks a lot of the polish and some very basic features that Mail has. For example, my wife was trying just two days ago to send a message to a subset of a group defined in Thunderbird's address book, and after ten minutes of both of us trying to figure it out, we finally gave up and she used _pen and paper_ to note the addresses. Doing this would have been trivial in Mail. These sorts of little not-uncommon niggling issues would prevent me from ever using Thunderbird myself.
    You could also try [MailSmith|http://www.mailsmith.org>, which was a pretty compelling mail client once upon a time, but which I haven't looked at in many years.

  • How to get programs out trash

    Well I cleaned out some of documents today and accidently put a few programs in the trash,but when I try to open them,it stated couldn't open as it was in the trash,so I took them out of trash on place on the desktop.How can I put them back where they are suppose to be and get the extra icon off my desktop.Thanks for any help

    pattishelbytwp wrote:
    How can I put them back where they are suppose to be and get the extra icon off my desktop.
    Just drag them back to where they had originally been. If they are programs rather than documents, they probably belong in the Applications folder, the one you see when you first open the hard drive's icon.
    However, if they are widgets or utility apps, they may need to be put in a different place.

  • I tried to download images from a jump drive and now the jump drive seems blank. How can I recover the images? Is there a software program out there to do this reliably?

    I took photographs with my Lumix camera (JPG) and put the sd chip into a jump drive and put them into my usb port to load. The images never loaded and now the sd chip has no images when I put it back into the camera. What can I do to restore the images? Is there Mac compatible software out ther to restore them? I am running a Macbook Pro OS X v 10.7.5. Thanks for your help.

    I'm not sure I can help you with this situation. Odds are you probably can't recover the images if they aren't hidden in the card, but this just seems to be a fluke. I've had flash drives that be plugged in only to find out that was their iminant death.

  • Is there a program out there to convert appleworks data to pages data

    Is there a program that will convert appleworks date to pages data?

    Word Processing documents can be opened directly only in Pages (AppleWorks 6 only).
    Draw Documents can be opened directly only in EazyDraw (the more expensive version from their website, not the cheaper one from the Mac App Store, and you will need v4.0 'Retro' not the most recent version) or Intaglio (though this appears not to be able to handle very complex drawings).
    Spreadsheets can be opened directly only in Numbers.
    Presentations can be opened directly only in Keynote.
    To use other programs, or open Painting or Database documents at all, you must export from AppleWorks first.
    If you can no longer run AppleWorks you cannot open these document types.
    This article examines the situation in a lot more detail:
    http://rfwilmut.net/aw

Maybe you are looking for