x404.co.uk
http://www.x404.co.uk/forum/

Things that every programmer should know
http://www.x404.co.uk/forum/viewtopic.php?f=4&t=10621
Page 1 of 1

Author:  Fogmeister [ Thu Sep 16, 2010 9:54 pm ]
Post subject:  Things that every programmer should know

Regardless of operating system, language, team size, project size, etc... there are some things that every programmer should know.

What are your little pieces of knowledge that you have picked up or come to realise that help you when programming.

For a start, I learnt very quickly to take constructive criticism of my code and use it to help better my code in the future. I never take criticism personally now or try to be too protective of my code as I know that it's perfectly possible that there are several better/alternative solutions.

Author:  forquare1 [ Fri Sep 17, 2010 12:40 am ]
Post subject:  Re: Things that every programmer should know

A good idea of what is needed and how it's put together is always great, if you don't have that luxury then a process I call 'iterative prototyping' can be just as good though takes longer - Program everything you can by hacking stuff together, when you have something that works how you want it to, reprogram it with good practises in mind.

If you ever find yourself thinking "this would be much easier in X language" then you probably haven't learnt the language you are using well enough or are using the wrong language for the job.

If you put your mind to it and don't mind it being slow, particular, and rather long winded, you can do almost anything in Bash!

Author:  jonlumb [ Fri Sep 17, 2010 7:13 am ]
Post subject:  Re: Things that every programmer should know

I'm not much of a programmer tbh, but something I've seen time and again on DWTF can be rectified by "check there isn't already a function that does this"

Author:  big_D [ Fri Sep 17, 2010 8:30 am ]
Post subject:  Re: Things that every programmer should know

1) always write your test scripts, before you've written a line of code

2) always write the function/method header comments before you start writing the code.

3) make sure that the comments are updated to reflect changes in the design of the system, before you implement the changes

4) for compiled languages, always comment out redundant code, don't just delete it, you never know ;)

5) use a code management system, like SourceSafe, SVN or CVS (see 4 above).

Author:  finlay666 [ Fri Sep 17, 2010 2:09 pm ]
Post subject:  Re: Things that every programmer should know

Things the junior at work can't do but every dev should:

Know how to debug code
Know how to respect a tester, for they are the ones that WILL break your code
Know how to view a stack trace
Know how to write out in some form of logging
Know how to make a meaningful error message
Know that it takes someone else to test & validate code/bug fixes to the person that wrote it
Write code comments (the "It's a small business, I can just ask" doesn't work when people leave)
Generate a functional spec, especially for things that are used multiple times (especially when there are consistent failings because they don't get fixed)
Then on failure to make a functional spec to not whine when bugs are found because "The customer didn't ask for it" (I doubt the customer should have to ask to not get null reference exceptions on deleting items)

If you use it more than 3 times and needs a comment to describe how it works, write a method for it

Author:  big_D [ Fri Sep 17, 2010 2:30 pm ]
Post subject:  Re: Things that every programmer should know

ALWAYS test the positive result in a decision (if...then, while, repeat etc.) rather than the negative result, where possible, as it is faster and more efficient.

E.g.

Code:
public void example(int a)
{
    if(a == 1) {
        do something
    }
    else {
        do something else
    }
}


is more efficient than:

Code:
public void example(int a)
{
    if(a != 1) {
        do something
    }
    else {
        do something else
    }
}

Author:  forquare1 [ Fri Sep 17, 2010 2:43 pm ]
Post subject:  Re: Things that every programmer should know

big_D wrote:
ALWAYS test the positive result in a decision (if...then, while, repeat etc.) rather than the negative result, where possible, as it is faster and more efficient.


Putting the negative first can make your code more readable in some situations. I know one shouldn't say it, but with todays computers, with small knock on performance is probably worth it for readability later.

Author:  Fogmeister [ Fri Sep 17, 2010 2:58 pm ]
Post subject:  Re: Things that every programmer should know

big_D wrote:
ALWAYS test the positive result in a decision (if...then, while, repeat etc.) rather than the negative result, where possible, as it is faster and more efficient.

E.g.

Code:
public void example(int a)
{
    if(a == 1) {
        do something
    }
    else {
        do something else
    }
}


is more efficient than:

Code:
public void example(int a)
{
    if(a != 1) {
        do something
    }
    else {
        do something else
    }
}

I did a test of this at work the other day.

It took about 30% longer for the computer to check if a boolean was negative than it did to check if it was positive.

I couldn't believe it!

Author:  forquare1 [ Fri Sep 17, 2010 6:51 pm ]
Post subject:  Re: Things that every programmer should know

Fogmeister wrote:
I did a test of this at work the other day.

It took about 30% longer for the computer to check if a boolean was negative than it did to check if it was positive.

I couldn't believe it!


I've tried it in Java and Perl and it's not that much in it:
Java:
Code:
class MyTest{
   
   public static void main(String[] args){
      int a = 96;
      
      if(a == 96){
         System.out.println("Finished");
      }else{
         System.out.println("Finished");
      }
   }
}

Time:
Code:
$ time java MyTest
Finished

real   0m0.394s
user   0m0.263s
sys   0m0.074s


Code:
class MyTest{
   
   public static void main(String[] args){
      int a = 96;
      
      if(a != 96){
         System.out.println("Finished);
      }else{
         System.out.println("Finished");
      }
   }
}

Time:
Code:
$ time java MyTest
Finished

real   0m0.362s
user   0m0.262s
sys   0m0.070s
- Yes, I did recompile ;)

Perl:
Code:
my $a = 96;

if($a == 96){
   print("Finished");
}else{
   print("Finished");
}

Time:
Code:
$ time perl mytest.pl
Finished
real   0m0.017s
user   0m0.005s
sys   0m0.007s

Code:
my $a = 96;

if($a != 96){
   print("Finished");
}else{
   print("Finished");
}

Time:
Code:
$ time perl mytest.pl
Finished
real   0m0.014s
user   0m0.005s
sys   0m0.008s

Author:  Fogmeister [ Fri Sep 17, 2010 7:47 pm ]
Post subject:  Re: Things that every programmer should know

Ah, I only skim read Dave's post.

My test was checking an actual boolean.

In pseudo code it was...

assign a boolean value.
if boolean then print "Here".
else print "Here".

this was compared to...

assign a boolean value.
if not boolean then print "here".
else print "here".

if that makes sense.

I also looped it about 10,000 times so I could get a sensible answer out of it as our system was too quick for anything up to 100 iterations and gave a time of 0.

Author:  big_D [ Sat Sep 18, 2010 7:17 am ]
Post subject:  Re: Things that every programmer should know

Processors are designed to test for true, testing for false requires more processing power.

With a modern processor, with a single user, it doesn't make much difference. Add in thousands of tests in a script, multiplied by hundreds of users on the same processor (e-shopping sites, for instance), it can make a big difference.

We had an e-shop with 4 load balanced servers, when it got more than 250 customers, it would grind to a halt - part of it was also poorly optimised SQL queries as well, changing those, and optimising the code meant that it was running fine with 1000 users afterwards. A lot of the SQL optimisation was also changing WHERE statements to check for positives, instead of negatives, plus re-ordering the joins.

Author:  finlay666 [ Sun Sep 19, 2010 8:19 pm ]
Post subject:  Re: Things that every programmer should know

big_D wrote:
We had an e-shop with 4 load balanced servers, when it got more than 250 customers, it would grind to a halt - part of it was also poorly optimised SQL queries as well, changing those, and optimising the code meant that it was running fine with 1000 users afterwards. A lot of the SQL optimisation was also changing WHERE statements to check for positives, instead of negatives, plus re-ordering the joins.


Nothing to stop a

if (something)
{
// do nothing
}
else
{
// do stuff
}

Author:  big_D [ Mon Sep 20, 2010 7:09 am ]
Post subject:  Re: Things that every programmer should know

Totally.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/