Login Register


C++ program filter_list
Author
Message
RE: C++ program #11
Take this for example:
Code:
#include <iostream> int x = 2; int main() { int x = 1; std::cout << x << std::endl; std::cout << ::x << std::endl; }

You can see where being explicit is of benefit? When you write something like 'using namespace std;' you destroy the whole point of a namespace...
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: C++ program #12
(05-15-2014, 12:07 AM)ArkPhaze Wrote: Take this for example:
Code:
#include <iostream> int x = 2; int main() { int x = 1; std::cout << x << std::endl; std::cout << ::x << std::endl; }

You can see where being explicit is of benefit? When you write something like 'using namespace std;' you destroy the whole point of a namespace...

that was my point...that its best practice to use std:: not 'using namespace' unless you're actually developing for a namespace...but I thought you stated earlier that introducing ambiguity wasn't important but namespaces were created because of bugs arising from ambiguity. We seem to agree on what the best practice is. I'm sorry I didn't explictly state the purpose of a namespace
(This post was last modified: 05-15-2014, 12:56 AM by miiike980.)

Reply

RE: C++ program #13
(05-15-2014, 12:07 AM)ArkPhaze Wrote: What is 'usinging namespame' exactly?
A typo, I'm pretty sure you know what I meant? Incase you don't let this help you:

Code:
using namespace

(05-15-2014, 12:07 AM)ArkPhaze Wrote: ... Performance, as I'm trying to tell you, is out of the question. OP posted about that line of code, not programming in C++ in general.

Firstly, OP was ambiguous in his question stating neither that the question was limited to this particular code or if he was asking in general. I took one understanding and posted about it.

So I don't know about you but when someone has multiple pieces of code and asks which is better I have a set of metrics which I use to try to come to a conclusion by. These metrics include:

1. Performance - If one piece of code is significantly better than the other it weighs heavily on my opinion.
2. Clarity - Is one piece of code easier to read. By that I mean I care about whether or not if I was assigned to maintain the code I would be able to readily understand it.

These are the two main metrics by which I judged the two options. Performance makes no difference here because both choices result in similar binaries, that doesn't mean I don't consider it a metric anymore its just that its not one that is going to make the difference. Which means to me the choice comes down to a style choice or question of clarity...


(05-15-2014, 12:07 AM)ArkPhaze Wrote: 'using namespace std;' is not related to coding style... Preference maybe, but that's not the same as coding style. Style has absolutely nothing to do with what declarations you make, but rather how you name your variables, what design paradigms you use, etc...

I guess I need to be clear about what coding style means... http://en.wikipedia.org/wiki/Programming_style

Quote:Programming style is a set of rules or guidelines used when writing the source code for a computer program.

I think that's pretty clear, so I believe whether you use 'using namespace ...' or always explicitly declare the namespace to be one of those guidelines followed when witting code. Maybe you define it differently that's okay, this is what I mean when I refer to coding style.

Quote:He specifically asked which was better to be using and why. You didn't answer anything.

I reasoned on the basis of the two above metrics(would you mind sharing by what metrics you're judging the code?)

Through that reasoning I determined that explicitly stating the namespace introduces the least amount of ambiguity and hence maintaining the clarity of the code and prevents bugs that could be introduced(and difficult to find) because of the ambiguity. I believe this is very relevant to the question regardless of your denial.

Quote:You're not a programmer are you?

I am mostly C professionally.

(05-15-2014, 12:07 AM)ArkPhaze Wrote: now you're saying any code is fine if it works?

That was sarcasm sorry; doesn't come across clearly in text Tongue. it was prompted by you're statement of "It would be perfectly find within a small body function," so I basically in a sarcastic tone was saying; yea and its perfectly fine if it runs. I recognize and affirm that even though code runs fine following best practices and developing good habits are important from small to big projects.

(05-15-2014, 12:07 AM)ArkPhaze Wrote: You don't even know what good habits are because you're not a C++ programmer.

Yet I am a developer and I do know enough C++ to answer this question...namespaces are not a unique feature to only C++ you know.

(05-15-2014, 12:07 AM)ArkPhaze Wrote: You can't answer OP's question for him.

And yet I did...now what?

(05-15-2014, 12:07 AM)ArkPhaze Wrote: This isn't a philosophical language, and C++ has many nuances that most don't understand at all.

Nope but it is an imperative language of which I know several and some of them even have namespaces also. Namespaces are not some crazy idea only C++ does, I understand enough to answer this basic question.

Quote:You're completely wrong btw..

Well can you clarify your next post then because you seem to affirm what I'm encouraging that he should use std:: and not 'using namespace std;'

(05-15-2014, 12:07 AM)ArkPhaze Wrote: I don't know why you're posting advice in this section when you have no idea about what that line of code does or what downsides it has when a programmer uses it over explicitly writing out the namespace specifiers...

I still don't know what you're talking about I'm affirming using std:: and not 'using namespace std;' I think you've misunderstood what I've said but allow me to repeat by original post on the matter.

Quote:So in the case of std I would advise to use std:: for clarity.

I think the real issue here is that you think I was advising something I was not and rightfully came down against it. It seems we ultimately agree, the best practice is to explicitly declare the namespace and not to use "using namespace ..."
(This post was last modified: 05-15-2014, 01:27 AM by miiike980.)

Reply

RE: C++ program #14
I know it's a bit late reply, but incase someone else stumbles upon this thread I'd like them to know there're other ways to do it as well, one way I prefer is like this:

Code:
int main(){ // We want to initiate the values for safety precautions int input = -1, sum = 0; // while we're recieving inputs and the input is not equal to 0 while (cin >> input && input != 0){ // same as sum = sum+input sum += input; } cout << sum; return 0; }

Reply

RE: C++ program #15
(07-26-2014, 12:18 AM)Marcus Ekström Wrote: I know it's a bit late reply, but incase someone else stumbles upon this thread I'd like them to know there're other ways to do it as well, one way I prefer is like this:

Code:
int main(){ // We want to initiate the values for safety precautions int input = -1, sum = 0; // while we're recieving inputs and the input is not equal to 0 while (cin >> input && input != 0){ // same as sum = sum+input sum += input; } cout << sum; return 0; }

I hope you know what you're doing when you do:
Code:
while (cin >> input ...)

You should be checking the state of the stream too since you're using cin and not a string stream. Because not only will an input of 0 break that while loop, other input will as well. But, not only that, you'll leave data behind in the stream, and the stream itself will have left the good state.

Code:
#include <iostream> int main() { using namespace std; /* input something like "sdf" here */ int input = -1, sum = 0; while (cin >> input && input != 0) { sum += input; } cout << sum << endl; /* you don't specify anything here, input still exists within the buffer and the stream has left the good state */ cout << "cin good? " << (cin.good() ? "Yes" : "No") << endl; int x = -99; cin >> x; cout << "X: " << x << endl; cout << "Done..." << endl; }
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply







Users browsing this thread: 1 Guest(s)