LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2012, 10:26 AM   #16
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081

Quote:
Originally Posted by worzel1968 View Post
i didn't post all of it (hence no initialisation)
I figured that might be the case, but the point still stands that a while loop doesn't fit the flow of the program which makes it easier to make mistakes as you did when summarizing. Basically repeating code is bad, and the while loop forces repeating the input statement. Looking at the rest of the code, it's clear that get-valid-input is also being repeated several times and should go into a function, like johnsfine said.
 
1 members found this post helpful.
Old 06-29-2012, 01:07 PM   #17
worzel1968
Member
 
Registered: Mar 2011
Location: Melbourne, Australia
Distribution: fedora17_64, ubuntu
Posts: 44

Original Poster
Rep: Reputation: 2
Thanks again for all your replies.

I am pleased, because you all seem to be saying what I was thinking.

@Sergei: Thanks for your acoustic example. I had a psuedo code example (above) of where I thought a do loop was necessary - I am pleased that you have provided a real life example.

It wasn't my code, and I recommended that the other guy abandon it, and start again with some clear thinking. I thought that it was important to do a design by commenting the methodolgy, That's what we did, except with a basic calculator, that had a basic design like this:

Code:
//Get a float
//Get an operator
//Get a float
//Calculate the answer
The GetFloat function has the error checking in it (like johnsfine said) - it uses scanf for simplicity. We are nearly there with having clear concise code (funny we have no do loops). The OP's code for the calculator was similar to the example code above - quite frankly a nightmare considering the simple nature of the problem, and he had 100 lines of code.

The problem with these novice coders is they tear into writing code, often without any or little idea of what they are doing. I suppose everyone has to start somewhere, but I have seen some shockers. Calling main (inside main) to restart the program was a beauty!!

You guys could have a field day with all these novices, but the problem is that one seems to be giving out the same advice over & over.

Finally, there have been no comments about my restricted Hungarian notation - do I assume the no news is good news. Wait - I shouldn't assume anything.
 
Old 06-29-2012, 01:55 PM   #18
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by worzel1968 View Post
there have been no comments about my restricted Hungarian notation
I hate all forms of Hungarian notation. For me they interfere with seeing the meaning of the names when I look at chunks of code. Obviously, I can look at a name and mentally cut off the type info in order to read the rest. But even when working with a lot of Hungarian notation code, I can't make myself see it that way when I read code the way I would read an English sentence (which does not require consciously reading individual words).

On this topic, I'm just not as sure as on typical programming style topics that what I see a wrong is really wrong, rather than just wrong for me.
 
Old 06-29-2012, 01:56 PM   #19
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by worzel1968 View Post
there have been no comments about my restricted Hungarian notation
I hate all forms of Hungarian notation. For me they interfere with seeing the meaning of the names when I look at chunks of code. Obviously, I can look at a name and mentally cut off the type info in order to read the rest. But even when working with a lot of Hungarian notation code, I can't make myself see it that way when I read code the way I would read an English sentence (which does not require consciously reading individual words).

On this topic, I'm just not as sure as on typical programming style topics that what I see as wrong is really WRONG, rather than just wrong for me.
 
1 members found this post helpful.
Old 06-29-2012, 07:07 PM   #20
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
This seems to fall into that category of question where one camp says "Whoaaaa, there! Too unconventional! It causes people to create bugs!", and the other camp who expects competent readers to understand the constructs supported by the language, and to know the circumstances under which any particular construct makes sense. I stand squarely in the latter camp. I don't (usually) feel bad about using programming language features that are unconventional or obscure, and I don't light my hair on fire when I see things written in ways I don't immediately grasp.

OTOH, I do try to write code to suite my audience, if I know who that is. I would not, for example, knowingly use some obscure language element in a tutorial code sample in these forums. If I was writing code that would be expected to be efficient in some way, and know that other programmers who might read it would be skilled in using the language, I would feel quite at ease to use whatever language construct the situation called for. Sometimes, bending the code around to satisfy some other programmer's sensibilities seems counterproductive and awkward.

If I encounter code that is written in some way that seems unconventional, I start with the notion that it was written that way for some good reason. Sometimes that turns out to be true, and other times I think it does not. Many times, it forces me to think about the problem in a different way than I might naturally. Many times, it teaches me a new technique.

In other words, I don't think there is just one correct answer.

--- rod.
 
1 members found this post helpful.
Old 06-30-2012, 09:48 AM   #21
worzel1968
Member
 
Registered: Mar 2011
Location: Melbourne, Australia
Distribution: fedora17_64, ubuntu
Posts: 44

Original Poster
Rep: Reputation: 2
Thanks Rod for your informative reply.

It's great to see what other peolple vastly more experienced than me do.

Thanks for the time spent in replying.

Cheers
 
Old 06-30-2012, 10:35 AM   #22
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
I don't see much upside to Hungarian notation. The downside is duplicated information.
Quote:
double dScaleFactorArray[] = {1.0, 1.1, 1.2};
For instance, if you decide you only want single float precision, now you have to go and change the name to fScaleFactorArray everywhere you use it.

I do sometimes use "n" as a prefix to indicate a count of things (but sometimes I use "_len" as a suffix instead), eg:
Code:
nCards = getNumber("how many cards should I deal?");
hand = deck.deal(nCards);
but it's probably better to have the count attached to the collection:
Code:
hand.size = getNumber("how many cards should I deal?");
deck.deal(hand); // although now we have to fill hand by side effect, hmm...

Quote:
unsigned short usDayOfYear = 0;
I read "us" as short for United States, which is a bit confusing.

Quote:
const unsigned short usDaysInWeekConst = 7; //should use #define for this
const double dEarthRadiusConst = //whatever it is. Better use of a const
I'm wondering what you're getting at with the #define vs const thing here.

EDIT: all quotes refer to worzel1968's post

Last edited by ntubski; 06-30-2012 at 02:19 PM. Reason: clarify quote source
 
Old 06-30-2012, 11:04 AM   #23
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Hungarian notation is by large used completely wrong. It's not supposed to indicate that's already said by the type. The original point of the notation is to indicate type information that isn't indicated by *just* the type. For example, a double could actually be a length, area, or volume, and it's sometimes useful to indicate that tersely as a prefix so that it's easy to visually do some basic dimensional analysis.

Quote:
I'm wondering what you're getting at with the #define vs const thing here.
I'm not sure either, since #define is a *terrible* way to express constants, because #define doesn't have any kind of scoping, and the compiler doesn't care either way.

Last edited by tuxdev; 06-30-2012 at 11:09 AM.
 
Old 06-30-2012, 11:17 AM   #24
worzel1968
Member
 
Registered: Mar 2011
Location: Melbourne, Australia
Distribution: fedora17_64, ubuntu
Posts: 44

Original Poster
Rep: Reputation: 2
Hi ntubski,

Quote:
For instance, if you decide you only want single float precision, now you have to go and change the name to fScaleFactorArray everywhere you use it.
Maybe I am spoilt, I use KDevelop, so Find & Replace isn't a problem for me - shows how selfish I am.

Quote:
I'm wondering what you're getting at with the #define vs const thing here.
I was thinking that something like DAYSINWEEK is always 7, so why not a #define - was that one of the basic uses of a #define ?

Code:
#define DAYSINWEEK 7
Quote:
I read "us" as short for United States, which is a bit confusing.
Fair enough, maybe I should change it. Ha that could start a whole new debate ....

I guess that everyone has their own style, and if coding is your work, then you will probably have to conform to your organisations coding standard. The main thing would be "To be consistent".

Thanks for your reply.
 
Old 06-30-2012, 11:21 AM   #25
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by ntubski View Post
I'm wondering what you're getting at with the #define vs const thing here.
I'm wondering who you were quoting that expressed that wrong opinion regarding #define

C and C++ don't have good support for compile time constants. I think that is a flaw in the language, but that is what we are stuck with. The keyword const does not make a variable into a compile time constant. It is still a variable.

To define compile time constant integers, I always use an enum. That is a bit counter intuitive (until you are used to it). But it does everything you want a compile time constant integer to do.

For other compile time constants, I have no good answer.
#define is a very bad answer because it has no reasonable scope limits. Once you work on any really big project, you will understand how important such scope limits are.

I have used kludges, such as an inline function (defined in an .hpp file) that returns the value of a function static variable (or for simpler types, just the value). That means you need constant_name() instead of constant_name every place you use it. Also you are making extra work for the compiler in a normal build and a distracting non inlined function in a debug build. Also, the optimizer sometimes fails to sort it all out, so you end up with worse code than a simple variable would give. But I don't know a better way, especially for the common case where the constant ought to be a member of a class and defined in the class .hpp file:

You wish a class definition could include something like
Code:
COMPILE_TIME_CONSTANT double unit = 1.0;
so instead kludge with something like:
Code:
inline static double unit() { return 1.0; }

Last edited by johnsfine; 06-30-2012 at 11:27 AM.
 
Old 06-30-2012, 11:25 AM   #26
worzel1968
Member
 
Registered: Mar 2011
Location: Melbourne, Australia
Distribution: fedora17_64, ubuntu
Posts: 44

Original Poster
Rep: Reputation: 2
Hi tuxdev, thanks for your reply.

We were replying at the same time, so I didn't see this bit.

Quote:
I'm not sure either, since #define is a *terrible* way to express constants, because #define doesn't have any kind of scoping, and the compiler doesn't care either way.
Another thing I have learnt today then. Press Save !!

I was going on what was in K&R "C Programming", I can see that this is not good in C (scoping) and in C++, because of scoping , inheritance etc

Cheers
 
Old 06-30-2012, 11:31 AM   #27
worzel1968
Member
 
Registered: Mar 2011
Location: Melbourne, Australia
Distribution: fedora17_64, ubuntu
Posts: 44

Original Poster
Rep: Reputation: 2
Hi Johnsfine, we were replying at the same time so I didn't see your post either for my last post.

Quote:
To define compile time constant integers, I always use an enum.
I like that - it's a great idea. I guess with the others I will just use const variables.

Thanks
 
Old 06-30-2012, 01:04 PM   #28
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by johnsfine View Post
...
For other compile time constants, I have no good answer.
#define is a very bad answer because it has no reasonable scope limits. Once you work on any really big project, you will understand how important such scope limits are.
...

When I was integrator of VLSI projects, we first defined naming conventions, something like this conceptually:

Code:
#define HIER1_HIER2_..._NAME_PROPER VALUE
.
 
Old 06-30-2012, 03:12 PM   #29
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
When I was integrator of VLSI projects, we first defined naming conventions, something like this conceptually:
That's a (not-that-great) solution to a problem that wouldn't have existed in the first place if you used something with decent scoping.

If you use a static const variable, and the compiler can determine you never ever try to take the address of it (so there's no pointer shenanigans to to try to ninja-modify the value), the compiler is legally allowed to treat it as a true constant (and almost all compilers do, when optimizations are turned on).
 
Old 06-30-2012, 03:18 PM   #30
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by worzel1968 View Post
Maybe I am spoilt, I use KDevelop, so Find & Replace isn't a problem for me - shows how selfish I am.
Sure, Find & Replace is easy, but it's still an extra manual step that can be forgotten/screwed up. Plus, if you are using version control (and you are, of course ) then your diff is now needlessly bigger and maybe harder to merge with other people's changes.

But again, what is the upside to Hungarian notation?
Quote:
I was thinking that something like DAYSINWEEK is always 7, so why not a #define - was that one of the basic uses of a #define ?
But you said using const for dEarthRadiusConst is better? What's the difference?


Quote:
Originally Posted by johnsfine
The keyword const does not make a variable into a compile time constant. It is still a variable.
Wouldn't a static const variable be inlinable by the compiler (I see tuxdev thinks so)? I haven't worked with floating point much, so the enum trick has always been enough for me.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this bad practice? jonnytabpni Linux - Security 8 05-26-2010 03:48 PM
Nesting while loops, bad practice? General Programming 17 05-05-2010 03:30 PM
iptables good practice - 2 questions ddaas Linux - Security 1 05-31-2005 07:09 AM
Is this good iptables practice ? michaelsanford Linux - Security 1 05-21-2005 09:32 PM
A good practice for compiling? Micro420 Mandriva 29 08-09-2004 03:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration