1

In our company there once were multiple programmers, now i am the last man standing. I have inherited all the code and i maintain, rewrite, extend (,fix) it.

I always run into this behaviour that i try to make stuff fool proof so that a dummie co-worker couldnt mess up the code. But there are limits to what i can do. I cannot prevent that someone holds a reference to an object. Even java/android system classes give you an object that you are not supposed to hold on to but just use it locally and thats it.

In our company a lot went wrong and i am kind of a burned child, i see possibilities how someone could do the wrong thing and the code keeps crashing and I am asked "why do you write stuff like that"...

Right now i am the only programmer but in the future maybe we have more (again), but right now i kinda restrain myself by spending too much time on trying to figure out how i could write my stuff so that the least amount of mistakes can be made with it. Even tho I would be the one who had to make the mistake.

Like everyone i do make mistakes ofc. So i null-check etc and write limits into the java-docs to remind myself what values i may pass etc. those simple checks are not what im talking about. I am talking about that often i could write a method in a simple and good way but some voice in my head says "oh no, what if SOMEONE would pass a wrong parameter or did this and that with it" and suddenly i find myself encapsulating logic and making stuff more complicated only to add some sort of protection against... dummie programmers with me being the only one possible.

I am NOT working on any open API or shared stuff. There will (most likely) never be any code audits etc. I am the only one using the code and it will stay like that for a long time.

So how do i stop myself from doing this fool-proof coding that costs me too much time and brainpower.

EDIT: i read the other question + answers. I understand the whole 'defensive programming' but i wonder when am i taking it too far ? And thats more my problem.

marked as duplicate by gnat, Bart van Ingen Schenau, user53019, user40980, amon Sep 2 '14 at 20:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    there will always be an idiot that will mess up in the oddest ways, as soon as something is declared fool-proof a bigger fool will break it – ratchet freak Sep 1 '14 at 12:30
  • 1
    what programming language are you using? Java? – gnat Sep 1 '14 at 12:31
  • java/android mostly but also objective c, the 2 big app-os' – NikkyD Sep 1 '14 at 12:36
  • 1
    Try, if possible, to write code you won't be ashamed of in a few months. – Basile Starynkevitch Sep 1 '14 at 12:38
  • 4
    If it were possible to write a program that anyone could understand, anyone could understand it and you and I would be out of a job. Programming, in general, is complicated despite our attempts to make it simple. That's why we still have jobs. :) – Neil Sep 1 '14 at 12:39
4

As someone who started in Basic and C; I have a policy:

If it can explode, make sure it explodes.

Paranoia with error checking and defensive styles often create slower and more difficult to debug code. (And can introduce its own bugs) The reality is that code must be documented and that you should expect a reasonable developer to use it. If you are doing null checks all over the place then there is something wrong with the design in general.

The more you try to hide things the more you often hurt yourself or your team. It comes back to bite you in the butt.

  • 2
    i think i read this somewhere "crash early, crash hard" or something like it – NikkyD Sep 2 '14 at 16:11
  • That's the common idiom. I just didn't remember it. The idea is the same. – lots of crisps Sep 2 '14 at 16:13
  • "Crash Early, Crash Often". It's a book.. – FirstOne Jun 13 '18 at 18:30
3

The best way to make code child-proof is to make it decoupled into modules, then each module can have its API documented, and can be designed to do 1 thing well.

This will not stop a child taking your network module and putting it into DVD slot, but it will allow you to tell them off afterwards for not using it as it was intended - as proven by the documentation.

This does mean you have to spend more time designing your classes and modules and the interface boundaries, but its time well spent.

1

Okay, so what I understand you are trying to get at (maybe understand), is that you need to make sure that no-one messes up your code? (because if they messed up the code things would break). Maybe the solution to your over-worrying is not making your code child proof, but instead, increasing redundancy, you should consider using subversion. Subersion basically allows you to save a project to a repository (repo), then when you want to make changes to the code, you 'branch' the project, meaning that you (or any other programmer) can make changes to the code in this branch without it affecting the 'trunk' (main copy) so if the code does get messed up, it is only in the branch and not actually in the real version, but if significant improvements are made to the code then you can merge your branch with your trunk meaning that the changes take place in the real working copy! So as long as these 'dummie' programmers are only working on the branch, then nothing gets messed up. Another cool function is if it does get messed up in the trunk, you can just go back in time to a time when those changes weren't made. Subversion really is a life-saver, consider beanstalk as your repo-base and TortoiseSVN as your subversion software.

  • 1
    Agreed, but I would recommend git over svn any time! (even for a single developer project) – Basile Starynkevitch Sep 1 '14 at 13:16
  • It depends on preference I guess! The main point is redundancy is a better way of saving time and effort than 'child-proofing' – seanyt123 Sep 1 '14 at 13:17
  • already using SVN (for android) and git for ios – NikkyD Sep 1 '14 at 13:25
  • So then you shouldn't be worried about child-proofing code, who cares if a programmer messes up your code if it is easily fixed, it will just act as a learning curve and their mistake won't have any affect – seanyt123 Sep 1 '14 at 13:26
1

I like the answer of @misterbiscuit, although I'm a Java guy and have very little experience with C (seems some concepts are exchangeable beyond the programming language).

I'd like to amend:

  • If you write software someone's (my) life could depend on, I'd encourage you to double- and triple-check every fckn value. Otherwise, read on.
  • Conventions - if null is a valid input-paramter, you need to check it. If, by convention, null is not allowed (but for some special cases), you can assume you won't get it. There are good books available every developer should have read ("Clean Code", "Effective Java")
  • Unit-Tests - when changing legacy code, I tend to write unit-tests for the current behaviour. This leads to many tests that seems to be wrong, in fact it is just the behaviour of the production code that made me to write the test that way (to make it green). You could do the same trick, but upfront - write tests that document the current behavior.
  • Documentation - it's necessary to write down architecture and concepts in a meaningful extent. Nobody reads a 300 pages document before starting coding (except it is called "Java for Beginners"). I like the concepts of arc42, because you can document architecture in 3 pages, if you like. and then add some common rules and you are done. (However, it depends on many more factors of how documentation will be)
  • 1
    If within a given situation something should never be null then you should not check for null. Nulls could arise from another person trying to use code in a way it wasn't designed for. They deserve it to explode. I write a lot of Java. In some ways it can be even harder to properly structure and manage errors. When i see devs with catch blocks all over the place i want to puke because they dont understand that error should not be caught for the sake of catching errors but because something useful should be done when it is caught. – lots of crisps Sep 2 '14 at 16:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.