Unix/Linux Go Back    

What is on Your Mind? Come inside and relax a while. Discuss whatever is on your mind. New members can introduce themselves. However, technical Q&A should happen in our other forums.

Naive coding...

What is on Your Mind?

discussion, software development

Thread Tools Search this Thread Display Modes
Old Unix and Linux 02-22-2017   -   Original Discussion by wisecracker
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Join Date: May 2012
Last Activity: 20 June 2018, 9:09 PM EDT
Location: Simplicity
Posts: 4,122
Thanks: 357
Thanked 1,401 Times in 1,258 Posts
Two corrections:

case "$STR" in 
"") echo "Blank" ;;
*[!0-9]*)  echo "Contains non-numeric" ;;
*) echo "Valid" ;;

The Following User Says Thank You to MadeInGermany For This Useful Post:
Corona688 (02-22-2017)
Sponsored Links
Old Unix and Linux 03-05-2017   -   Original Discussion by wisecracker
bakunin's Unix or Linux Image
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
Join Date: May 2005
Last Activity: 20 June 2018, 6:21 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,777
Thanks: 112
Thanked 1,693 Times in 1,244 Posts
A few observations about programming habits and programming in general:

I think the real difference between naive and non-naive (clever) programming is not so much to make (or make not) use of a certain language construct but the employment of certain algorithms. The example of Corona688 shows that very clearly. The difference in simplicity (and perhaps speed) comes not from using some "clever language feature" a non-expert might not know about, but from the ability to look at the problem from a different angle and draw the right conclusion.

Here is a story from my own practice: I once worked for a development team who created some database application. At one time they had trouble with a daily import routine because it ran approximately 32 hours. (For the non-experts: standard days come with only 24 of them. ;-) ) The import script worked like that: first a huge SQL-statement to create some table. This table was fed into a loop where something was changed, then the whole was fed back into the DB. The script looked like this (only the structure):

#! /bin/ksh

db2sql "...some 300 lines of SQL here...." |\
while read record ; do
     newrecord=$(echo $record | sed '....some changing of the record here....')
     db2sql "... import record from $newrecord here..."

exit 0

There is nothing "logically wrong" here and when you test it with 5 records it is probably as fast as any other solution. Doing it with some ten-million-records table, though, will reveal that calling sed one time for every record is slightly slower (by several orders of magnitude) than to call it once in a pipeline:

db2sql "...some 300 lines of SQL here...." |\
sed '....some changing of the records here....' |\
while read newrecord ; do
     db2sql "... import record from $newrecord here..."

So, this was my first instinctive comment and (we tested it later) it would have brought down the processing time by ~3.5 hours. But i worked on this together with the DBA and he observed inside the 300-line-SQL-monster (which i had ignored, being an SQL-ignorant) that most of it was effectively creating a left outer join of the whole database on itself and then dropping 99% of it, So, after selecting what really was to be selected - and finding out that the changes could be done inside instead of using the shell and an external program to do it - - we arrived at:

db2sql "select for update ...some 20 lines of SQL here...."

which took - about 5 seconds. All the while management was calling for bigger hardware to "meet increasing needs" instead of just firing this incompetent click-boy who confused some graphic representation of the DB within a tool (Informatica) with reality.

So, what is the differrence between clever and naive programming? It is about the same difference as 32 hours and 5 seconds.

My two cents.

The Following 4 Users Say Thank You to bakunin For This Useful Post:
Corona688 (03-06-2017), Don Cragun (03-05-2017), ni2 (04-08-2017), zaxxon (03-06-2017)
Sponsored Links
Old Unix and Linux 05-24-2017   -   Original Discussion by wisecracker
cb88's Unix or Linux Image
cb88 cb88 is offline
Registered User
Join Date: May 2017
Last Activity: 26 August 2017, 11:32 PM EDT
Posts: 13
Thanks: 0
Thanked 1 Time in 1 Post
I wrote a rather naive snake implementation... it worked by each position being logged in an array... so each time you move a for loop scanned the array for collisions it would take a little longer. The target machine was a 50Mhz Sparcstation LX so... I thought nothing of it when writing it but it became obvious when running the game. After about 100 indexes it would get so slow you could just about walk away and get coffee, perhaps I exaggerate a bit but still! Rather than using an array of previous positions, I probably should have allocated an array the size of the play area and marked them traversed or not.
Old Unix and Linux 05-25-2017   -   Original Discussion by wisecracker
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
Join Date: Aug 2005
Last Activity: 13 June 2018, 6:37 PM EDT
Location: Saskatchewan
Posts: 22,696
Thanks: 1,183
Thanked 4,334 Times in 3,995 Posts
Why was it looped? If you know where the snake is and where it's going, there's only one cell to check.
The Following User Says Thank You to Corona688 For This Useful Post:
MadeInGermany (06-08-2017)
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help with coding crazydude80 Shell Programming and Scripting 1 03-26-2012 06:25 PM
Naive Bayes gizmo87 Homework & Coursework Questions 0 11-15-2011 06:56 PM
can I use this coding w33man UNIX for Advanced & Expert Users 6 03-02-2004 07:49 AM

All times are GMT -4. The time now is 10:40 PM.