Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write correct code at the first time? [closed]

Tags:

correctness

I usually make lots of mistakes (logic errors, syntax errors) in the first attempt to accomplish some programming tasks. I have to write unit test to detect those bugs. This is especially problematic when I am in an interview. In that situation, I am under pressure and I can not test my code with compiler and unit test.

My question is that how can I write correct code in the first place? I know it is difficult. Is there any pragmatic approach to reduce bugs at the first time?

I was required to write a function that receives a pointer to an int array and the size of the array. Fill that array with prime number. It's not a difficult problem at all. But I made lots of mistakes at the first time and keep finding out new bugs. Since it was a phone interview I was asked to type the answer and send it thought mail.

My first try

    void prim(int * array, int size) 
{ bool isPrime = true; 
for (int i=0;i<size;i++) 
{  
for (int j = 2;j<i/2;j++)  
{    if (i%j==0){
        isPrime = ture;
        *array = i;
        array++;

      }  } } }

It was a really bad idea to write code in gmail. I will never do it again. I knew the program needs two for loops. However there are some big problems in the code

  1. 'i' should not increase at the end of first loop.
  2. if i%j==0, isPrime should be false
  3. the array operation should be put at the end of loop when we find a prime

My second try, I added isPrime test, fixed some errors and resubmitted it.

    void prim(int * array, int size)
    {
    bool isPrime = true;

    for (int i=0;i<size;i++)
    {

     isPrime = true;
 for (int j = 2;j<i/2;j++)
 {
   if (i%j==0){
    isPrime = false;
  }

if (isPrime)

{
    *array = i;
    array++;
}
 }
}

There are still some fundamental errors in the code. But my time is up, I try to fix them in my third try.

  void prime(int * array, int size)
    {

   assert(array != null)
   assert(size>0)

    bool isPrime = true;

    for (int i=0;i<size;)
    {

     isPrime = true;
 for (int j = 2;j<i/2;j++)
 {
   if (i%j==0){
    isPrime = false;
  }

if (isPrime)
{
    *array = i;
    array++;
   i++;
}
 }
}

Finally. After the interview, I felt that I really massed up. I copied the code into my IDE, fixed all the bugs and tested it. I sent this to my interviewer and said sorry about that. I was really surprised by how many errors I could make in such a small problem. Final Version

#include <iostream>
#include <assert.h>
using namespace std;


void prime(int * array, int size)
{

    assert(array != 0);
    assert(size>0);
    if (size == 1){
        *array=2;
        return;
    }
    else {
        *array=2;
        array++;
    }
    bool isPrime = true;
    int testNum = 3;
    for (int i=1;i<size;)
    {
        isPrime = true;
        for (int j = 2;j<testNum;j++)
        {
            if (testNum%j==0){
                isPrime = false;
                break;
            }

        }
        if (isPrime)
        {
            *array = testNum;
            array++;
            i++;
        }
        testNum++;
    }
}
    int _tmain(int argc, _TCHAR* argv[])
    {
        int t[5]={0,};
        int sample[5]={2,3,5,7,11};
        prime(t,5);
        for (int i=0;i<5;i++){
            assert(t[i]==sample[i]);
        }
        return 0;
    }
like image 200
LNK2019 Avatar asked Mar 17 '26 12:03

LNK2019


1 Answers

lots of practice :)

although i dont think a few minor syntax errors will bother an interviewer too much - as long as your theory is sound.

like image 92
SpaghettiMonster Avatar answered Mar 20 '26 01:03

SpaghettiMonster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!