here are some questions that i have to improve my code readability

since i study alone and my code starts to be more big and more chaotic
i have some questions that maybe will sound silly to more but i have to ask someone.

1- its better to declare global only the constant variables?
2- should i create manyyyyy functions small and do simple small tasks each one?
3- should i let functions take many arguments like that:

void diagonalDownLeft(int[][size],int[][size],int,int,int&,bool,int);

if not how i can not do it?
4- comments!where i should put them?right of each line?or at top of a function or loop?do i need to comment everything?i tried at right of each line and was too messed with many comments :P

5- should i make more programs in classes for practice?
6- what is the most preferable spacing and indent to look less chaotic?

i will put in this thread more questions when i have them :D

As with many other things there are no right or wrong answers to the questions you have asked. One programmer may do it one way while another programmer will do it some other way. Its all a matter of personal preferences, or in some cases, company coding policy.

1. Generally, yes that's true. But there are always exceptions, and those should be very rare.

2. As long as you don't overdo them, such as writing a function that does nothing but write HelloWorld to the console window. I had an interview with one company whose coding policy was to put no more than one if statement in a function. That was going beyone what I believe to be the extreme right end of the line.

3. An alternative is to put all those parameters into a structure and pass a pointer to the structure.

4. I had one supervisor that couldn't even read C/C++ so I had to comment every line. The code looked horrible to me, but I had to give the boss what he wanted. At a minimum there should be a comment at the beginning of each function that explains the purpose of the function and possibly explain each of the arguments. Comments should appear elsewhee in the function to explain difficult-to-follow code.

5. Absolutely yes! The more practice you get the better and easier it will become. Great violinists do not become so great by playing computer games all the time.

6. I prefer indentation of 4 spaces, never use tabs. You can set these values and preferences in all the IDEs that I have used. If your IDE has an option to replace tabs with spaces then you should select that option because tags look horrible when posting code into web browsers or on other IDEs.

what is the difference of tab and spaces?is not the same?i thought its the same with the difference that at one you press one button only

Learn to think and design your programs in terms of real world concepts and use classes to express these ideas.

what is the difference of tab and spaces?is not the same?i thought its the same with the difference that at one you press one button only

They are not the same thing. A tab is a single character '\t' which can be interpreted differently by different IDEs and operating systems. When you replace tabs with spaces in your code it changes it from "Hello\tWorld" to "Hello........World" there each . is a space (DaniWeb forums removes extra spaces so I had to replace them with . character.

another question that i remembered is

for use srand and rand i readed that you need to #include <time>
but i use #include <cmath> is any difference on them?or just the same exists in both?

since i study alone and my code starts to be more big and more chaotic
i have some questions that maybe will sound silly to more but i have to ask someone.

As with most things, you need to study and work hard yourself. There isn't going to be guru created to make you the best, you have to be your own guru.

1- its better to declare global only the constant variables?
First of all, you should try to avoid global variables as much as possible. If you have no choice then you should encapsulate them.

2- should i create manyyyyy functions small and do simple small tasks each one?

Yes and no. Don't over over do it. But generally, make each function have specific small task. If possible, reduce the function into more function. But be aware, there is always some line, which is subjective.

3- should i let functions take many arguments like that:
Probably not. Try to break the function into sub-function, or maybe even encapsulate things in a struct and pass that struct. Do what make sense.

4- comments!where i should put them?right of each line?or at top of a function or loop?do i need to comment everything?i tried at right of each line and was too messed with many comments

This is a style issue. First put comments where you need it. Don't put comments in areas where the code explains what its doing itself.

5- should i make more programs in classes for practice?

No, pay attention in class. Ask the teacher questions. Practice in your spare time.


6- what is the most preferable spacing and indent to look less chaotic?

Subjective-Style. Do what is clear.

firstperson thanks but you missunderstood the study alone thing.

i am not a student,the teacher that i have is a book deitel&deitel "how to program c++" 7th edition and internet and yeee i am finishing chapter 7 and all exercises by myself :D

thats why i made that thread to take advices.
for me is more important to understand why something happens that to see it ready :D

what about the rand question?
some times the book instead of cout<<"3"; puts cout<<'3'; why? whats the difference?

srand and rand are in cstdlib. That is what you should include. usually, they might be included indirectly like from iostream library. And usually, you would import ctime library for the time function to generate different seed for srand every run.

And the difference between "3" and '3' in that example is little. "3" is a string that contains '3' and '\0'(null character), thus "3" is null terminated, while '3' is just a char.

All you need to know about rand can be found here.

'3' is a character, "3" is a string. cout simply outputs what it is given, so in the example you mention, there is no real difference. They are essentially identical.

2 more questions.

the book says that i should always use set and get functions for variables in classes
is this the best?i think someone told me once that i dont need to use get or set but i dont remember.

and the other question is,in a class at private: i declare a 2d array.
if i write int array[2][2]; is ok but if i write int array[2][2]={};
i get error why?

set and get: depends. If the variables are private then yes the class will need them if something else wants to set or get their values. IMHO its good to use them regardless so that there is only one place in the entire program where the class variables can be changed. If you ever have to change something then there is only one place to change it.

About 2d array: yes you will get compiler error. Initialize the array in the class constructor(s).

i have a question lets say that i have a string "hello" how i can witch between letters? to e or l or h?

i suppose it can happen like array using brackets but how?

i have a question lets say that i have a string "hello" how i can witch between letters? to e or l or h?

i suppose it can happen like array using brackets but how?

What? Please be clear.

i want to do an recursive exercise palindrome that says

a palindrome is a string that is spelled the same way forward and backward like "radar".

i want to write a recursive function testpalindrome that returns true if it is palindrome and false if its not.
***note that like array,the square brackets [] operator can be used to iterate through the characters in the string

*** thats my question too,i dont want advice about the program i want only to tell me how i can do the one with the brackets.

StringValue[0] is the 1st character in the string.
StringValue[2] is the 3rd character in the string.
and so on...

ah ok so i add the word by character with a loop,

i thought i was adding somehow the word as string like cin>>stringA;
and i was changing after.
ok thanks a lot.

ah ok so i add the word by character with a loop,

i thought i was adding somehow the word as string like cin>>stringA;
and i was changing after.
ok thanks a lot.

Not a clue what you're talking about here.

lol i just tested it and i understood what you said

sorry for my bad posts :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.