Principles Of Great Coding

Welcome. Please Login, Register, Or Activate! 
type your username and password to login
Date: November 23, 2009, 11:04 PM
431550 members and 298568 Topics
Latest Member: tymlees_ay
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Programming  |  Principles Of Great Coding
Pages: (1) (2) (3) Go Down Send this topic Notify of replies
Author Topic: Principles Of Great Coding  (Read 3214 views)
oludimuni
Principles Of Great Coding
« on: February 02, 2009, 02:27 PM »

PRINCIPLES OF GREAT CODING

In this thread, we’re looking at some more principles that can be very helpful when you’re writing computer code.  These are themes that underpin everything you do as a programmer and can make a huge difference to the quality of code you produce and the time it takes you do

so!

Don’t Repeat Yourself (DRY)
The concept here is that anything you use in your code should have a single, unambiguous representation.  This means if you are storing a number, only store it in a single place (don’t have PI stored in three different places).  Not only is multiple storage wasteful, but it multiplies your effort if you want to change, especially if you have to go hunting through several files to find where the numbers are defined.  Multiple representations are also a great way to generate bugs if you forget to change some of them.  This also applies to code; don’t repeat chunks of code that do the same thing - have a single version and put it in a function.
javarules (m)
Re: Principles Of Great Coding
« #1 on: February 02, 2009, 05:48 PM »

TEST TEST and TEST
 
Tests have saved my a55 on more than several occassions
*dhtml
Re: Principles Of Great Coding
« #2 on: February 03, 2009, 07:46 AM »

Interesting
yawa-ti-de (f)
Re: Principles Of Great Coding
« #3 on: February 03, 2009, 11:21 AM »

You might as well add the following to the list:

1) Use abstraction
2) If you use the same code more than once, better make it a function
3) Indentation
4) Not just testing but incremental (unit, integration and system) testing
5) Comment your code
6) Use sensible variable names
7) Write your code as though you were immediately handing it over to someone else who will have to decipher it.
ade2kay (m)
Re: Principles Of Great Coding
« #4 on: February 04, 2009, 09:26 AM »

@yawa-ti-de -- Nice,  I love you points.

Guys, i have this habit of writing complex code,  I mean if something should be done in 10 lines, i try to write it in just 2 or 3 lines, using complex constructs but with great indentation and insightful variable names. Is this really a bad code habit ?

Some enlightement pls
logica
Re: Principles Of Great Coding
« #5 on: February 04, 2009, 12:04 PM »

i really think comments are a bit outdated as the use of sensible variable names can now be used mostly. we no longer have those languages that limit the length of a name to a few characters. and if u r a beginner in the field, u have no business reading /handling complex code, therefore u wouldn't expect me 2 explain using comments, basic ideas like looping or setting variables.
yawa-ti-de (f)
Re: Principles Of Great Coding
« #6 on: February 04, 2009, 01:09 PM »

ade2kay,

If as you have said it is complex, then you might as well write it in such a way that if you drop dead today, the guy to take over can understand it.  Put it this way, if I wrote such code and left the company and you took over, would you like it? Therein lies your answer.  Gone are the days of C-coding in the 70s when memory was of the essence.  If you doubt me, try timing your MS Vista boot up.

logica,
I will beg to differ with you on this one.  I wouldn't necessary call $cookieToHoldLoginDetails a sensible variable name, as self-explanatory as it may sound.  I am of the opinion that comments have their place, granted they must be sensible.  For example, I like to write this comment in particular. "//connecting to database, ".   Sure, if you see the code, it is self-explanatory, but to me, it doesn't hurt to know where the actual connection starts.  I like to see it this way: MS powerpoint has a lot of bells and whistles.  Having said that, you shouldn't make your entire presentation one that distracts from the message.
webdezzi (m)
Re: Principles Of Great Coding
« #7 on: February 04, 2009, 01:24 PM »

yea, ade2kay, u remind me of   !$x ? $y=1: $y=0;
that little thing in php will take newbies 5 lines (sorry 4 d little igberaga)

@topic, yea there is this popular inefficient code u must av used without even knowing it
(Disclaimer: i am not in anyway referring to any one or organization, This is for learning purposes only)


$var1 = getTransResponseAmount(getStatus("xxx","xxxxxxxxxx",$x));
$var2 = getTransResponseCode(getStatus("xxx","xxxxxxxxxx",$x));

the big inneficiency comes where the getstatus function needs to connect to a server elsewhere

if it takes $var1 30 sec to get result, 30 sec to get result for $var2, that make 1 minute

u cud as well have result from getstatus() saved in a variable first, then run whatever function you want on the result.

@yawa, yes, sensible variable names,  but sometimes, u dont want ppl to understand your codes for some reasons.
i like using dreamweaver search n replace to replace the variables.

like replacing $Username with $htuiXbhbfn

OMG! you will wish u can find me and kill me for this.   Grin Grin
ismailaewe (m)
Re: Principles Of Great Coding
« #8 on: February 04, 2009, 02:24 PM »

As for me i always like to put comments to make my code easy to read and understand.
dammytosh
Re: Principles Of Great Coding
« #9 on: February 04, 2009, 02:53 PM »

Please add this to the principle.
  Try to ensure that your method or function performs a single task. U know if your method or function is performing too many tasks if you have problems naming it.   
     it makes the code cleaner and easier to debug.

@logica
You are right i once heard Bob Martins (Uncle Bob) saying that on a programming show and i guess it makes sense to me too. Good variable names can save u several lines of comments.



yawa-ti-de (f)
Re: Principles Of Great Coding
« #10 on: February 04, 2009, 03:37 PM »

Yes webdezzi, I will hunt you down and kill not only your body but your soul.  Job security has been replaced by readability and maintainability  Cool

Another example of good coding:  Not using include files unnecessarily.  I don't know about you guys, but I have worked in places where, just when you managed to find one file, you open it, only to find out that it in itself contains only one line of code - an include to another file adn that file includes another file and so on and so forth.  I have seen such that has gone 6 levels deep  Shocked Shocked Shocked

If you must, just include something once.
ztyle (m)
Re: Principles Of Great Coding
« #11 on: February 04, 2009, 04:30 PM »

 ;d
webdezzi (m)
Re: Principles Of Great Coding
« #12 on: February 04, 2009, 04:34 PM »

ztyle, why na! why u post this ugly girlfriend of yours? Grin Grin
webdezzi (m)
Re: Principles Of Great Coding
« #13 on: February 04, 2009, 04:46 PM »

@yawa, not really job security, it is code security.
the same reason we have obfuscation software like zend encoder/guard
This one makes the hacker feel he can do it only to give up after several days

 about Includes of includes.
u just cant live without it, same reason u have functions being passed across to functions as parameters.

Its strenuous wen u av to go thru someone else's though

Albato (m)
Re: Principles Of Great Coding
« #14 on: February 04, 2009, 04:56 PM »

Open source is the way to go.
Never re-invent the wheel.
Use what is already there - then improve on it.

Its probably the lazy man's method but wait, every one using an open source cms is doing that. Joomla, Drupal, Wordpress etc - even SMF

Now you get it Smiley
Emmacomng
Re: Principles Of Great Coding
« #15 on: February 04, 2009, 05:17 PM »

Keep It Simple, Stupid!
The simpler your code is, the easier it is to construct and maintain.  So, subject to the constraints of our objectives, the simpler you can make your code the better.  This has a connection to premature optimisation (see this post ), because optimised code tends to be less simple.  We think a reasonable rule-of-thumb is that unless most simple way of doing something will be obviously too inefficient, it’s worth trying.  you can always change it later and, because it’s simple code, this shouldn’t be too hard.  One way to describe this is to paraphrase Albert Einstein:  Code should be as simple as possible, but no simpler.
webdezzi (m)
Re: Principles Of Great Coding
« #16 on: February 04, 2009, 05:38 PM »

Mr Simple,
Note that this applies to every endeavour, what you know is simple, what u dont, it hard.

@albato, Well i love open source and i always encourage ppl to try it, though for some reasons, i hardly use it in my works. I love it

same applies to our hardcore notepad coders.
writing html in notepad for a commercial project to me is plain madness, well to others it's "i know html so well" (i expect some nailing)

It looks to me like trying to break in thru the wall when the door is opened.
jacob05 (m)
Re: Principles Of Great Coding
« #17 on: February 04, 2009, 06:16 PM »

Grin
@webdezzi
nice point "Why breaking through the wall when the door is already opened ?" i will add that to my diary.
Albato (m)
Re: Principles Of Great Coding
« #18 on: February 04, 2009, 07:38 PM »

I support open source because it broke Microsoft's (and other "hidden source code" companies) monopoly of the internet, and applications software market. Today anyone can become a programming guru just by studying already made codes. Anyone can set up a website and tweak or redesign it anyway they want.

My heroes are the open source radicals who first rebelled in creating Linux operating system. They revolutionised the world. Like the previous person said,
Quote from: jacob05 on February 04, 2009, 06:16 PM
"Why breaking through the wall when the door is already opened ?"
*dhtml
Re: Principles Of Great Coding
« #19 on: February 04, 2009, 10:37 PM »

This is programming war zone mehn - lemme just dey observe - keywords: job security/code security/obfuscated codes/short variable names/bad coding practice/open source/close source/lazy man/reinvent wheel/hard coding/soft coding - maybe i can even publish a book from this thread sef.

*dhtml
Re: Principles Of Great Coding
« #20 on: February 05, 2009, 03:20 AM »

At least me too i dey for this film - even if i no talk beta.
dammytosh
Re: Principles Of Great Coding
« #21 on: February 05, 2009, 09:55 AM »

Another principle is to automate your routine code where possible. by automating some routine code, u dnt av to test them again cos it will be correct for sure. 

 "Time spent on infrastructure programming is a complete waste of time"
                                                 Juval Lowly (DotNet Guru Extra Ordinaire.)

Albato (m)
Re: Principles Of Great Coding
« #22 on: February 05, 2009, 10:48 AM »

Familiarise yourself with all the open source code repositories on the web. You can start with:

http://www.hotscripts.com/

http://www.dynamicdrive.com/

These 2 sites are every programmer's dream. Talk of codes in php, c++, java. They have already made applications like social networking site scripts etc. All free and open source.

Like I have been saying, I believe in open source and re-use of already made codes. I believe in "no string attached" sharing of web resources and technologies. In that way, everyone will be able to join the computer revolution.
ujem (m)
Re: Principles Of Great Coding
« #23 on: February 05, 2009, 12:20 PM »

Always have comments in your code especially when scripting this allows for easier understanding by a third party,
yawa-ti-de (f)
Re: Principles Of Great Coding
« #24 on: February 05, 2009, 01:18 PM »

webdezzi,

I could be wrong but such code is obfuscated per se, to keep it compact.  Keeping it compact helps reduce file size.  In a browser, this will result in quicker download times.   In other words, the original source is readable.  Again, I could be wrong but I go by personal/my professional experience.  If this isn't the case with Zend or whom/whatever, I still insist it is wrong.  As one poster has rightly put it, you do unto others as you want others to do unto you.  If you don't want to take over code that is crap, then you yourself shouldn't write crap.  Period!

with respect to includes, includes are good, don't get me wrong.  What is wrong is includes that are unnecessarily deep.  In other words, it takes me (and I say this again, based on experience), opening 6 different files just to make one tiny change and if you do an independent audit of the code, there was no legit reason to have that many includes.  It stinks to high heavens of an immature coder.
webdezzi (m)
Re: Principles Of Great Coding
« #25 on: February 05, 2009, 02:15 PM »

@yawa, like i said earlier, there are many reason u myt want 2 obfuscate your codes, maybe u run an sms website for instance.
you myt want to use such softwares.

Not really compact, some help rebuild the logic so it's more efficient as in the case of zend guard

i have some 3rd party scripts here that will definitely pass for unnecessary includes u talk about
but those includes make sense to me. i dont think a developer in his right sense will want to use it unnecesssarily, well they have their reasons and u have urs.


@albato
I am a big fan of Hotscripts.
nitation (m)
Re: Principles Of Great Coding
« #26 on: February 07, 2009, 06:58 AM »

Hi folks,

From my view as a developer, I believe we need to stress the fact that most programmers in Nigeria do not consider security as the afore most requirement during their development stages.

Let us give a break to indentation, commenting, functionalities , etcetera.  The question we need to ask is: How secured is my latest application? What are the chances that the end users are protected? How long does it take for my server/network to be exploited? - How vulnerable is my program?Huh?

Folks, am a security professional and am certain about how unsecured most in house programs are in Nigeria. I don't wanna start pointing fingers, but i will give you an instance.

Take a calm look at the attached image!

[ Proof of concept ]

My 2cent


* tribune_malware.jpg (78.75 KB, 1280x800 )
yawa-ti-de (f)
Re: Principles Of Great Coding
« #27 on: February 07, 2009, 12:59 PM »

Nitation,

I think that rather than give a break, security should be included.  At the end of the day, all we have discussed here go hand in hand, IMHO.
nitation (m)
Re: Principles Of Great Coding
« #28 on: February 08, 2009, 01:10 PM »

Yawa-ti-de,

I agree with you. I was only stressing the fact that no one is saying anything security related.

Kudos to you bro.

yawa-ti-de (f)
Re: Principles Of Great Coding
« #29 on: February 08, 2009, 01:20 PM »

nitation, you call me bro?  Huh Cool Shocked, he he.

All,

Here is a reason why comments, as old-school as it may sound to some, still makes sense:

I am working on a web app to launch soon.  I wrote the membership module about 2 weeks ago but while asking someone to do some testing, they uncovered an issue.  I had to go back (something I hate to do Grin) to fix it.  Call me slow but despite the good var names I had, indentation, abstraction, etc I couldn't for the life of me figure out why I wrote the code the way I did.  I had to start from scratch and in the process, add comments.  2 days ago, I had to make a slight modification so that the module would work on a few new pages I created and guess what? cos of the comments, making the necessary code changes was a breeze.

Again, call me slow, but comments do, and should, have its place as a principle of good coding.
candylips (m)
Re: Principles Of Great Coding
« #30 on: February 08, 2009, 02:14 PM »

Your post title sounds like one of those programming questions we used to answer in Uni
nitation (m)
Re: Principles Of Great Coding
« #31 on: February 08, 2009, 06:12 PM »

@yawa-ti-de

My bad! Wasn't calm enough to take a look at your gender. I hope you understand i meant no harm whatsoever ~{}

Regarding your post, I for one don't feel commenting should be regarded as an old-school concept. Why should we take the course of commenting out of our application? I still don't get it!

My 2~cent


 Managed Extensions for C++.   Vista Issues With Oracle 10g  What Software Have You Created?  Page 2
Pages: (1) (2) (3) Go Up Send Topic to Friend by E-mail Reply 


Sections: Autos/Cars (2) Jobs/Vacancies (2) (3) Career Talk Education General(2) Politics Romance Computers Phones Travel
Sports Fashion Health Religion Celebrities TV/Movies (2) Music/Radio (2) Books Webmasters Programming

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa. See also: Nairalist Classified Ads
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.