LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-28-2011, 09:20 PM   #16
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406

a couple points i can give you, and have learned the hard way

functions in any language should perform ONE task and ONE task only, even if the function ends up only being 2, 3 or 4 lines long, the more you separate each task into functions the more re-usable they are. if the functionality of the function can't be described in one line it's probably doing too much

second, avoid hard-coding values for computations in functions, unless the value is a constant value that never changes, instead pass the values into the functions as arguments, this way the function can be re-used with a different data set, if the value changes, but infrequently, design a config file so you can change the values without re-compiling (if using a compiled language) or having to dig too far into the code if using an interpreted language

third, give variables and functions meaningful names, yes you can use X,Y,Z,variable1,variabl2 etc.. but remember, you might not be the next person to look at the code, not to mention the more complex the code gets the harder it becomes to keep track of variable names.

forth, optional, but if your task is really, really big, then segment the program into logically arranged files.

Last edited by frieza; 06-29-2011 at 12:23 PM.
 
1 members found this post helpful.
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 06-30-2011, 03:34 PM   #17
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by ta0kira View Post
Although I haven't done much collaborative programming, the advice I remember, probably from this board, was that your code should be clear enough to essentially explain itself and that comments should not be used to explain what the code should.
In a "real" programming shop, one starts with a detailed program specification. Thus the "explanation" is already written. The coder's task is to implement the specification, and the comments are, usually, references to or extracts from the specification document.
Quote:
Also, did your team members maintain the code with all of these comments, or did the comments go in toward the end?
Yes, the comment were maintained by the coders.
Quote:
I used to try to hold myself to a standard such as yours, but I found it slowed my progress down significantly. Some of that was trying to keep the comment blocks unobtrusive (e.g. indenting and wrapping) so I could still visually parse the control structures. Of course, I generally program alone.
As I've gotten older, I've found it really helps to have good comments in old code. Even code I've written solely for myself. (I don't think my coding abilities are fading, but my wife sometimes finds me "forgetful.)
Quote:
Anyway, I'm not challenging your experience; I'm just wondering what your take is on this conflicting advice. Also, what do you think of the idea of augmenting the code with e.g. an HTML or PDF document explaining its structure in a way that can't be conveyed in plain text? Thanks!
Kevin Barry
Well, if you start with a good specification, "augmentation" is somewhat redundant. On the other hand, note my comment about a program to extract the comments as program documentation. I think (but haven't actually looked) that the Linux kernel documentation is generated in that fashion.

I don't think maintaining the comments needs to be difficult if you keep your functions "single-purpose."
 
1 members found this post helpful.
Old 06-30-2011, 04:25 PM   #18
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
Quote:
I used to try to hold myself to a standard such as yours, but I found it slowed my progress down significantly. Some of that was trying to keep the comment blocks unobtrusive (e.g. indenting and wrapping) so I could still visually parse the control structures. Of course, I generally program alone.
that depends on just how detailed you want your comments to be, some functions are rather self explanatory and need only a simple description of what the function does, what the arguments are supposed to be, and what the return value is, other times you might have to comment individual blocks or lines of code within functions to remind me what that particular line/block does within a larger function

yeah commenting can be a bit of a hassle, but coming back even after a hiatus of even a few weeks code you wrote can look foreign to you and commenting will save the hassle of having to read the code to figure out what's going on.
 
Old 07-01-2011, 11:21 AM   #19
texasone
Member
 
Registered: Jun 2008
Location: /home/lorax
Distribution: Debian Testing
Posts: 141

Original Poster
Rep: Reputation: Disabled
Good pointers guys. I'm really learning a lot here, thanks.
Question though. How much usage of function/subs do you use. Depending on who I was talking to, people would run their program differently. Some would have all the control of it in the "main" (not to be confused with C main) program and have all the tasks done in subs/func while others would only use the "main" program to call a func/sub that would run the program. Does anyone have any comments on good practices with that?
Thanks
RzITex
 
Old 07-01-2011, 03:56 PM   #20
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Too many comments often mean poorly written code.

E.g. unclear variable names.

The goal is to make code self-explanatory. The exceptions are, for example, work-arounds for bugs, some empirical formulas/coefficients, name of the algorithm used, especially, if it's not widely known algorithm.

Also, comments explaining how this piece of SW interacts with other pieces/processes and the decisions made to ensure the correct interaction. E.g. comments on all kinds of timeouts, number of stop-bits (yes, one might need 1.5 stop-bits).
 
Old 07-01-2011, 05:05 PM   #21
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
Quote:
Originally Posted by texasone View Post
Good pointers guys. I'm really learning a lot here, thanks.
Question though. How much usage of function/subs do you use. Depending on who I was talking to, people would run their program differently. Some would have all the control of it in the "main" (not to be confused with C main) program and have all the tasks done in subs/func while others would only use the "main" program to call a func/sub that would run the program. Does anyone have any comments on good practices with that?
Thanks
RzITex
imho you would use the main function to do initial tasks and tie the other functions togeather, having an intermediate function seems extranious, but i'm sure someone will post a reply as to why it isn't
 
Old 07-02-2011, 04:34 AM   #22
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
+1 to Frieza's comment, mind you I have found that a few python folk use __main__ as a redirector - it doesn't seem to do a great deal.
 
Old 07-05-2011, 06:52 PM   #23
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by frieza View Post
imho you would use the main function to do initial tasks and tie the other functions togeather, having an intermediate function seems extranious, but i'm sure someone will post a reply as to why it isn't
I try to write C/C++ programs to be front-end independent (e.g. as an .so), then write a command-line implementation that might easily be replaced by a GUI front-end without having to restructure the parts that do the actual work.
Kevin Barry
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Linux-compatible Snail-Mail Program AdoptAPet Linux - Desktop 14 03-14-2011 07:19 PM
why not install g++ and compatible program on production yuanjunliang Linux - Security 2 12-15-2009 09:00 PM
Good linux chinese language language program? darsunt Linux - Software 1 04-10-2009 12:06 PM
best language to program 3d games swatward Programming 2 02-28-2005 03:19 PM
Program Language Sifvion Programming 7 06-06-2002 01:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration