Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Defensive Programming

Name: Anonymous 2008-09-13 18:26

I used to be a student of the ``Defensive Programming'' school of programming. I'm mainly a C programmer, but I occasionally dabble in languages such as LISP. Anyways, whenever I would program in C, I would program very defensively, and check that all the arguments passed to every function were not NULL (assuming they were pointers. This turned my code into mostly a bunch of if statements.

After a while, I figured out that this was pointless, and most of the functions I write today, I don't bother checking if the arguments are NULL or not, because only a smart fucker such as myself is going to be using them. I instinctively know when everything is NULL or not. Of course, I do still check the arguments sometimes, but definitely not in every function. It just turns into a mess.

What do you guys do? Am I a retard, or are you similar?

Name: Anonymous 2008-09-13 18:29

LISP
Am I a retard
Yes.

Name: Anonymous 2008-09-13 18:46

Overcautious function argument validation shows you've spent more time writing user-facing things than you have libraries for programmers to use.
Just don't bother. Idiots who pass bogus arguments to functions shouldn't be surprised when the whole thing segfaults (and when it does, it's because of something they did wrong, not you, and they have the power to fix it themselves (unlike end users)), and people who don't will thank you for the speed gains and the increased legibility of your code.

Name: Anonymous 2008-09-13 19:01

>>1
I'm a duck programmer. I never check for anything, just catch exceptions if shit fucks up (it’s easier to ask forgiveness than permission). In particular, I never ever check for the types of anything. My functions rely heavily on dynamic typing. They work as long as they work. If I write a function double to duplicate something by adding it to itself, I'll never ever check what the fuck I'm duplicating; I just call + on the object. If the object supports +, and + doesn't fuck up with itself, then it's done.

This approach maximizes reusability and minimizes duplication of effort. My functions work with anything they could possibly work with, including objects from completely different programs; even those that didn't exist at the time I wrote my function, without any sort of massive inheritance tree or interfaces or any of that Java-ish bullshit for faggots.

Of course, I use THE FORCIBLY INDENTED LANGUAGE PYTHON.

Name: Anonymous 2008-09-13 19:27

One word, assert.h, thread over.

Name: Anonymous 2008-09-13 19:39

If you're writing a library you should check the arguments of the functions that can be called by an user of the library, because the user can pass anything to your functions. If you give an error instead of segfaulting it's easier to locate the error and the user knows immediately that it's his fault. For internal functions you don't have to check the arguments, because then only you can fuck it up.

Name: Anonymous 2008-09-13 19:41

I almost only validate variables where wrong values might lead to logical errors or if i'm sure, invalid values may be passed from time to time. In other cases I just wait for a crash to fix instead of writing 100 ifs and a few lines of code that actually do something usefull.

Name: Anonymous 2008-09-13 19:45

For internal functions you don't have to check the arguments, because then only you can fuck it up.
And we know that can never happen.

Name: Anonymous 2008-09-13 20:48

Ha! Look at you plebian programmers dabbling with dangerous side effects. It's easy for me as a functional Scheme programmer to deal only with the expected cases and disregard everything else.

Name: Anonymous 2008-09-13 21:01

When you must fail, fail noisily and as soon as possible.

I prefer a segfault than a silent fail, because the latter leads to lots of tracing and "now where did that pointer get set to null? It's going to be pretty hard to tell if the fuckton of functions called since have just been returning and letting things continue..."

I've also seen stuff like this:

a=1;
... /* a bunch of code which doesn't do anything at all with a */
if(a!=1) {
 /* some sort of error-recovery shit */
}
...


I don't think you should be checking the hardware to see if it's doing things right (unless it's a diagnostic program of some sort), because if the hardware is dying then all bets are off for the software.

Name: Anonymous 2008-09-13 21:29

>>10
>When you must fail, fail noisily and as soon as possible.

But how do you do this if you are writing a library? GLib is gross, it just logs errors and warning to stdout.

Name: Anonymous 2008-09-13 23:09

>>1
if(shit_broke){
 fork() && abort();
 for(;;){
  fputs("\a",stdout);
  fputs("\a",stderr);
 }
}

Name: Anonymous 2008-09-13 23:09

Name: Anonymous 2008-09-13 23:12

>>11
Seriously, stdout? :(

Name: Anonymous 2008-09-14 1:34

in Croatia we have a saying: a program with no error checking is just a program with error checking but with error checking removed.
the moral of the story

Name: Anonymous 2008-09-14 3:39

>>15
Crobaltislavian proverbs > Croatian proverbs.

Name: Anonymous 2008-09-15 6:49

>>15
what the actual fuck??

Name: Anonymous 2008-09-15 7:48

>>12
EXPERT MULTIMEDIA SUPPORT
if(shit_broke){
 fork() && abort();
 for(;;){
  fputs("\b",stdout);
  fputs("\b",stderr);
 }
}

Name: Anonymous 2008-09-15 14:34

┌┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┐
┤This is an x86 ├
┤microchip!!    ├
┤Copy it to help├
┤it proliferate.├
└┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┘

Name: Anonymous 2008-09-15 15:22

>>19

    ┌┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┐
    ┤This is a patented IBM/390 mainframe  ├
    ┤          31-bit millichip!           ├
    ┤       Don't copy that floppy.        ├
    ┤    Disobedience will be punished     ├
    └┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┘

Name: Anonymous 2008-09-15 23:20

Look, I fucking hate C for this reason.  What if like, I actually WANT to point to 0x00000000?  What the hell is really wrong with that?  A null pointer is a pointer too.  Fuck off, C-tards.

Name: Anonymous 2008-09-15 23:58

>>21
#define NULL 0x00000001

Name: Anonymous 2008-09-16 0:09

>>21
inline assembly

Name: Anonymous 2008-09-16 1:06

>>21
What if like, I actually WANT to point to 0x00000000?
char getb(char *ptr){
 char b;
 asm volatile (
  "movb (%1), %0"
  : "=r" (b)
  : "r" (ptr)
 );
 return b;
}
void setb(char *ptr, char b){
 asm volatile (
  "movb %1, (%0)"
  : : "g" (ptr), "g" (b)
  : "memory"
 );
}

Name: Anonymous 2008-09-16 5:47

>>21
A null pointer is defined as a pointer that never points to a valid memory location. It doesn't necessarily have to be 0x00000000. If 0x00000000 is a valid memory address on your architecture the null pointer can't be 0x00000000.

Name: Anonymous 2008-09-16 6:31

>>25
What about a system on which all integers point to valid memory locations?

Name: Anonymous 2008-09-16 7:10

>>26
just use -1

Name: Anonymous 2008-09-16 7:16

>>27
0/10

Name: Anonymous 2008-09-16 7:34

>>28
>0/10
TYPE ERROR, non valid integer

Name: Anonymous 2008-09-16 7:58

>>26
Then you need to use the null register.

Name: Anonymous 2008-09-16 7:58

>>25
Represent pointers by some type larger that int.

Name: Anonymous 2008-09-16 8:00

>>25
char nil = 0;
NULL = (void *) &nil;

Name: Anonymous 2008-09-16 9:27

   (⌒\.  ∧_∧  In an imaginary world,
    \ ヽヽ( ・ω・) are we even responsible for our actions?
     (mJ     ⌒\
      ノ ________/ /
     (  (。・-・)< What are you doing?
  /\丿 l|\⌒⌒⌒ \
 (___へ_ノ. \|⌒⌒⌒⌒|

Name: Anonymous 2008-09-16 11:15

>>33
I'm fucking your sister

Name: Anonymous 2008-09-16 11:49

>>34
Fisting your sister

Name: Anonymous 2008-09-16 11:50

>>33

                         ▂ ▪ ▂▄▅▆▇■▀▀〓◣▬ ▪ ■ … .
        ∧_∧           .▂▅■▀ ▪ ■ ▂¨ ∵▃ ▪ ・
        ( ・ω・)< It's a mystery  ◢▇█▀ ¨ ▂▄▅▆▇██■■〓◥◣▄▂
      /⌒\  ⌒\     ■ ▂▅██▅▆▇██■〓▀▀ ◥◣ ∴ ▪ .
      ノ \ \,_/ / ▅▇███████▀ ▪ ∴ ….▅ ■  ◥◣
     (  | ̄\▓░█▅▆▇████████▆▃▂  ▪ ■▂▄▃▄▂
  /\丿 l|\⌒⌒▒▓\        ■  ¨ ▀▀▀■▀▀▀ ▪ ■
 (___へ_ノ.\|⌒⌒⌒⌒|

Name: Anonymous 2008-09-16 11:52


            ∩_∩
          ry ´・-・`ヽっ
          `!       i
          ゝ c_c_,.ノ  
             (                 
              )        
         .∧ ∧.(         
         ( ・ω・∩    After these messages,
         o   ,ノ    We'll be Riiiiiiiiiiight back!
        O_ .ノ
          .(ノ
         ━━

Name: Anonymous 2008-09-16 11:53

My programs are Java faggotries with several layers of diamond armours and nukes.

Name: Anonymous 2008-09-16 11:54

              ∩_∩
          ry ´・-・`ヽっ < What he doesn't know is
          `!       i that this is killing my
          ゝ c_c_,.ノ   asshole.
             (                 
              )  
               (
               /
              /
             (

Name: Anonymous 2008-09-16 11:55

   人     
  (__)    
  (__)   
 ( __ )    
 ( ・∀・) <My name is Squeeks and I am derailing this thread.
 (つ   つ  
 | | |
 (__)_)

Name: Anonymous 2008-09-16 12:09

this is not /dqn/

Name: Anonymous 2008-09-16 12:17

   人     
  (__)    
  (__)   
 ( __ )    
 ( ・∀・) <My name is Squeeks and I have shit on my head.
 (つ   つ  
 | | |
 (__)_)

Name: Anonymous 2008-09-16 12:19


   へ-ヘ
   ミ*´ー`ミ Java!
 ~(,_uuノ

Name: Anonymous 2008-09-16 12:26

this is not /dqn/

Name: Anonymous 2008-09-16 13:03


   へ-ヘ
   ミ*´ー`ミ Is this /dqn/?
 ~(,_uuノ

Name: Anonymous 2008-09-16 13:10

hEllO iM DAdDYCOOL thE programme Rif you payme eNough i Will gIve yoU access tO a pRivAte aRea of pRogrAmming ;)
                                  /\___/ヽ           
    (.`ヽ(`> 、                      /''''''   '''''':::::           
     `'<`ゝr'フ\                 +  |(●),  、(●)、.:| +      
  ⊂コ二Lフ^´  ノ, /⌒)                 |  ,,,ノ(、_, )ヽ、,, .::::|       
  ⊂l二L7_ / -ゝ-')´                .+ |   `-=ニ=- ' .::::::| + .    
       \_  、__,.イ\           +     \   `ニニ´  .:::/    +  
        (T__ノ   Tヽ        , -r'⌒! ̄ `":::7ヽ.`- 、   ./|  . 
         ヽ¬.   / ノ`ー-、ヘ<ー1´|  ヽ | :::::::::::::ト、 \ (  ./ヽ    
          \l__,./       i l.ヽ! |   .| ::::::::::::::l ヽ   `7ー.、‐'´ |\-、 

Name: Anonymous 2008-09-16 13:12


          ∧_∧
         ( ・ω・) Is >>46 even happening?
         (  つ(\
      (\_ノ(___)⌒ ⌒ヽ_
      ) ____  ・_つ
     (/      (/
   。 ゚
  。

Name: Anonymous 2008-09-16 13:14

You guys are making me ashamed of being a VIPPER. Seriously, cut this.

Name: Anonymous 2008-09-16 13:17

 ∧_∧
( ;´Д`)
人 Y /        
( ヽωつ ο°o。 
(_)_)

Name: Anonymous 2008-09-16 13:18

ashamed of being a VIPPER
you're doing it wrong if you're not ashamed

Name: Anonymous 2008-09-16 13:18

You guys are making me ashamed of being a PROGRAMMER. Seriously, cut this.

Name: Anonymous 2008-09-16 13:19


      /\___/ヽ
     /       :::::::\
    .|          .::::|
    |  ''''''   ''''''   .::::|
    .|(●),   、(●)、::::|
     \ ,,ノ(、_, )ヽ、,,.:::::/
     /``ーニ=-'"一´\
   _/((┃))_____i |_ 
.. / /ヽ,,⌒) ̄ ̄ ̄ ̄ (,,ノ   \
/  /_________ヽ..  \

Name: Anonymous 2008-09-16 13:19


       /\___/ヽ
     /''''''   '''''':::::::\
    . |(●),   、(●)、.:| +
    |   ,,ノ(、_, )ヽ、,, .::::|
  .   |   `-=ニ=- ' .:::::::| +
     \  `ニニ´  .:::::/     +
     /ヽ、ニ__ ーーノ゙\_
    .| ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄.|  トン
   _(,,)   J A V A   (,,)_
.. /. |..           .|  \
/   .|_________|   \

Name: Anonymous 2008-09-16 14:04

this is not /dqn/

Name: Anonymous 2008-09-16 22:15

>>54
are you sure?

Name: Anonymous 2008-09-16 22:23

I'm an offensive programmer.  I bombard the user with so much shit they don't have a chance to crash the program.

Name: Anonymous 2008-09-16 23:16

>>56
I snorted while I lolled

Name: Anonymous 2008-09-16 23:52

>>56
I chuckled.

Name: Anonymous 2008-09-17 0:13

>>58
I chuckled while I snorted while I loled while I stared humorlessly.

Name: Anonymous 2008-09-17 6:08

Back in Croatia, my mother country, we have a saying about programs. The saying goes that programs without error checking are equivalent to programs with error checking, but with error checking disabled. The moral of the saying is that the user has no way of knowing if a program has or has not error checking. Much like how Schroedinger's cat may or may not be alive inside that box, you have no way of knowing.

Name: Anonymous 2008-09-17 12:29

>>11
Segfault, duh.

Don't change these.
Name: Email:
Entire Thread Thread List