Closed Thread
Results 1 to 5 of 5

Thread: Query on correct coding practise

  1. #1
    squid attack is offline Learning Programmer
    Join Date
    Jul 2010
    Posts
    32
    Rep Power
    0

    Query on correct coding practise

    The following code compiles, but I was just wondering whether it was safe or an unsound practice
    to ignore the additional if (randvar >=3) //{do stuff} line and just continue the code, (particularly if the continued code is exactly what the randvar result would want to do if it was 3 or more. )

    Thanks for any clarification on this.( Not sure of the correct terminology above).

    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    
    int otherFunc (int randvar)
    {
     // do whatever
    return 0;
    }
    
    int main ()
    {
    	srand (time (NULL));
    
    	int randvar = (rand()%10+1);
    if (randvar <= 2) { otherFunc (randvar);}
    
     cout<<" do other stuff ignoring  if (randvar >=3) {// }" <<endl;
    
    cin.ignore().get(); 
    	return 0;
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Query on correct coding practise

    I don't think I understand your question.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    squid attack is offline Learning Programmer
    Join Date
    Jul 2010
    Posts
    32
    Rep Power
    0

    Re: Query on correct coding practise

    Sorry.
    I guess I mean the next line of code after if (randvar <= 2) { otherFunc (randvar);}
    should be if (randvar >=3) { do something else);}

    I know if I omit if (randvar >=3) { do something else}
    and just continue the code it compiles, but what I was just wondering was
    whether this was acceptable or not?

  5. #4
    dbug is offline Programmer
    Join Date
    Sep 2010
    Posts
    155
    Rep Power
    8

    Re: Query on correct coding practise

    I'm not sure if I undertand correctly your question...

    The code is syntactically correct with or without the "if (randvar >= 3)". Another question is what do you really want to do. If you do not add the condition, then both statements (the otherFunc call and the cout) will be executed when randvar <= 2, and only the last one otherwise. In the other hand, if the condition is added, only one of the statements will be executend, depending on the value of randvar. If this is the case, I think the best way would be:

    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int otherFunc(int randvar)
    {
    	// do whatever
    	return 0;
    }
    
    int main()
    {
    	srand(time(NULL));
    	int randvar = (rand() % 10 + 1);
    	if (randvar <= 2)
    	{
    		otherFunc(randvar);
    	}
    	else // or "if (randvar >= 3)" if you like
    	{
    		cout<<" do other stuff ignoring  if (randvar >=3) {// }" <<endl;
    	}
    	cin.ignore().get(); 
    	return 0;
    }

  6. #5
    squid attack is offline Learning Programmer
    Join Date
    Jul 2010
    Posts
    32
    Rep Power
    0

    Re: Query on correct coding practise

    Thanks dbug thats exactly what I wanted to know. Whether either method was syntactically acceptable.
    Trying to awkwardly express such a query made googling it impossible!

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Best XNA Engine support/practise
    By Pedro in forum C# Programming
    Replies: 0
    Last Post: 10-03-2011, 12:32 PM
  2. Intermediate PHP Optimization - Theory and Practise
    By Alexander in forum PHP Tutorials
    Replies: 2
    Last Post: 12-02-2010, 05:30 AM
  3. Am I correct?
    By unleashed-my-freedom in forum Visual Basic Programming
    Replies: 1
    Last Post: 01-10-2010, 05:53 AM
  4. Is my recursion correct?
    By Gear2d in forum C and C++
    Replies: 4
    Last Post: 09-19-2008, 11:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts