3/31/09

How to program well (part 1 of n)

Be sure to clearly define "programming interface" of your function.

Writing a function is a simple part of programming in any language yet it is an essential part of programming and having good functions are very important to produce high quality software. I've seen too many pooly written functions and decided to document what not to do and what to do.

When I say, pooly written function, these are functions with no clear "interface contract". Here, I am not talking about what is inside of the function (ie. implementation). I actually don't care about implementation (well, it should work, but it is really up to programmer to implement the function however he/she sees appropriate). It is more about "interface".

There are several important rules about writing good functions

Function name should have a single action word
A function should do one thing, or a unit of things that is cohernt
Etc..

However, the issue I see most offten with pooly written functions are lack of clear documentation about its arguments.

Function arguments should have acceptable values (or domain)

For instance, if function Foo takes an integer argument, it must clearly state whether 0 is acceptable, negative value is acceptable, whether it has upper/lower limits, etc. Usually function should accompany documetation that describe these details. Amazingly, many professional progammers just don't do this...

Next, function should "enforce" these domains by using Assertions and throwing Exceptoins. I would recommend setting up both, but sometimes throwing exceptions is sufficient, but not doing either is bad. Without this enforcement, problem would occur down the code stream and it is probably more time consuming to toubleshoot. With enforcement of input values at the function entry, it is much easier to understand what went wrong.... client code deviated from "interface contract" (ie. was never supposed to pass negative value, but it did).

(I will update this entry)

30 comments:

Anonymous said...

piglets http://sftc.communityserver.com/members/Electric-Blankets/default.aspx garages http://www.webjam.com/furnacefilters flavoursome http://www.asianave.com/vendingmachines/ hopes http://www.webjam.com/kitchencabinets pulpit http://www.webjam.com/slipcovers duvedec

Penis Enlargement Pills said...

I really agree with the facts that you have shared on this post. An interesting topic like this really enhances reader's mind to have more effective decisions over a certain issue.

online generic viagra said...

Hi,
This is an awesome blog post.... Really its great to be visiting your blog post. Keep posting... Thanks!

Vitoliv liver detox pills said...

You are a really intelligent person I can say, as Programming is not the easiest of jobs to do.

Herbal penis enlargement pills said...

I am really amazed to see that you have done a lot of work on this sector. These sort of thing is tough

Natural stress relief pills said...

I think its not about what is inside of the function...

Orgasm enhancement pills said...

Function arguments should have acceptable values (or domain)

Vimax penis enlargement pills said...

I have very little knowledge about programming because it is something which is hard to catch and understand.

Premature ejaculation pills review said...

With enforcement of input values at the function entry, it is much easier to understand what went wrong...

Best acne treatment for sensitive skin said...

Thanks alot for this very helpful post. Both parts are well explained and I found them very informative. Hats off

Prosolution erectile dysfunction gel said...

i have zero knowledge about programming because I am a business student.

Hersolution female stimulant gel said...

It is very effective method. programming is my favorite.I love do coding.thanks for sharing it

hcg buy online said...

An interesting topic like this really enhances reader's mind, HCG Blue Drops, the Faster and Natural way to lose weight

NeosizeXL increase penis size pills said...

Very useful post.Where is next part .Give the link i can't find it.

Best penis enlargement supplement said...

Without this enforcement, problem would occur down the code stream and it is probably more time consuming to toubleshoot.

Duramale anti premature ejaculation pills said...

Nice . I will be thinking about programming . I am figuring the next part .

NeosizeXL increase penis girth pills said...

There are several important rules about writing good functions...

Weak erection pills said...

This way of programming is very effective in a way.This is really a great process indeed.

Duramale delayed ejaculation pills said...

Function name should have a single action word. Basically a function should do one thing, or a unit of things that is coherent.

19PFL3505D said...

Hello! are you a programmer? :D

Un26d4003 review said...

i like to programming in C and C++

Natural penis growth pills said...

Name should have a single action word.

Vimax penis enlargement pills said...

This is a very good post. This will be very helpful for programmers.

Duramale stop quick ejaculation supplement said...

This is an awesome blog post.... Really its great to be visiting your blog post. Keep posting... Thanks!

Infrared grills review said...

Thanks a lot Yoshi for this article. You are really a life savior and hope that your future posts will remain the same.

All Top models 2012 said...

Its really nice that there are still people that write about this stuff. Many thanks to you and wish you all the luck sir.

Anonymous said...

private placement memo ebook shareware trial http://audiobookscollection.co.uk/de/Addressing-the-Economics-of-Waste/p226187/ ebook nature [url=http://audiobookscollection.co.uk/PCs/c2200/?page=5]anatomy trains ebook[/url] six thinking hats ebook

Anonymous said...

[url=http://onlinemedistore.com/products/gift-kamagra-jelly.htm][img]http://onlinemedistore.com/6.jpg[/img][/url]
stickers kids pharmacy http://onlinemedistore.com/products/fosamax.htm rite aid pharmacy maryland [url=http://onlinemedistore.com/products/relafen.htm]neonatal care pharmacy presentations[/url]
prescription prices from wal mart pharmacy http://onlinemedistore.com/products/sumycin.htm phentermine usa pharmacy [url=http://onlinemedistore.com/products/sinemet.htm]sinemet[/url]
grove pharmacy montclair new jersey http://onlinemedistore.com/products/prazosin.htm tower online pharmacy [url=http://onlinemedistore.com/products/lynoral.htm]pharmacy techinician online course[/url]
canadian pharmacy use in texas http://onlinemedistore.com/products/bactrim.htm canada pharmacy celexa [url=http://onlinemedistore.com/products/maxalt.htm]maxalt[/url]

Anonymous said...

premium dating [url=http://freeinternetdating.info/relationships/types-of-human-relationships]types of human relationships[/url] amish dating websites
chinese average dating age http://freeinternetdating.info/romance/free-online-romance-ebooks moroccan american advice dating
dating m1 garand [url=http://freeinternetdating.info/matchmaker/disneys-mulan-matchmaker]best friends dating[/url] first nations dating site turtle island

Anonymous said...

john kelvin dating profile http://loveepicentre.com/ gay dating guidlines
danity kanes dawn dating q [url=http://loveepicentre.com/contact/]minor dating laws indiana[/url] local coed dating
maryland white men dating black women [url=http://loveepicentre.com/]dating is he into me[/url] chubby chick dating [url=http://loveepicentre.com/user/smith/]smith[/url] average guy dating beautiful girl