I came across this phrase a couple of weeks ago and it has made me decide to set off a discussion.
I had never heard of it before but I did some research and discovered that I probably fall into this category.
My phrase is: "I code to work, not work to code!", so therefore I guess when viewing any of my code, pros' must think how primitive some of it looks.
So, what do you guys who code for a living think when you see some attempts of others solving problems and use _brute_force_methods_ for example to solve their coding problems when there are probably more elegant solutions?
Also what is wrong with a naive solution that works for the coder although it may not be anywhere near as fast as another method more obvious to someone else?
The plotting and drawing routines for example on AudioScope can't seem to be done any other way except by _brute_force_...
From the definition of naive, naive programming isn't necessarily bad programming; it just shows that the person writing the code lacks experience, judgement, or wisdom. In many cases naive programming produces code that is just slower than what a more experienced programmer might right, but sometimes the naive code fails miserably.
Suppose you write code to get the month, day, and year to put in a report that you're about to generate and to use as part of the filename for that report. That could naively be done with:
A more experienced programmer might do the same thing with:
If you run this program every day at noon, will the label and filename variables be set differently with these code segments? Almost certainly, no.
If you run this program every day in a cron job scheduled to run at one minute before midnight what values might you get for those variables on December 31, 2016 when lots of long-running month-end and year-end reports are running at the same time? With the naive code you could get any one of the following four sets of values for those variables:
Note that the middle two of those will overwrite daily reports from the start of the previous month or year and the last one will be overwritten by the report written at the end of the day on January 1st (unless the mistake is caught sometime during the day on New Year's day). With the more experienced programmer's code, the last three will never happen, but you might get a diagnostic message instead of the wanted report if the code is started late. When the naive programmer is bitten by this, (s)he learns from experience that the code needs to be made more robust and becomes less naive (and learns that you shouldn't schedule cron jobs to run at one minute before midnight if the code that will be run depends on being run before midnight).
Note that naive code can sometimes run faster than well written code, but fail miserably when unexpected (and unchecked) events occur.
Most of the code we write in this forum assumes that data is in the specified format and skips a lot of error checking that should appear in production code. Many of us should be more careful to point out that the sample code we suggest here should be strengthened with appropriate error checking if the code is going to be used in a production environment.
This User Gave Thanks to Don Cragun For This Post:
I use "date with eval" instead of "unsafe" here docs (that denote ugly tmp files).
--
You code like you eat.
Fast food makes you fat, and you risk a heart attack.
This User Gave Thanks to MadeInGermany For This Post:
I have this insane distrust of compilers and interpreters.
So I do what could be called naive coding in most langauages that I know well enough because of this distrust.
This is one example of my naive code and IS actually inside AudioScope.sh.
Derivatives of this have never failed under normal conditions on the langauges I have used so it seems idiot proof.
Would professionals like yourselvs consider this puerile coding?
I have no idea if it is possible to have buffer overrun in bash scripting from a 'read', (input), statement.
On searching a few months ago I found this and I do use this method below on small apps to do the same job but I still use longhand derivatives of the above for more serious stuff.
This seems bullet proof but due to my insane distrust of compilers and interpreters I am not sure...
Last edited by wisecracker; 02-19-2017 at 09:17 AM..
Reason: Change LIMIT to 10.
I don't like the assumption of ASCII codes.
Without it becomes better readable.
Also you are missing a logical OR
Then you can optimize even the OR
And then it's only a small step to realize that perhaps the whole loop can be replaced.
I came across this phrase a couple of weeks ago and it has made me decide to set off a discussion.
I had never heard of it before but I did some research and discovered that I probably fall into this category.
My phrase is: "I code to work, not work to code!", so therefore I guess when viewing any of my code, pros' must think how primitive some of it looks.
So, what do you guys who code for a living think when you see some attempts of others solving problems and use _brute_force_methods_ for example to solve their coding problems when there are probably more elegant solutions?
For one-off scripts and things you only do once, nothing wrong with it. You're writing code to solve a problem, and if the problem's solved to your own satisfaction without severe side effects, who cares?
The problem is, coding that way, with no effort to learn further, builds bad habits. Naive methods are not applicable to all situations, or even most situations. If you only have the heavy-duty sledgehammer in your toolbox, you'll break all the smaller nails.
Quote:
Also what is wrong with a naive solution that works for the coder although it may not be anywhere near as fast as another method more obvious to someone else?
You're building audioscope.sh as a teaching tool. You've eschewed many modern features because you consider them hard to read.
I think reducing it to a quarter of its length, could make it easier to read.
Quote:
The plotting and drawing routines for example on AudioScope can't seem to be done any other way except by _brute_force_...
Saying so doesn't make it so. You've fought tooth-and-claw against anyone who tries to optimize it.
I have this insane distrust of compilers and interpreters.
So I do what could be called naive coding in most langauages that I know well enough because of this distrust.
This is one example of my naive code and IS actually inside AudioScope.sh.
Derivatives of this have never failed under normal conditions on the langauges I have used so it seems idiot proof.
Would professionals like yourselvs consider this puerile coding?
That's just about the most difficult way possible to solve the problem. I only resort to it when the language features just can't handle it (i.e. needing to build a recursive parser from scratch).
When you find yourself doing this for trivial things, you're definitely overthinking it. Try inverting the problem. What if you looked for exactly one non-numeric character? You only need to find one to prove the string's bad, and if you can't... fait accompli.
One way:
This is portable across all bourne shells. In BASH, you could reduce it to a single statement.
My company has an in house instant messaging system (like WhatsApp) where users can communicate with each other. I currently have code to email me certain items from my Sparc machine running SunOS 5.10. I want what I am emailing myself to now instant message me. The team that created the messenger... (5 Replies)
HI,
Can some one guide me how to make changes to the script below so that it can load the history of a program to IT server ?
Format of data:
YYYYMMDD065959.dsk.log
YYYYMMDD235959.dsk.log
currently both are loaded together. Need to separate them as above format.
Thanks in advance.
... (2 Replies)
HI,
Can some one guide me how to make changes to the script below so that it can load the history of a program to IT server ?
Format of data:
YYYYMMDD065959.dsk.log
YYYYMMDD235959.dsk.log
currently both are loaded together. Need to separate them as above format.
Thanks in advance.
... (1 Reply)
Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
I have to write a program in Unix to do the following.Given a phrase like george hates john, by... (0 Replies)
Hi All,
I am new to pro*C. I have a select statement as
select a.ename,a.sal,a.empno from emp where &n=(select count(distinct(b.sal)) from emp b where a.sal<=b.sal
for this query I have to write a pro*C program. So can you please send me the complete code. Then I will foloow the same... (1 Reply)
I would like to start coding on my mac, but I'm getting an error when I attempt to execute my script
-bash : testscript: command not found
I have verified that the #! line points to the correct directory.
If you have some insight it would be greatly appreciated!
- D (1 Reply)
I apologise because I had pasted this question in the newbies forum first (because i am a bit of a newbie) but thought it might be better suited in here
if i have to sepearate parameters can I use this syntax especially the or part (||) and is this correct
if (6 Replies)