Pages: [1] 2   Go Down
Author Topic: Is a short tidy code neccessary ?  (Read 2040 times)
0 Members and 1 Guest are viewing this topic.
Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 !   smiley   )

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..
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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..
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Holland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 439
Arduino likes cookies too
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6653
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
« Last Edit: October 24, 2010, 01:05:54 am by John_Smith » Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7175
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Central Europe
Offline Offline
Edison Member
*
Karma: 7
Posts: 1220
Use the Source, Luke.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-wink

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.
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Central Europe
Offline Offline
Edison Member
*
Karma: 7
Posts: 1220
Use the Source, Luke.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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
Logged

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I will try that, and the flowchart, thanks all
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Think of flowcharting like stabiliser wheels on a bicycle.
You'll need them only for a short time.
Logged

Per Arduino ad Astra

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

or like nappies, when you are very young or very old  !
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Pages: [1] 2   Go Up
Jump to: