Author Topic: while loop in software  (Read 64848 times)

0 Members and 1 Guest are viewing this topic.

Offline jeremy

  • Super Contributor
  • ***
  • Posts: 1079
  • Country: au
Re: while loop in software
« Reply #25 on: December 17, 2014, 11:32:32 am »
...I also very often use
 
Code: [Select]
while(chip_is_busy);...
I would never do this. I would never let code like that past a code inspection.
You need to have a timeout in order to prevent the possibility that a hardware failure or glitch can bring the software to a grinding halt.

To be fair, I was talking about using this code in debugging so that you could attach a debugger...
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3231
  • Country: gb
Re: while loop in software
« Reply #26 on: December 17, 2014, 11:55:15 am »
...I also very often use
 
Code: [Select]
while(chip_is_busy);...
I would never do this. I would never let code like that past a code inspection.
You need to have a timeout in order to prevent the possibility that a hardware failure or glitch can bring the software to a grinding halt.

From that tiny bit of code, how can you tell that there isn't a separate thread that handles a timeout condition (e.g. an interrupt) ?
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: while loop in software
« Reply #27 on: December 17, 2014, 11:57:51 am »
Quote
Code should be unambiguous clearly written most in short one liners and as much comment as is needed to understand the code 3 years later without RTFD.

Agreed. A piece of code that's not readable is not a maintainable thus not usable. Such code can be very dangerous and is a cost item, rather than investment in any organization.

The same goes with

Code: [Select]
while (condition is true);
================================
https://dannyelectronics.wordpress.com/
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6189
  • Country: us
Re: while loop in software
« Reply #28 on: December 17, 2014, 12:02:30 pm »
for ( i=0, ptr=array, bError=FALSE; i<10 && !bError; i++, ptr++)
Programming like this makes me want to kick the colleague that wrote it.

+1

Code should be simple and straight forward.
 

Offline ptricks

  • Frequent Contributor
  • **
  • Posts: 671
  • Country: us
Re: while loop in software
« Reply #29 on: December 17, 2014, 12:19:28 pm »
I use
while(1);

quite a bit when I want to stop a program from moving to the next step but don't have debug options.
 

Offline grumpydoc

  • Super Contributor
  • ***
  • Posts: 2905
  • Country: gb
Re: while loop in software
« Reply #30 on: December 17, 2014, 12:28:32 pm »
for ( i=0, ptr=array, bError=FALSE; i<10 && !bError; i++, ptr++)
Programming like this makes me want to kick the colleague that wrote it.
Code should be unambiguous clearly written most in short one liners and as much comment as is needed to understand the code 3 years later without RTFD.
I have to say I don't have too much of a problem with that for loop, it's a little full but easy to see what's going on.

I do have a bit of an issue with the mixture of Hungarian and non-Hungarian notation though. I'm no big fan of Hungarian but if you are going to use it do so consistently.
 

Offline Rigby

  • Super Contributor
  • ***
  • Posts: 1476
  • Country: us
  • Learning, very new at this. Righteous Asshole, too
Re: while loop in software
« Reply #31 on: December 17, 2014, 03:27:14 pm »
I felt the same way when people became afraid of pointers. It was long time ago and special languages has been developed for said people, but even today I have an occasional conversation with a fan of "pointer-less"  design of a C program.

Well, as someone who wasn't force-fed pointers early in his programming career, I don't see the need for the damn things.  I understand their history and how they were used to create and access arrays before arrays became a formal structure, and so on.  I know what they do, what they don't do, and why people use them.

I still don't see the need for the effing things.

I think some people just really like to put little stars by all their variable names.
 

Offline Kjelt

  • Super Contributor
  • ***
  • Posts: 6458
  • Country: nl
Re: while loop in software
« Reply #32 on: December 17, 2014, 04:16:28 pm »
Well, as someone who wasn't force-fed pointers early in his programming career, I don't see the need for the damn things.  I understand their history and how they were used to create and access arrays before arrays became a formal structure, and so on.  I know what they do, what they don't do, and why people use them.
I still don't see the need for the effing things.
I wonder how you will write a messageprotocol handler without using pointers or any large structure with multiple data stuffed behind eachother.
All you can do is handle elementary datatypes.
I will go as far as say that you can not write a serious c program without any pointers.
 

Offline jaxbird

  • Frequent Contributor
  • **
  • Posts: 778
  • Country: 00
Re: while loop in software
« Reply #33 on: December 17, 2014, 04:54:40 pm »
for ( i=0, ptr=array, bError=FALSE; i<10 && !bError; i++, ptr++)

If I was the reviewer of that code it would never make it into production or even source control. Just because you can, does not mean it's a good idea to rape a for loop.

I even demand changing simple thing like:

if (0 < x)

to

if (x > 0)

just for the sake of general easy readability. It's all about people maintaining the code in the future not wasting time because it was written by someone thinking it was cool to write in that style.

Analog Discovery Projects: http://www.thestuffmade.com
Youtube random project videos: https://www.youtube.com/user/TheStuffMade
 

Online Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11518
  • Country: my
  • reassessing directives...
Re: while loop in software
« Reply #34 on: December 17, 2014, 05:54:54 pm »
I still don't see the need for the effing things.
dynamically created memory, polymorphism to name a few...
I think some people just really like to put little stars by all their variable names.
and performance for eternity. well star is better than brackets isnt it?
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline jaxbird

  • Frequent Contributor
  • **
  • Posts: 778
  • Country: 00
Re: while loop in software
« Reply #35 on: December 17, 2014, 05:55:12 pm »
Well, as someone who wasn't force-fed pointers early in his programming career, I don't see the need for the damn things.  I understand their history and how they were used to create and access arrays before arrays became a formal structure, and so on.  I know what they do, what they don't do, and why people use them.

I still don't see the need for the effing things.

I think some people just really like to put little stars by all their variable names.


Pointers can be confusing, especially when you have pointers to arrays of pointers pointing to object containing pointers or linked lists of pointers etc.

But explicit pointers in languages like c and c++ do give you more control (and understanding of the actual implementation) than implicit pointers in languages like c# where you need to differentiate between value and reference types. E.g. trying to expose a writable variable in a structure in c# through a class property is not possible. That can be very confusing to a dev lacking understanding of the pointer concepts.

Analog Discovery Projects: http://www.thestuffmade.com
Youtube random project videos: https://www.youtube.com/user/TheStuffMade
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: while loop in software
« Reply #36 on: December 17, 2014, 06:01:42 pm »
I even demand changing simple thing like:

if (0 < x)

to

if (x > 0)

just for the sake of general easy readability.

Who the hell can't easily read (0 < x)? Kindergartners? You need better colleagues...
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline jaxbird

  • Frequent Contributor
  • **
  • Posts: 778
  • Country: 00
Re: while loop in software
« Reply #37 on: December 17, 2014, 06:11:43 pm »
Who the hell can't easily read (0 < x)? Kindergartners? You need better colleagues...

It's all about standardizing for readability. Why would you write (NULL != MyShittyObject)? It's not like we are questioning the value of NULL or 0, so it belongs on the other side, simple as that.

Attitudes like that is the reason you will not get a Christmas bonus  8)

Analog Discovery Projects: http://www.thestuffmade.com
Youtube random project videos: https://www.youtube.com/user/TheStuffMade
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: while loop in software
« Reply #38 on: December 17, 2014, 06:20:23 pm »
Why would you write (NULL != MyShittyObject)?

Habit, from writing (NULL == MyShittyObject). I've heard rather convincing arguments in favor of preferring that approach so that an accidental (NULL = MyShittyObject) becomes invalid code.

Looks quite readable to me, too. Perhaps some people need to spend their Christmas bonuses on reading glasses? >:D
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline jaxbird

  • Frequent Contributor
  • **
  • Posts: 778
  • Country: 00
Re: while loop in software
« Reply #39 on: December 17, 2014, 06:31:39 pm »
Habit, from writing (NULL == MyShittyObject). I've heard rather convincing arguments in favor of preferring that approach so that an accidental (NULL = MyShittyObject) becomes invalid code.

Looks quite readable to me, too. Perhaps some people need to spend their Christmas bonuses on reading glasses? >:D

Let's just say you'd have a tough time in my team :) I don't have much patience for "enlightened" individuals with arguments like: because reasons.

Working against the general consensus never brought good results unless you have convincing arguments why your style is superior.

Analog Discovery Projects: http://www.thestuffmade.com
Youtube random project videos: https://www.youtube.com/user/TheStuffMade
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: while loop in software
« Reply #40 on: December 17, 2014, 06:38:39 pm »
Let's just say you'd have a tough time in my team :)

A team whose leader quibbles about things like which side of the comparison operator the constant goes on? I daresay I would. But that sort of ridiculous, overreaching micromanagement is rampant in software anyway - I'm quite glad to be out of it. :phew:

Working against the general consensus

You've an amusing understanding of "consensus".
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4064
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: while loop in software
« Reply #41 on: December 17, 2014, 06:45:40 pm »
Who the hell can't easily read (0 < x)? Kindergartners? You need better colleagues...
It becomes much harder when you have (0 < someFunctionWithArguments(&argument, with, value, DEFINE))
But the psuedo doesn't make sense. You want the result of this() to be higher than 0. Not zero to be lower than the result of this().
It isn't rare to have style rules. http://www.chibios.org/dokuwiki/doku.php?id=chibios:guides:style_guide
 

Offline jaxbird

  • Frequent Contributor
  • **
  • Posts: 778
  • Country: 00
Re: while loop in software
« Reply #42 on: December 17, 2014, 06:46:22 pm »
A team whose leader quibbles about things like which side of the comparison operator the constant goes on? I daresay I would. But that sort of ridiculous, overreaching micromanagement is rampant in software anyway - I'm quite glad to be out of it. :phew:

You've an amusing understanding of "consensus".

Clearly you've never spent time in a shiproom evaluating and triaging stupid bugs by "enlightened" developers trying to get a product ready to ship.

Analog Discovery Projects: http://www.thestuffmade.com
Youtube random project videos: https://www.youtube.com/user/TheStuffMade
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: while loop in software
« Reply #43 on: December 17, 2014, 06:55:30 pm »
It becomes much harder when you have (0 < someFunctionWithArguments(&argument, with, value, DEFINE))

...it does? Funny, I just picture a number line. 0 is here... and sFWA(...) is here. Rather in the order they were written, actually.

Flip it around and I have to picture that line backwards. Makes me have to stop and reconsider the code in a different way.

Face it, different people think in different ways, and you two just happen to be arrogant enough to think your way is how everybody thinks. Some people will find your way easier, and others will find it harder. All you're doing is choosing a half of your devs to make uncomfortable. I wonder how much crap code that makes them churn out...

Quote
You want the result of this() to be higher than 0. Not zero to be lower than the result of this().

The two are exactly equivalent - I want both!

Quote
It isn't rare to have style rules. http://www.chibios.org/dokuwiki/doku.php?id=chibios:guides:style_guide

It isn't rare for those style rules to be batshit insane either. Like this:

Quote
Very important types are allowed to be use camel case. Less important types must be all lower case and suffixed by a “_t”.

Very important types? What the hell are those? The ones with an MBA?

Clearly you've never spent time in a shiproom evaluating and triaging stupid bugs by "enlightened" developers trying to get a product ready to ship.

And you think demanding they put the constant on the "correct" side of the comparison operator will improve them? They're just shitty devs with big egos! You'll never improve them.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline baljemmett

  • Supporter
  • ****
  • Posts: 665
  • Country: gb
Re: while loop in software
« Reply #44 on: December 17, 2014, 07:04:16 pm »
Habit, from writing (NULL == MyShittyObject). I've heard rather convincing arguments in favor of preferring that approach so that an accidental (NULL = MyShittyObject) becomes invalid code.

Let's just say you'd have a tough time in my team :) I don't have much patience for "enlightened" individuals with arguments like: because reasons.

Except he gave you a reason...  Of course, you can think that said reason isn't good enough for you, and your style guide is free to say whatever it wants, but to sarcastically characterise someone else's justified argument as an "enlightened" equivalent of "because reasons" puts you squarely in the category you claim not to have time for.
 

Offline jaxbird

  • Frequent Contributor
  • **
  • Posts: 778
  • Country: 00
Re: while loop in software
« Reply #45 on: December 17, 2014, 07:11:23 pm »
Let's leave it here I don't think we are getting anywhere.

But if you ever get a real job with a large corp, you might be surprised to find there are strict standards and unless you comply, your work will be rejected.

I do believe you can see the benefits and logic in adhering to a standardized style opposed to everyone exercising individual styles.

Analog Discovery Projects: http://www.thestuffmade.com
Youtube random project videos: https://www.youtube.com/user/TheStuffMade
 

Offline Rigby

  • Super Contributor
  • ***
  • Posts: 1476
  • Country: us
  • Learning, very new at this. Righteous Asshole, too
Re: while loop in software
« Reply #46 on: December 17, 2014, 07:46:30 pm »
Well, as someone who wasn't force-fed pointers early in his programming career, I don't see the need for the damn things.  I understand their history and how they were used to create and access arrays before arrays became a formal structure, and so on.  I know what they do, what they don't do, and why people use them.
I still don't see the need for the effing things.
I wonder how you will write a messageprotocol handler without using pointers or any large structure with multiple data stuffed behind eachother.
All you can do is handle elementary datatypes.
I will go as far as say that you can not write a serious c program without any pointers.

Your last sentence sums it up, for me.  Except, I read it as "avoid C whenever possible."  I misspoke a bit; I don't mean "write C without pointers," I mean "DON'T WRITE USING C."  Use C++ at least.  It's a lot less stupid than C, you can get object orientation and provide SOME structure to your application.

I'm NOT advocating ANY other language, I'm not bashing C in favor of [insert language], I just hate pointers.  I wasn't bathed in pointers when I started programming, and therefore I don't see a need for them.  I truly don't.  I wasn't bathed in religion when I was young, either, and now I view pretty much all religion as huge wastes of time.

You're free to disagree with me, if you like.  I'm not going to tell you you're wrong, because I don't know that you're wrong, or that I'm right.  It's just what I think.

I still don't see the need for the effing things.
dynamically created memory, polymorphism to name a few...
I think some people just really like to put little stars by all their variable names.
and performance for eternity. well star is better than brackets isnt it?

There are performant languages that don't use pointers.  I'm not sure what you mean when you say "brackets" because that term seems to vary between countries, but I will say that the only time I use * or & voluntarily, they are used as operators and not pointers.
 

Offline Kjelt

  • Super Contributor
  • ***
  • Posts: 6458
  • Country: nl
Re: while loop in software
« Reply #47 on: December 17, 2014, 07:53:10 pm »
Not important who is right or wrong thats too black/white but I can not see even writing C++ without pointers, i mean do you use structs? Will you pass them by value or by reference in a function? If by value please do not write embedded code it will eat your stack for lunch if that computes ????
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5313
  • Country: gb
Re: while loop in software
« Reply #48 on: December 17, 2014, 08:07:55 pm »
Hmm, mighty glad I never worked for a large corp (other than once) that micromanaged coding to the nth degree like that. As I think I alluded to earlier, the one i did was a very large retail bank did indeed have a stipulation for only one operation in each statement, this included not being able to use

a=*p++;

It would have to be

a=*p;
p++;

However, bearing in mind the quality of the programmers they were using, basically new graduates dropped in there by a very large accountant with an IT consultancy bolted on the side, it needed to be simple.

My thoughts are that if everyone has to write noddy code for the lowest common denominator, it's probably time to improve that lowest common denominator. Equally, everyone must be mindful of whether they're writing write only code.

Regarding not using pointers, I'd say that it's probably more to do with not being comfortable with the concept rather than being bad. In embedded stuff and driver programming in particular, where performance is everything, pointers bring you closer to the machine code. Passing data structures by value ain't going to win you any races.

About the mix of Hungarian and non-Hungarian, somewhat ironically I was trying to give a clear example without a ton of other code. From the comments, like it or not it appears it succeeded in doing that, in fact it seems that everyone knew what was being done in at statement. Although I wouldn't go to those extremes in practice, it seems it was clear in what was being attempted. BTW, no, I don't mix, I'm pretty anal about the style and consistency that I use, and like it or not I was brought up on Hungarian and have no intention of changing now!

Back when I did real work, I wrote a spec for writing SQL statements after a number of schoolboy related coding problems. Sadly we'd had some ivory tower idiot recruited and parachuted in who was an academic with no real world experience, and demanded that everything was set-based without any procedural stuff. A noble desire. But have you ever tried to understand a ten page SQL statement with 250 tables in it? Interestingly, I was brought in when the join ended up breaking the 256 table join limit of the system when an underlying view was altered. Spent a week disecting the query and related views and re-writing it from scratch.
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6189
  • Country: us
Re: while loop in software
« Reply #49 on: December 17, 2014, 08:11:07 pm »
Not important who is right or wrong thats too black/white but I can not see even writing C++ without pointers, i mean do you use structs? Will you pass them by value or by reference in a function? If by value please do not write embedded code it will eat your stack for lunch if that computes ????

With c++ you don't need a pointer to pass by reference.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf