[1+1=2]

OneAndOneIs2

« Now is the timeThe whole Myth »

Tue, Jan 02, 2007

[Icon][Icon]Bleah

• Post categories: Omni, Programming

Back to K&R, and I encounter the next excercise: Write a loop to replace the trusty "Getline" function that doesn't use && or || - the "AND" and "NOT" operators

I almost didn't do this one: It basically requires you to take an elegant and simple bit of code, and replace it with something horrible. It would have been slightly better if I knew about "break" but I don't yet (by the book).

Still, I did it: Too stubborn to leave it alone. Even so, I still don't like it. Horrible bit of code.

Follow up:


{
        int i, c;

        for (i = c = 0; c != '\n' && (c = getchar()) != EOF && i < max-1; ++i)
                string[i] = c;
        string[i] = '\0';
        return i;
}

That was the original bit of code. The "for" had to be replaced with a loop that didn't require && or ||, and so:

{
        int i, c, cont;

        i = c = cont = 0;
        while (!cont)
        {
                if (c != '\n')
                {
                        if ((c=getchar()) != EOF)
                        {
                                if (i < max-1)
                                {
                                        string[i] =c;
                                        ++i;
                                }
                                else
                                        cont = 1;
                        }
                        else
                                cont = 1;
                }
                else
                        cont = 1;
        }
        string[i] = '\0';
        return i;
}

4 comments

Erez Schatz
Comment from: Erez Schatz [Visitor]
You're, again, confusing elegance with shorthand. One of my biggest quirks with the world of pure OO programming is that too many times functions are called which creates a huge overload. Being in a state of mind that "everything should go in one line" is exactly the trapping of this overhead. It's true that runtime performance for both examples is the same for the code you wrote, however, keeping an open mind will allow you to avoid OO pitfalls in the future. Also, there is little difference between "manual breaks" such as your cont = 1 and "break", other than cont = 1 being a natural satisfying of the loop condition, and break terminating a loop abruptly (you can say I'm not a fan of loop exits).
Point is, which I've mentioned a couple of times in comments to your coding examples is that elegance isn't measured by number of code lines. One of the first things every CS student notices is that the more "elegant" the algorithm, the bigger and nastier it is to implement. In most cases, a neat, clean, efficient algorithm takes a minimum of twice the amount of code lines as a crude, brute and simplistic solution. At the end, the compiler doesn't care how many lines of code you used, and the second "crude" piece is much easier to read and understand than your elegant fit-all-truth-checks-in-one-line solution.
03/01/07 @ 06:34
Andrew
Comment from: Andrew [Visitor] · http://andrews.co.nr
I absolutely agree with Erez, when I think about it more closely. An elegant piece of code is absolutely different from a shorthand piece of code, and the shorthand is generally more crude and unreadable in it's very nature. Elegant code, when you think about it, is USUALLY longer than "less elegant" code, simply because it is easier to debug, understand, and amend later.

The key is to find a balance, of course.
03/01/07 @ 07:43
Alison
Comment from: Alison [Visitor] · http://www.creativehedgehog.com
Erez Shatz said
One of the first things every CS student notices is that the more "elegant" the algorithm, the bigger and nastier it is to implement


Arrgh! Yes! recursion, anyone?

*cry* I'm on holidays, on holidays... :)
03/01/07 @ 08:40
oneandoneis2
Comment from: oneandoneis2 [Member] · http://geekblog.oneandoneis2.org/
Actually, I agree (in principle) as well. I hate coming across examples of code that are "clever" and crammed into a line or two so you have to spend hours working out what it actually does. You might recall that, the first time I came across the getline() function, I complained about how "busy" it was and how hard to understand it was (for a beginner).

But just having more lines of code to perform a task, doesn't necessarily make it clearer to read: I can't say that multiple levels of nesting such as in the above example matches the criteria of being easy to read. OK, it's more spread out. That just means that it won't fit in one standard terminal screen. And whereas in the first line, you can (fairly) easily follow the if condition 1 AND condition 2 AND condition 3 logic, in the second you have to look at all the ifs and their matching else's before you can work out just what this bit of code is doing.

Using multiple lines can make it clearer, but it can also obfusticate. IMHO, the yesterday's exercise was an example of the latter.

For example:

#include <stdio.h>

int main()
{
char world[] = "Hello, world\n\0"
int i = 0;

while (world[i] != '\0')
{
putchar(world[i]);
++i;
}
return 0;
}


It's more lines to do the same task, but is it really easier to understand than the normal "Hello, world" example?
03/01/07 @ 09:39

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)
 

[icon] Blogroll

Blogroll generated by MagpieRSS

[Links][icon] My links

[Icon][Icon] You know.. It's been 24 hours and I *still* can't think how the way COBOL uses multi-level variables could ever have seemed like a good idea
05/09/10

[Icon][Icon] Dominic tried to explain how circular references can cause a memory leak to a colleague this morning, and got told off for not working. Apparently, the analogy of a madman shooting anybody who isn't being pointed at by somebody else was NOT the boss-safe way to go..
01/09/10

[Icon][Icon] I last listened to:
The Offspring - She's Got Issues

[Icon][Icon] Most recent photo:
Submersible houseboat

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My LQ profile

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist

[Icon]MyCommerce

[FSF Associate Member]


September 2010
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

powered by b2evolution