Login Register


Hey Programmers! Do you have a minute to share some wisdom ? filter_list
Author
Message
Hey Programmers! Do you have a minute to share some wisdom ? #1
I am wondering what rules you respect in order to get clean and great code ? what rules you have and respect for pre-develpment stage of a coding project, development stage and post development stage. Like no spaghetti code, use more than one programming language if it feets better your project... Can you please help me with this ? and just let the advices flows?

Thank you guys!

Reply

RE: Hey Programmers! Do you have a minute to share some wisdom ? #2
From what I've learned,

First Rule - Leave Comments

Good programmers always mention comments in their code in case if they want to review it after some time to understand what they did in that certain part.

Second Rule - Whitespace

Code:
#include <iostream> #include <cmath> int calculator (int x, int y) { return x + y; } int main () { std::cout << "The sum of 2 and 4 is" << calculator(2, 4) << std::endl;}

Whitespacing

Code:
#include <iostream> #include <cmath> int calculator (int x, int y) { return x + y; } int main () { std::cout << "The sum of 2 and 4 is" << calculator(2, 4) << std::endl; }

See the difference, the code is more clear to read.

Rule Three - Maintain Logic

Make sure your code have proper logic so that the person can understand what is being done by this code.

Rule Four - Always create an Algorithm

For me I found out that it was more useful to create an algorithm first rather than jumping straight into programming phase. What's the advantage? The advantage is that while writing an algorithm you might get new ideas to enhance your program further while being in programming phase what if you've completed half of your program and you suddenly realize that a certain part need editing and addition of new functions which can sometimes be painstaking and time consuming so it's better to get all the ideas, write an algorithm and then proceed to the programming phase.

Rule Five - Debug and Test

Test your program to the limit, try to eliminate as much bugs as you can ensuring a bug free code and program.
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: Hey Programmers! Do you have a minute to share some wisdom ? #3
Thank you Ex094!
That's great info !

I will add one that helped me

Divided code

Make the pieces independent from each other, interacting together in clear and simple ways.
For example a function that searches for something in a string must not do the string ordering also. Make that two independent functions, one search and one sort.

Reply

RE: Hey Programmers! Do you have a minute to share some wisdom ? #4
(02-12-2014, 05:54 PM)unixbreak Wrote: Thank you Ex094!
That's great info !

I will add one that helped me

Divided code

Make the pieces independent from each other, interacting together in clear and simple ways.
For example a function that searches for something in a string must not do the string ordering also. Make that two independent functions, one search and one sort.

Yes, That is one important thing too!
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: Hey Programmers! Do you have a minute to share some wisdom ? #5
To add: CLEAR variable names, dont use stuff like $t, $t1 and such, but make sure the variable describes what it actually does.

Also, keep the same namespacing, for example, precede all your query stuff with $sql_ and such

Reply

RE: Hey Programmers! Do you have a minute to share some wisdom ? #6
I think everything is mentioned already.
1- Use tabs instead of spaces, tabs are universal and work the same in every editor
2- comments should be straightforward, don't use too much and keep it clear(when you share the program make sure it's in english)
3- what steviecomet said is also very important, I personally name variables like this:
$iVarName for integer values
$aVarName for array values
$bVarNames for booleans etc.
4- make use of functions rather than writing the same code over and over again, you can use functions for your other projects as well. At some point you'll have a huge library that allows you to write code much faster
5- don't make use of deprecated functions like mysql_connect, fat chance in the future you'll have to recode the entire site(I've had that :p). In this case make use of pdo or in case of other functions check out the php manual for replacements.
(This post was last modified: 03-09-2014, 03:01 PM by xAttack.)

Reply

RE: Hey Programmers! Do you have a minute to share some wisdom ? #7
(03-09-2014, 02:18 PM)zomgwtfbbq Wrote: I think everything is mentioned already.
1- Use tabs instead of spaces, tabs are universal and work the same in every editor
2- comments should be straightforward, don't use too much and keep it clear(when you share the program make sure it's in english)

I sort of disagree with 1 and 2...

In python and Greek mythology, indentation [should,has to,must,is preferred to,recommended to,...] be 4 spaces, now you might think: "Why is this guy talking about python while we are talking about PHP! eh? What a Dum Dum!!!"

Well... I don't know why! But I know this: when I edit a code using vim or notepad, 4 spaces (or x number of spaces) is a better "standard" than a tab, as each editor (as you may already know) define a tab differently, yes good people of earth... they do that! And sometimes the results are extremely annoying... (and has bad effects such as vomiting and high blood pressure)

OK, about 2, what is too much? we've been taught all our life to comment our code heavily... one bad thing about open source projects is comments! People when coding think that we will just understand this:
Code:
akjshbediu = 17981.234; // this is the (x to m) alpha I was talking to mike the other day

Or
Code:
adkjhku_33234("Hello world\n", &askdj, &my_var91378); //wrap it up

I actually found some of the comments like those I mentioned above... in fact i broke a keyboard once because of this! (I was really mad!)

So... I think that you should set a rule about spacing, and don't worry about over commenting the code, at the end ... modern editors can fold them!

Thanks
[Image: wvBFmA5.png]

Reply

RE: Hey Programmers! Do you have a minute to share some wisdom ? #8
(03-09-2014, 03:02 PM)Ligeti Wrote:
(03-09-2014, 02:18 PM)zomgwtfbbq Wrote: I think everything is mentioned already.
1- Use tabs instead of spaces, tabs are universal and work the same in every editor
2- comments should be straightforward, don't use too much and keep it clear(when you share the program make sure it's in english)

I sort of disagree with 1 and 2...

In python and Greek mythology, indentation [should,has to,must,is preferred to,recommended to,...] be 4 spaces, now you might think: "Why is this guy talking about python while we are talking about PHP! eh? What a Dum Dum!!!"

Well... I don't know why! But I know this: when I edit a code using vim or notepad, 4 spaces (or x number of spaces) is a better "standard" than a tab, as each editor (as you may already know) define a tab differently, yes good people of earth... they do that! And sometimes the results are extremely annoying... (and has bad effects such as vomiting and high blood pressure)

OK, about 2, what is too much? we've been taught all our life to comment our code heavily... one bad thing about open source projects is comments! People when coding think that we will just understand this:
Code:
akjshbediu = 17981.234; // this is the (x to m) alpha I was talking to mike the other day

Or
Code:
adkjhku_33234("Hello world\n", &askdj, &my_var91378); //wrap it up

I actually found some of the comments like those I mentioned above... in fact i broke a keyboard once because of this! (I was really mad!)

So... I think that you should set a rule about spacing, and don't worry about over commenting the code, at the end ... modern editors can fold them!

Thanks
Hmmm so far all editors I've used interprete tabs the same, but that doesn't go for whitespaces. 4 spaces? Imagine you'd have to make additions to your program, your 4 space theory would cause the same high blood pressure as you get from tabs. :Grin: Apart from that tabs behavior can be set in any editor.

About the 2nd part of your comment, what I mean is that you don't necessarily need to make your comments like a book. If the comments are straightforward and tell what the code does you can keep your mind on the code. I personally hate lots of comments when the person could have said the exact same thing in one line.

Yes you're right about the comment folding, I love comment folding. :Heart:
(This post was last modified: 03-09-2014, 06:03 PM by xAttack.)

Reply

RE: Hey Programmers! Do you have a minute to share some wisdom ? #9
1. Consistency
2. Comments (short and meaningful)
3. Avoid magic numbers
4. Keep things modular
5. Run tests (blackbox testing, etc.)
6. Exception handling if needed
7. Keep things simple for the user
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Hey Programmers! Do you have a minute to share some wisdom ? #10
I think everyone has covered a lot of points. I only have two points to share.

(1) When you code, ask yourself this question, "In one year later, will I be able to understand my own code?".
(2) If there are a lot of repeated piece of code in your project, it smells like it is time for "REFACTORING".

Reply







Users browsing this thread: 1 Guest(s)