Thread: how to make my code more readable ?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    111

    how to make my code more readable ?

    Hello ..

    i've created a small program (it was an HW ) and i wish to get some ideas about how to make my code more readble:
    for example:
    use whitespace.
    .
    .
    ...use meaningful variables.
    this kind of ideas.


    so here is what i coded (ignore the main() it ain't good enuth yet)



    Code:
    /***************************************************************************
     *            Hw Assignment 1
     *
     *  Thu Apr  5 19:42:59 2007
     *  by Jabka Atu
     *  
     ****************************************************************************/
    /*
       As an hw assignment :(sorry if i write some thing that isn't understandble enugh)
     [qoute="Teacher"] 
      create a program in cpp that will do substraction and addition on strings
      the length is unknown and the string could store extreamly big integers (both postive and negatice).
      also create a histogram,get size,print etc functions.
     
      you cannot use verybigintegers.
      you can use iostream,math,string,assert.
     [/Qoute]
     
    
    
     */
    /*
     *  This program is free software; you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License as published by
     *  the Free Software Foundation; either version 2 of the License, or
     *  (at your option) any later version.
     *
     *  This program is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *  GNU General Public License for more details.
     *
     *  You should have received a copy of the GNU General Public License
     *  along with this program; if not, write to the Free Software
     *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
     */
     
    
    
    #include <iostream>
    #include <assert.h>
    #include <string>
    
    using namespace std;
    class VeryLarge{
    	private:
    		char *number;
    		int size;
    	
    		int remove_zeroes(char *input_to_func);
    		int check_if_postive(char *input_to_func);
    		char* add_two_postive_numbers(const VeryLarge& second);
    		void set_number_it(char *input_to_func);
    		char is_bigger(VeryLarge &first,VeryLarge &second);
    		VeryLarge* substruct_two_postive(VeryLarge &first,VeryLarge &second);
    		bool sign;// True for postive
    	
    	public:
    	
    		VeryLarge();
    		VeryLarge(char *input_to_func);
    		VeryLarge(const VeryLarge &);
    		~VeryLarge();
    		
    		void set_number(char *input_to_func);
    		char* get_number();
    		int get_size();
    		VeryLarge* addition	( VeryLarge &second);
    		VeryLarge* substruct( VeryLarge &second);
    		
    		void histogram();	
    		void print();
    	};
    
    //default constructer	
    VeryLarge::VeryLarge()
    {
    	number=NULL;
    	sign=false;
    	size=0;	
    	
    }
    
    VeryLarge::VeryLarge(char *input_to_func)
    {
    	number=NULL;
    	set_number(input_to_func);
    }
    
    //Copy constructer
    VeryLarge::VeryLarge(const VeryLarge& sec)
    {
    	number=new char[sec.size];
    	assert(number);
    	strcpy(number,sec.number);
    	size=sec.size;
    	sign=sec.sign;
    }
    
    //Distructer
    VeryLarge::~VeryLarge()
    {
    	if (number!=NULL)
    		delete [] number;
    	
    }
    
    int VeryLarge::check_if_postive(char *input_to_func)
    {
    	if ((input_to_func[0]=='+') || ((input_to_func[0]>='0')&&(input_to_func[0]<='9')))
    		return 1;
    	if (input_to_func[0]=='-')
    		return 0;
    }
    
    
    
    int VeryLarge::remove_zeroes(char *input_to_func)
    {
    	
    	int size_of_input_to_func=strlen(input_to_func);
    	int o_amount=0;
    	
    	for (int i=0;i<size_of_input_to_func;i++)
    		{
    		if (input_to_func[i]=='0')
    			{
    			o_amount++;
    			}
    		if (input_to_func[i]!='0') 
    			break;
    			
    		}
    	
     return o_amount;
    }
    
    
    
    
    void VeryLarge::set_number(char *input_to_func)
    {
    	if (number!=NULL)
    		delete [] number;
    	
    	int o_amount=0;
    	int postive_check=check_if_postive(input_to_func);
    	if (postive_check)
    		{
    		sign=true;
    	
    		if ((input_to_func[0]>='0')&&(input_to_func[0]<='9'))
    			{
    				o_amount=remove_zeroes(input_to_func);
    			}
    		else
    			{
    					o_amount=remove_zeroes(input_to_func+1) + 1;
    			}
    		}
    	
    	if (!postive_check)
    		{
    			sign=false;
    			o_amount=remove_zeroes(input_to_func+1) + 1;
    		}
    	
    	size = strlen(input_to_func);
    	size=size-o_amount;
    	
    	if (size!=0)
    		{
    			number=new char [size];
    			assert(number);
    			strcpy(number,input_to_func + o_amount);
    		}
    	if (size==0)
    		{
    			//since i in all the class i use number as an array i use the same methom the same here 
    			number= new char[2];
    			assert(number);
    			number[0]='0';
    			number[1]='\0';
    		}
    
    }
    
    char* VeryLarge::get_number()
    {
    	return number;
    }
    
    int VeryLarge::get_size()
    {
    	return size;
    }
    void VeryLarge::print()
    {
    	if (!sign)
    		cout<<'-';
    	cout<<number<<endl;
    }
    char* VeryLarge::add_two_postive_numbers(const VeryLarge& second)
    {
    
    	int carry=0;
    	char output=0;
    	int second_size =0;
    	int i;
    	second_size=second.size;
    
    	char *result=NULL;
    	int length_of_result=0;
    	int smallest_size=0;
    		if (size > second_size)
    			{
    				length_of_result = size + 1;
    				result = new char [length_of_result];
    				result[0]='0';
    				strcpy(result + 1 , number);	
    				smallest_size=second.size;
    			}
    
    		else{
    				length_of_result = second_size + 1;
    				result = new char [length_of_result];
    				result[0]='0';
    				strcpy(result + 1 ,second.number );	
    				smallest_size=size;
    			}
    	
    		for (i=0;i<smallest_size;i++)
    			{
    				output=number[size-1-i]+ second.number[second.size - 1 - i] + carry - 48 * 2;
    				carry=0;
    		
    				if (output>9)
    				{
    					result[length_of_result -1 - i] =output -10 + 48;
    					carry =1;
    				}
    				else
    					result[length_of_result - 1 - i] = output + 48;
    		
    			}
    		if (carry!=0)
    				result[length_of_result - 1 - i] = result[length_of_result - 1 -i ] + 1;
    				
    
    	return result;
    
    }
    
    VeryLarge* VeryLarge::addition(VeryLarge& second){
    	
    	VeryLarge *obj;
    	char *result=NULL;
    	
    	if (sign && second.sign)
    	{
    		result=add_two_postive_numbers(second);
    		obj=new VeryLarge(result);
    	}
    	
    	if (!sign && !second.sign)
    	{
    		result=add_two_postive_numbers(second);
    		
    		obj=new VeryLarge(result);
    		(*obj).sign=false;
    	}
    	
    	if (!sign &&second.sign)
    	{
    		(*this).sign = true;
    		obj=substruct(second);
    		(*obj).sign=!(*obj).sign;
    		(*this).sign=false;
    	}	
    	if (sign && !second.sign)
    	{
    		second.sign=true;
    		obj=substruct(second);
    		second.sign=false;
    	}
    	
    	if (result!=NULL)
    		delete [] result;
    	
    	return obj;
    }
    
    char VeryLarge::is_bigger(VeryLarge &first,VeryLarge &second){
    	/*############################################
    		This Routine is to find wich array is bigger (the value)
              ############################################
    	*/
    	if (first.size > second.size)
    		return 1;
    	
    	if (first.size < second.size)
    		return 0;
    	
    	if (first.size == second.size)
    		{
    			
    			for (int i=0;i<first.size;i++)
    				{
    					if (first.number[i]>second.number[i])
    						return 1;
    					if (first.number[i]<second.number[i])
    						return 0;
    				}
    				
    			return -1;//are equal
    		}
    }
    
    
    VeryLarge* VeryLarge::substruct_two_postive(VeryLarge &first,VeryLarge &second){
    
    	VeryLarge *obj=NULL;
    	//#################
    		char *result;
    	//#################
    	/*
    	At first i thout to use integer but then i thout why don't i use chars (only 1 byte instead of 4).
    	and as the values are in -255 to 
    	int i=0;
    	int borrow=0;
    	int ans=0;
    
    	int size_of_first=strlen(first_value);
    	int size_of_second=strlen(second_value);
    	int biggest_size=(size_of_first>size_of_second)?size_of_first:size_of_second;
    	int smallest_size=((size_of_first<size_of_second)?size_of_first:size_of_second);
    	*/
    	
    	char i=0;
    	char ans=0;
    	char borrow=0;
    	char size_of_first=first.size;
    	char size_of_second=second.size;
    	char biggest_size=(size_of_first>size_of_second)?size_of_first:size_of_second;
    	char smallest_size=((size_of_first<size_of_second)?size_of_first:size_of_second);
    
    	result=new char[biggest_size + 1];//one more for '\0'
    	result[biggest_size]='\0';
    
    	//#################
    		
    	char *first_value=first.number;
    	char *second_value=second.number;
    		
    	//#################
    		
    		for (i=0;i<size_of_second;i++)
    				{
    					ans=first_value[size_of_first - i-1] - second_value[size_of_second-i-1]-borrow; 				
    					borrow=0;
    														
    					if (ans<0)
    						{
    							borrow=1;
    							result[biggest_size - i - 1]= ans + 10 + 48;
    						}
    					else 
    						{
    							result[biggest_size - i - 1]=ans + 48;
    						}
    					
    				}//end of for
    				
    		if (borrow)
    				{
    					for (i=0;i< (size_of_first - size_of_second);i++)
    						{
    							ans=first_value[size_of_first -size_of_second- i-   1 ] - borrow;
    							borrow=0;
    							if (ans<48)
    							{
    								borrow=1;
    								result[biggest_size-size_of_second - i-1 ]= ans + 10;
    							}
    							else 
    							{
    								result[biggest_size-size_of_second - i-1]=ans;
    							}	
    							
    						}//end of for
    										
    				}//end of if (exsitence of borrow)
    		
    	obj=new VeryLarge(result);
    		
    	return obj;
    	
    
    }
    	
    
    VeryLarge*  VeryLarge::substruct( VeryLarge &second){
    	
    	VeryLarge *result;
    	//I will use return inside each of if sentence to make it work faster:
    	
    	if (sign && second.sign)
    			{
    				result=substruct_two_postive((*this),second);
    				return result;
    			}
    	
    		
    	if (sign && !second.sign)
    		{
    	
    			second.sign=true;
    			result=addition(second);
    			second.sign=false;
    			
    			return result;
    		}
    
    
    	
    	if (!sign && second.sign)
    		{
    			this -> sign = true;
    			second.sign = true;
    			result=addition(second);
    			(*result).sign=false;
    			this -> sign = false;
    			return result;
    		}
    	
    	if (!sign && !second.sign){
    		
    		second.sign=true;
    		result=substruct_two_postive((*this),second);
    		second.sign=true;
    		(*result).sign=false;
    		
    		return result;
    	}
    	
    		
    }
    
    
    void VeryLarge::histogram(){
    	
    	char amount[11]={48,48,48,48,48,48,48,48,48,48,'\0'};
    	char max='0';
    	int i=0;
    	int j=0;
    	for (i=0;i<size;i++)
    		{
    			amount[number[i]-'0']++;
    			if (amount[number[i]-'0']>max)
    			max=amount[number[i]-'0'];
    		}
    	
    	for (i=0;(i<(max - '0'));i++)
    		{
    			cout<<max-i-'0'<<":";
    			for (j=0;j<10;j++)
    				{
    			
    					if (amount[j]!=(max-i))
    						cout<<"  ";
    					if (amount[j]==(max-i))
    						{
    							cout<<"* ";
    							amount[j]--;
    						}
    				}
    		
    			cout<<endl;
    		}
    	
    	cout<<"  ";
    	
    	for (i=0;i<10;i++)
    		cout<<i<<" ";
    
    }
    
    void menu(){
    
    	cout<<"Welcome to SCS (String Calculation System)\n";
    	cout<<"\n\n";
    	cout<<"1. Enter first value \n";
    	cout<<"2. Enter second value \n";
    	cout<<"3. Add the strings (Arithmetic)\n";
    	cout<<"4. subtract the second string from the first\n";
    	cout<<"5. histogram of first value\n";
    	cout<<"6. histogram of seconf value n\n";
    	cout<<"7. extand the limit of input stram to n\n";
    	cout<<"q. to exit\n";
    
    }
    
    bool check_input(char *buff){
    	int size=strlen(buff);
    	int i=0;
    	if (!((buff[0]=='+')||(buff[0]=='-')||((buff[0]>='0')&&(buff[0]<='9'))))
    		return false;
    	for (i=1;i<size;i++)
    		if (!((buff[i]>='0')&&(buff[i]<='9')))
    			return false;
    	return true;
    }
    
    void get_number(char *buff)
    {	
    	cout << "Enter number: ";
    	cin>>buff;
    	if (!(check_input(buff)))
    		{
    		cout<<"Dood im sorry but your input isn't ok"<<endl;
    		get_number(buff);
    		}
    	
    }
    
    int main()
    {
    
    	char ch='0';
    	int n=1024;
    	char *buff=new char[n];
    	VeryLarge num1,num2,*obj;
    	
    	while ((ch!='q')&&(ch!='Q'))
    		{
    			menu();
    			cout<<"\nchoice:";
    			ch=getchar();
    			switch (ch)
    				{
    					case '1':
    						{
    						cout<<"\nThe First number :\n";
    						get_number(buff);		
    						num1.set_number(buff);
    						break;
    						}
    					case '2':
    						{
    						cout<<"The Second number :\n";
    						get_number(buff);
    						num2.set_number(buff);
    						cout<<endl;
    						break;
    						}
    					case '3':
    						{
    						obj=num1.addition(num2);
    						cout <<"The Result is :";
    						(*obj).print();
    						delete obj;
    						break;
    						}
    					case '4':
    						{
    							cout<<"The result is \n";
    							obj=num1.substruct(num2);
    							(*obj).print();
    							delete obj;
    							break;	
    						}
    					case '5':
    						{
    							num1.histogram();
    							break;
    						}
    					case '6':
    						{
    							num2.histogram();
    							break;
    						}
    					case '7':
    						{
    							cout<<"Please enter an intger size";
    							cin>>n;
    							delete [] buff;
    							buff = new char[n];
    							assert(buff);
    							break;
    						}
    					
    				}	
    			
    		}
    
    return 0;
    }
    Last edited by jabka; 04-19-2007 at 04:35 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I think you already got two ideas. Add whitespace. In particular, your code could use spaces between tokens. For example, this:
    Code:
    if (!((buff[i]>='0')&&(buff[i]<='9')))
    might be easier to read as this:
    Code:
    if (!((buff[i] >= '0') && (buff[i] <= '9')))
    As for meaningful variable names, it looks like you tried, but VeryLarge and input_to_func can still be improved. Very large what? It's a representation of a potentially very large integer, right? So say VeryLargeInteger or BigInt or something. Also, input_to_func is obviously the input to the function, but what data does it hold? Is it a string that holds the number? What does it represent?

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    first off, I'd use less than 8 spaces for indentation, but I presume this is some artefact of how you posted the code.

    generally I like to put a space on either side of a binary operator and after commas and semi-colons in for loops.
    Most people also indent their braces {} at the same level as the construct above

    example:
    Code:
    	
    // your code
    	for (i=0;i<size;i++)
    		{
    			amount[number[i]-'0']++;
    			if (amount[number[i]-'0']>max)
    			max=amount[number[i]-'0'];
    		}
    
    // refactored
    	for (i = 0; i < size; i++)
    	{
    		amount[number[i] - '0']++;
    		if (amount[number[i] - '0'] > max)
    			max = amount[number[i] - '0'];
    	}
    There are several parts of your code with inconsistent formatting: sometimes you declare pointers/references as
    Code:
    type *variable;
    type &variable;
    and sometimes as
    Code:
    type* variable;
    type& variable;
    It's not a huge deal, it's just potentially confusing.

    these are personal opinions. the only absolute rule with code formatting is to pick a convention and stick to it. Be consistent.

    Also if you're working with a group of developers, settle on a format before you start. If they already have a format when you join, do not attempt to impose yours on them.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    In addition to what has already been mentioned, you could try to eliminate some redundancies and make use of standard functions.

    For example:
    Code:
    int VeryLarge::check_if_postive(char *input_to_func)
    {
       if ( (input_to_func[0]=='+') || ((input_to_func[0]>='0')&&(input_to_func[0]<='9')) )
          return 1;
       if ( input_to_func[0]=='-' )
          return 0;
    }
    This might be written like this:
    Code:
    int VeryLarge::check_if_postive(char *input_to_func)
    {
       if ( input_to_func[0] == '+' || isdigit(input_to_func[0]) )
       {
          return 1;
       }
       if ( input_to_func[0] == '-' )
       {
          return 0;
       }
       // And what if it is none of the above? [Error checking -- maybe return -1?
    }
    I always use full bracing. I like the extra vertical whitespace and it keeps the bugs from biting.

    And here, think using an else -- if something is not true, it's false; no need to check the same thing twice.
    Code:
       for ( int i = 0; i < size_of_input_to_func; i++ )
       {
          if ( input_to_func[i] == '0' )
          {
             o_amount++;
          }
          if ( input_to_func[i] != '0' )
          {
             break;
          }
       }
    Or even consider what break does.
    Code:
       for ( int i = 0; i < size_of_input_to_func; i++ )
       {
          if ( input_to_func[i] != '0' )
          {
             break;
          }
          o_amount++;
       }
    Some might even suggest something like this:
    Code:
       for ( int i = 0; i < size_of_input_to_func && input_to_func[i] != '0'; i++ )
       {
          o_amount++;
       }
    I'm not fond of the last one, but some are in that camp.

    Similarly, consider this.
    Code:
       int postive_check=check_if_postive(input_to_func);
       if ( postive_check )
       {
          sign=true;
    
          if ( (input_to_func[0]>='0')&&(input_to_func[0]<='9') )
          {
             o_amount=remove_zeroes(input_to_func);
          }
          else
          {
             o_amount=remove_zeroes(input_to_func+1) + 1;
          }
       }
    
       if ( !postive_check )
       {
          sign=false;
          o_amount=remove_zeroes(input_to_func+1) + 1;
       }
    This might be done like this.
    Code:
       sign = check_if_postive(input_to_func);
       if ( sign )
       {
          if ( isdigit(input_to_func[0]) )
          {
             o_amount = remove_zeroes(input_to_func);
          }
          else
          {
             o_amount = remove_zeroes(input_to_func + 1) + 1;
          }
       }
       else
       {
          o_amount = remove_zeroes(input_to_func + 1) + 1;
       }
    This was a fairly quick pass at it, with a goal of moving toward something that might eventually end up like this.
    Code:
       bool offset = sign && !isdigit(input_to_func[0]) || !sign;
       o_amount = remove_zeroes(input_to_func + offset) + offset;
    bool x = sign && !isdigit(input_to_func[0]) || !sign;
    o_amount = remove_zeroes(input_to_func + x) + x;[/code]This only looks "cool" to the new:
    Code:
             result[length_of_result - 1 - i] = output + 48;
    Later on it is often preferred to do this.
    Code:
             result[length_of_result - 1 - i] = output + '0';
    And the logic of things like this could be simplified:
    Code:
       if ( sign && second.sign )
       {
          result=add_two_postive_numbers(second);
          obj=new VeryLarge(result);
       }
    
       if ( !sign && !second.sign )
       {
          result=add_two_postive_numbers(second);
    
          obj=new VeryLarge(result);
          (*obj).sign=false;
       }
    
       if ( !sign &&second.sign )
       {
          (*this).sign = true;
          obj=substruct(second);
          (*obj).sign=!(*obj).sign;
          (*this).sign=false;
       }
       if ( sign && !second.sign )
       {
          second.sign=true;
          obj=substruct(second);
          second.sign=false;
       }
    If you think about it, !sign && !second.sign and !sign && !second.sign both mean the same thing: is the sign the same? It could be written like this sign == second.sign. When the signs are not the same, that would be an else. So rewriting it might point out some issues that might be buggy.
    Code:
       if ( sign == second.sign )
       {
          result = add_two_postive_numbers(second);
          obj = new VeryLarge(result);
          if ( !sign )
          {
             (*obj).sign = false;
          }
       }
       else
       {
          (*this).sign = true;
          obj = substruct(second);
          (*obj).sign = !(*obj).sign;
          if ( !sign )
          {
             (*this).sign = false;
          }
       }
    Now I didn't do any debugging; these were meant as general examples for you to look into if you care to.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    111
    good evening ....
    sorry for that i didn't replay fast (i found some bug in program) and my network went down ...
    few more question:
    if condition
    {
    ....
    }
    else
    {
    ....
    }
    or
    if condition
    {
    ....
    }

    else
    {
    ....
    }
    what about the pointer handling :
    what is better :
    Code:
    (*ptr).field
    i think it's more clear but i'm not sure
    or
    Code:
    ptr -> field
    also i have som bug in addition and substraction
    the fix for substruct_two_postive is here :
    Code:
    char* substract_two_postive_numbers(char *first_value,char *second_value){
    	//  VeryLargeInteger* substruct_two_postive(VeryLarge &first,VeryLarge &second);
    	//  first_value  = first.number
    	//  second_value = second.number;
    	//  VeryLargeInteger *obj;
    	/*
    	Changelog:
    	
    	replaced size_of_first - size_of_second to delta : this will make it work faster
    	added copy rutine when there is no borrow
    	*/
    
    	char *result   = NULL;
    	char i		  = 0;
    	char ans       = '0';
    	char borrow  = 0;
    	
    	//for object will use object.size
    	char size_of_first       = strlen(first_value);//(*this).size          //the post system here does some thing strange with tabs all the = are on the same colmun
    	char size_of_second = strlen(second_value);//second.size
    	char biggest_size      = (size_of_first > size_of_second) ? size_of_first : size_of_second;
    	char smallest_size    = (size_of_first < size_of_second) ? size_of_first : size_of_second;
    	char delta 	           = size_of_first - size_of_second;
    	
    	result=new char[biggest_size + 1]; //one more for '\0'
    	result[biggest_size] = '\0';
    
            //i need to figure how to does 3 for in on block
    	for (i = 0;i < size_of_second; i++)
    	{
    		ans=first_value[size_of_first - i - 1] - second_value[size_of_second - i - 1] - borrow; 				
    		borrow = 0;
    		if (ans<0)
    		{
    			borrow=1;
    			result[biggest_size - i - 1]= ans + 10 + 48;
    				
    		}
    		else 
    		{
    			result[biggest_size - i - 1]=ans + 48;
    		}
    	}
    			
    	if (borrow)
    	{
    		for (i = 0;i < (delta);i++)
    		{
    			ans=first_value[delta - i-   1 ] - borrow;
    			borrow = 0;
    
    			if (ans < 48)
    			{
    				borrow = 1;
    				result[delta - i - 1 ] = ans + 10 ;
    			}
    			else 
    			{
    				result[delta - i - 1] = ans ;
    			}	
    							
    		}//end of for
    	}//end of if (exsitence of an borrow
    	
    	if(!borrow)	//since there is an option that there 
    				//will be no borrow i will cp it.
    	{
    		
    		for(i = 0;i < delta ; i++)
    		{
    			result[delta - i - 1] = 
    				first_value[delta - i - 1];
    		}
    	
    	}
    
    	//obj = new VeryLargeInteger(result)
    	//return obj
    	return result;
    }
    Last edited by jabka; 04-20-2007 at 12:35 PM.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by jabka View Post
    what about the pointer handling :
    what is better :
    Code:
    (*ptr).field
    i think it's more clear but i'm not sure
    The -> syntax is universally preferred. However, there are many equivalent ways:

    Code:
    ptr->field /* Do it this way */
    (*ptr).field
    ptr[0].field
    0[ptr].field
    The last one is a joke, but it actually works.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use code tags, not quote tags to post code. Your two if/else blocks look the same.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by brewbuck View Post
    The -> syntax is universally preferred. However, there are many equivalent ways:
    Not fully equivalent. The array indexing stuff doesn't (necessarily) work with pointer-like objects (iterators & smart pointers).
    Not even (*p).foo and p->foo are fully equivalent, although they should be in a sane system.

    Quote Originally Posted by Dave_Sinkula
    Some might even suggest something like this:
    Code:
       for ( int i = 0; i < size_of_input_to_func && input_to_func[i] != '0'; i++ )
       {
          o_amount++;
       }
    I'm not fond of the last one, but some are in that camp.
    Interesting. Why are you not fond of it? Most people avoid jump statements like break when it's so easy to.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I much prefer the break. Complicated statements inside the for control are less readable IMO than a break statement.

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Well, i avoid the for statement almost entirely, I prefer :

    Code:
    x = 0;
    while ( x < SomeVal ){
        SomeCode();
        x++;
        }
    notice that I keep my braces indented as welll, which makes scanning context changes easier.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Again, that seems worse than the comparable for loop. It leaves x in scope after the loop is over, and it means you cannot use continue statements clearly inside the loop. It also makes the three important parts of the loop harder to find.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by CornedBee View Post
    Interesting. Why are you not fond of it? Most people avoid jump statements like break when it's so easy to.
    Pretty much what Daved said. It's easier for me to miss that there are two control expressions.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. guide me to make this code better or efficient
    By larne in forum C++ Programming
    Replies: 11
    Last Post: 01-19-2009, 05:54 AM
  2. make user press enter / wierd code thingy
    By CorvusVita in forum C++ Programming
    Replies: 10
    Last Post: 04-05-2006, 09:55 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Trying to make this code faster & Cramer
    By just2peachy in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2004, 10:54 AM