Reply to topic  [ 13 posts ] 
Things that every programmer should know 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Sep 16, 2010 9:54 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
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!


Fri Sep 17, 2010 12:40 am
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 6:44 pm
Posts: 4141
Location: Exeter
Reply with quote
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"

_________________
"The woman is a riddle inside a mystery wrapped in an enigma I've had sex with."


Fri Sep 17, 2010 7:13 am
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
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).

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Fri Sep 17, 2010 8:30 am
Profile ICQ
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
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

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Fri Sep 17, 2010 2:09 pm
Profile
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
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
    }
}

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Fri Sep 17, 2010 2:30 pm
Profile ICQ
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
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.


Fri Sep 17, 2010 2:43 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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!

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Fri Sep 17, 2010 2:58 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
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


Fri Sep 17, 2010 6:51 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Fri Sep 17, 2010 7:47 pm
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
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.

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Sat Sep 18, 2010 7:17 am
Profile ICQ
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
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
}

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Sun Sep 19, 2010 8:19 pm
Profile
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
Totally.

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Mon Sep 20, 2010 7:09 am
Profile ICQ
Display posts from previous:  Sort by  
Reply to topic   [ 13 posts ] 

Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.