Go Down

Topic: Is a short tidy code neccessary ? (Read 2599 times) previous topic - next topic

Boffin1

By the way, is there any reason why a sketch has to be tidied up if it is working well?

I know there is a lot of pride in keeping it short ( my 2 axis doming machine has only 13 lines ) but I am in a hurry for the current project, and I can have a pcb made and have the thing running with its ridiculously long code,  and tidy it up later when I am up to date, for subsequent units. ( I couldn't do that with my CMOS prototypes !   I love this Arduino)

I would be too embarrassed to even post the code on the forum, ( and these pages aren't long enough for all the lines !   :)   )

For instance every 7x5 display at the moment has its own look-up table for the whole alphabet for the LEDs !

I am updating some of my CMOS logic products to Arduino now, I suppose later I will use the grown up version of the chip , but while it works that's fine by me, and it will save me a fortune in pc boards, both space wise, and also for all the wasted prototypes that I could paper my workshop walls with..
With my mobile phone I can call people and talk to them -  how smart can you get ?

Osgeld

short tidy code can be a double edged sword, make it too short n tidy and it becomes difficult to see whats going on

make it too long winded it becomes burdensome to read debug and sometime execute
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Boffin1

OK that's cool, its definitely easy to see whats going on, although scrolling through it makes me rather giddy !

It will do for now, and I can set myself a challenge to see how neat I can make it when I have some time..
With my mobile phone I can call people and talk to them -  how smart can you get ?

Imahilus

Clean and tidy can be a double edged sword, true.
Unclean, bloated code.. however, is a hiltless blade, you'll nearly always hurt yourself with it. Either because it is hard to get help, since it is so bloated, or because it contains a lot of duplicate code.

As with a lot of things, theres a sweet spot in the middle.

westfw

code should be like your desk.  Neat enough for you to be able to find the things you're looking for without much trouble.
Exactly how neat this is varies quite a bit from person to person.
It really sucks to go back to an old program not not be able to understand what you were doing...

Joel Maslak

Coming from a business point of view, if the code satisfies all requirements and can be maintained easily, I would refrain from turning it into something that would be difficult to maintain easily.

That said, if I was sharing my code with others, a bit of professional pride would require me to make sure it was *clear* as to what I was doing.  If I thought I'd need to maintain it, the same goes.

As for optimization, if I have 32K of program memory, I don't mind using 31K of it - so long as I can fit everything I need into the RAM.

mowcius

Quote
code should be like your desk.  Neat enough for you to be able to find the things you're looking for without much trouble.
Exactly how neat this is varies quite a bit from person to person.
It really sucks to go back to an old program not not be able to understand what you were doing...

Good thing my code's neater than my desk  ;D
But only slightly...

Mowcius

Boffin1

#7
Oct 24, 2010, 07:48 am Last Edit: Oct 24, 2010, 08:05 am by John_Smith Reason: 1
now and again I hold a box at the end of the desk/bench and "scroll" everything off.  A  control +T tidy up function would be great for the desk..

My mile long code, by the way, only uses half the memory .... so as I am using Shiftout to 10 display characters, and they all come on in a random pattern, I might as well have my brandname showing when it is switched on, which I think I can do in the setup...
With my mobile phone I can call people and talk to them -  how smart can you get ?

liudr

Make sure you do a flow chart some time during your clean-up, earlier better then later. Make some optimizations. I wish I could remember my stuff, but my current project is already over 2,500 lines even after quite a bit of optimization. Each time I go back to it I had to spend time figuring out what it going on. I'm glad I have one flow chart for a large part of the program. If I need to go back to that part I'll spend less time reviewing.

Korman

Quote
Coming from a business point of view, if the code satisfies all requirements and can be maintained easily, I would refrain from turning it into something that would be difficult to maintain easily.


Exactly. And people asking the question about how clean and tidy usually have the part about satisfying requirements done but have big doubts about the easy maintenance part. Having a clean and tidy to code with meaningful comments is absolutely necessary to be able to maintain it. I've suffered so many times from messy code in my career that I can tell, no other way works. Code is only finished once you wouldn't be ashamed of if it was published in the current form.

About code being short, that's usually not a goal to strive for, but it's often just an automatic consequence. If you remove all the cruft and make things easy to maintain, a lot of duplicate code disappears and code will be structured to make things more compact. However, beyond that point, shortening the code further should only be done if you really are running out of space, which is nearly never the case.

So as a general rule, it's absolutely necessary to have clean and tidy code. But don't make code more confuse just to make it a little smaller.

Korman

Boffin1

I was wondering what happened to flow charts, I remember we used to do them when I was messing around with basic in the 80s, but I didnt want to mention them as I saw what happened to the guy who dared to mention gusub  ;)

I downloaded some Yenka program that someone suggested using to (re)learn the flowchart shapes etc, I must have a look, but basically its just getting used to how to picture the instructions, when I was used to the loops and gotos of the basic.

I think I will leave my code as it is, but go through and comment quite heavily,  what seems obvious now might seem crazy in a months time.
With my mobile phone I can call people and talk to them -  how smart can you get ?

Korman

Quote
Make sure you do a flow chart some time during your clean-up, earlier better then later. Make some optimizations.


Liudr,

no to both things.

Optimisations should be done only on code that has performance problems. Don't waste time on optimisations that work well enough. Optimisations make code more complex. Don't confuse optimisation with throwing out the garbage or repairing bad code.

As to the flow chart, that tool was very popular 30 years ago, but has proven to have many limitations and it doesn't work very well for many purposes. A flow chart usually works best on linear code, but that is just as easily documented by putting section headers with in your code. Stuff like:
Code: [Select]
// Set up the ports.
// The ports are set up in the reverse way to prevent the car
// from backing into the garage wall.
...
... code ...
...
// Check where we are.
// Check near field reception first to prevent acting on imprecise
// long range tactical scanner data.
...
... more code ...
...


In most situations, a clear and easy to read code documentation is preferable to a flow chart. The important things to document is to document goals this block of code achieves and why some non-obvious implementation has been chosen over a more common on. Just by reading  the comments you should get a good idea what your code does and what important design decision you have made.

Korman

Boffin1

I will try that, and the flowchart, thanks all
With my mobile phone I can call people and talk to them -  how smart can you get ?

Groove

Think of flowcharting like stabiliser wheels on a bicycle.
You'll need them only for a short time.
Per Arduino ad Astra

Boffin1

or like nappies, when you are very young or very old  !
With my mobile phone I can call people and talk to them -  how smart can you get ?

Go Up