Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,717 members, 7,816,951 topics. Date: Friday, 03 May 2024 at 09:00 PM

Principles Of Great Coding - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Principles Of Great Coding (7007 Views)

Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) / What Are The Differences Between Programming And Coding (2) (3) (4)

(1) (2) (3) (Reply) (Go Down)

Principles Of Great Coding by oludimuni: 2:27pm On Feb 02, 2009
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.
Re: Principles Of Great Coding by javarules(m): 5:48pm On Feb 02, 2009
TEST TEST and TEST

Tests have saved my a55 on more than several occassions
Re: Principles Of Great Coding by Nobody: 7:46am On Feb 03, 2009
Interesting
Re: Principles Of Great Coding by yawatide(f): 11:21am On Feb 03, 2009
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.
Re: Principles Of Great Coding by ade2kay(m): 9:26am On Feb 04, 2009
@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
Re: Principles Of Great Coding by logica(m): 12:04pm On Feb 04, 2009
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.
Re: Principles Of Great Coding by yawatide(f): 1:09pm On Feb 04, 2009
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.
Re: Principles Of Great Coding by Nobody: 1:24pm On Feb 04, 2009
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("Bleep","xxxxxxxxxx",$x));
$var2 = getTransResponseCode(getStatus("Bleep","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 ur 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
Re: Principles Of Great Coding by ismailaewe(m): 2:24pm On Feb 04, 2009
As for me i always like to put comments to make my code easy to read and understand.
Re: Principles Of Great Coding by dammytosh: 2:53pm On Feb 04, 2009
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.
Re: Principles Of Great Coding by yawatide(f): 3:37pm On Feb 04, 2009
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.
Re: Principles Of Great Coding by ztyle(m): 4:30pm On Feb 04, 2009
;d
Re: Principles Of Great Coding by Nobody: 4:34pm On Feb 04, 2009
ztyle, why na! why u post this ugly girlfriend of yours? grin grin
Re: Principles Of Great Coding by Nobody: 4:46pm On Feb 04, 2009
@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
Re: Principles Of Great Coding by Albato(m): 4:56pm On Feb 04, 2009
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
Re: Principles Of Great Coding by Emmacomng: 5:17pm On Feb 04, 2009
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.
Re: Principles Of Great Coding by Nobody: 5:38pm On Feb 04, 2009
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.
Re: Principles Of Great Coding by jacob05(m): 6:16pm On Feb 04, 2009
grin
@webdezzi
nice point "Why breaking through the wall when the door is already opened ?" i will add that to my diary.
Re: Principles Of Great Coding by Albato(m): 7:38pm On Feb 04, 2009
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,
jacob05:

"Why breaking through the wall when the door is already opened ?"
Re: Principles Of Great Coding by Nobody: 10:37pm On Feb 04, 2009
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.
Re: Principles Of Great Coding by Nobody: 3:20am On Feb 05, 2009
At least me too i dey for this film - even if i no talk beta.
Re: Principles Of Great Coding by dammytosh: 9:55am On Feb 05, 2009
Another principle is to automate ur 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.)
Re: Principles Of Great Coding by Albato(m): 10:48am On Feb 05, 2009
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.
Re: Principles Of Great Coding by ujem(m): 12:20pm On Feb 05, 2009
Always have comments in your code especially when scripting this allows for easier understanding by a third party,
Re: Principles Of Great Coding by yawatide(f): 1:18pm On Feb 05, 2009
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.
Re: Principles Of Great Coding by Nobody: 2:15pm On Feb 05, 2009
@yawa, like i said earlier, there are many reason u myt want 2 obfuscate ur 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.
Re: Principles Of Great Coding by nitation(m): 6:58am On Feb 07, 2009
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??

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

Re: Principles Of Great Coding by yawatide(f): 12:59pm On Feb 07, 2009
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.
Re: Principles Of Great Coding by nitation(m): 1:10pm On Feb 08, 2009
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.
Re: Principles Of Great Coding by yawatide(f): 1:20pm On Feb 08, 2009
nitation, you call me bro? 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.
Re: Principles Of Great Coding by candylips(m): 2:14pm On Feb 08, 2009
Your post title sounds like one of those programming questions we used to answer in Uni
Re: Principles Of Great Coding by nitation(m): 6:12pm On Feb 08, 2009
@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

(1) (2) (3) (Reply)

Why I Left Android App Development Since Last Year. / A Little Tip For Notepad++ users Like Me. / Let's Build A Simple Blog With Python(django)

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 52
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.