ways to keep mind clear and focused while coding?

Hi,
today I spent an embarrassing amount of time (2hrs) mainly in trying to get this code to work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
string getExtension(string path)
{
    int i=path.length();
    for(; path[i]!='\\'; i--)
    {
        if(path[i]=='.')
        {
            // we have reached the '.', meaning that from here to
            // the null terminator is the extension.
            string ext;
            while(path[i])
            {
                ext+=path[i++];
            }
            return ext;
        }
    }
    return ("");
}

string getExtension(char* path)
{
    string pth=path;
    return getExtension(pth);
}

string allowedFiles[8]={".c", ".cpp", ".cc", ".h", ".hpp", ".hh", ".asm", ".inc"};

void processContents(directory d)
{
    /*** processes d.contents to remove all files which
    are not of type .c, .cpp, .cc, .h, .hpp, .hh, .asm, .inc , etc
    ***/
    srcparse parser;
    bool deleteFile=FALSE;
    if(!d.listcontents())
    {
        cout << "error : d.listcontents in processContents()" << endl;
        return;
    }
    for(list<file>::iterator it=d.contents.begin(); it!=d.contents.end(); ++it)
    {
        /* here we run thru all of them and check if they are of type ? in the
        table. if not, we delete them */
        for(int i=0; i<8; i++)
        {
            if(!strcmpi(getExtension(it->getpath()).c_str(), allowedFiles[i].c_str()))
            {
                // the file matches one of the ones allowed.
                deleteFile=false;
                break;
            }
            if(strcmpi(getExtension(it->getpath()).c_str(), allowedFiles[i].c_str()))
                deleteFile=true;
        }
        if(deleteFile)
        {
            cout << "removing file from list :" << it->getpath() << endl;
            d.contents.remove(*it);
            it=d.contents.begin();
        }
    }
    cout << "file list:" << endl;
    for(list<file>::iterator it=d.contents.begin();it!=d.contents.end(); ++it)
        cout << it->getpath() << endl;
    srcparse::stats stats{0, 0, 0};
    srcparse::stats tmp{0, 0, 0};
    for(list<file>::iterator it=d.contents.begin();it!=d.contents.end(); ++it)
    {
        tmp=parser.parse((char*)it->load());
        stats.chartotal+=tmp.chartotal;
        stats.linetotal+=tmp.linetotal;
        stats.linecode+=tmp.linecode;
    }
    cout << stats.linecode;

}


and by the first hour I was really unfocused and started making stupid mistakes such as extra curly brace, semicolon after if(..), and stuff like that.
So I'm making this thread to ask for some ideas to keep my mind focused and on the job even over long periods of time without a break...

as is visible the above code was pretty simple but the problem was confusing and I started doing stupid things.

so I'm just asking for ways to keep focused and minimize errors that I make while coding?

Thanks
oh and also, I wanted to ask, how often should I expect my own code to compile on a first try? (code like that above) ... because for me, it invariably never does :P
> So I'm making this thread to ask for some ideas to keep my mind focused and on the job even over long periods of time without a break...

Take a break when your mind becomes unfocused. If needed, have a good night's sleep, and take a fresh look at it the next day.


> how often should I expect my own code to compile on a first try?

Write a few lines which are compilable, compile them, fix errors if any, test if possible, write a few more lines ... And you won't have too many problems.
Last edited on
As JLBorges said a good night's sleep is really important.
Think before code, unless you will get confused.
If the errors are endless and the code getting confusing you can try to code evrything again
how often should I expect my own code to compile on a first try?

When you start out, you'll probably find yourself struggling with both syntax and logic.

A good IDE such as VS2012 will help by highlighting syntax errors as you're typing.

As you become more experienced, the syntax will become second nature and you will find youself focusing more on the structure and logic of your program.
Well I if your gettin distracted with the stuff around u...I would put them some where else and in desktop if you have stuff that distract you like games,Skype ..I would log out of Skype and delete my games if I care more about programming. In fact I deleted my mw3 4 days ago becuz of that...
Um, try to eat before you start programming and eat while you're at it?

I just listen to music and...yeah...
Topic archived. No new replies allowed.