Results 1 to 10 of 10

Thread: Writing code.

  1. #1
    Join Date
    Jun 2004
    Posts
    296
    Rep Power
    0

    Default Writing code.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. - Martin Fowler

    I just ran across that statement and I thought it quite profound. Anybody care to comment on this. I believe this is why I have slowly begun to appreciate more and more languages with a functional feel to them like ruby. For a long time I have been very happy to program in the C/C++ type of languages, and almost detest anything functional-like like Haskell, javascript, python, etc, but now I realise that it takes some discipline to write C/C++/Java languages in a way that makes it easy for colloborative effort, especially with ppl you will never meet or talk with. As I reflect on my experiences as a programmer and as one who has taught programming. I have realised that by myself I spend alot of time trying to make my code look beautiful for some reason I didnt realise and when looking at other peoples code I could tell how good they where by how beautiful their code looked, (well most of the time, some ppl are quite clever and write the most horrible code, but atleast u can see a sense of something interesting going on even if u cant really pinpoint how its being done) I noticed that alot of problems people had with coding lay in how unbeautiful their code was, in terms of both looks and what it said about how they viewed the solution to the problem. I've also found that when working with very large systems where you want to maximize on reuse, many designs lend themselves to alot of mechanics or extra work that a programmer has to do stay in the framework. Which is why I agree with the person who said that a framework should make programming easier not harder, but I guess the problem many times is developers of large systems dont really think in terms of developing a framework.

    At the end of the day, as a developer of a large system and for own sanity and enjoyment of programming, I strongly believe that one should program in straight forward manner, instead of using byzantine methods! Its most natural to think that a clever programming writes code that is so good, that less clever programmers cant read, but that is a big problem. Rather it makes more sense to be even more clever such that the code you write still acheives what you want with the minimal amount of code and even optimized but so clear that newbies can make some sense of it. That is code that almost doesnt need comments (you should still always write comments) because it looks so naturall

  2. #2
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    i try to put in the effort to format my code...but nobody can understand ma cod more than half the time. beautiful code goes beyond formatting, and stretches in to making the concept simple... a paradox .

    i read a lot of code, and personally, my designs normally make extensive use of design patterns and well known algorithms... for a well read programmer thats easy to understand, however, for a novice its quite the opposite.

    how do u say this is a binary search which is more efficient on an ordered list than a linear search on a plain array. linear search on plain array is simpler and straight forward, but id never choose that unless there was no other option.

    how do u code an event (observer pattern) in plain c++, so a novice understands... it makes a lot more code that apparently does nothing extra. but the coder who knows the observer pattern does not read all the code to understand it.

    the understanding is in the documentation and not the code.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  3. #3
    Join Date
    Jun 2004
    Posts
    296
    Rep Power
    0

    Default

    I agree that design patterns and certain other good practices are out of the scope of many beginners, but Ive still found that there is a way to acheive beauty by programming as if tho you were writing an easy and weaving a story. Ive seen some code where I didnt even have to guess or think too hard about what it was doing, because I could almost read it like english. So I far Ive traced this ability to 1) proper use of OO and 2) language features that lend themselves to a more natural approach to specifying what you want to get done.

    In the case of proper OO.
    1) We could have code that goes

    <code>
    Master = new UserMaster
    Master->buildUser(username, password)
    if (Master->isUserAGoodBoy(username))
    Displayer->setUserAGoodBoy
    endif
    </code>

    Ok thats horrible, how about

    <code>
    User = User::create(username, password)
    if (User->isAGoodBoy())
    Displayer->setUserAGoodBoy
    endif
    </code>

    Hmm, thats something I kind of pulled out of my head, and Im not sure it really communicates what I had in mind, but I believe the second snippet to be more beautiful, just because its more apparent, well to me that a user is being created and you can ask it questions about itself, but of course thats just standard OO, but I am often suprised at how many programs are written without thinking like this, not just for the trivial case, but in terms of the entire structure or framework of the design of a complex application.

  4. #4
    Join Date
    Jun 2004
    Posts
    296
    Rep Power
    0

    Default

    Ive also found that many people who use design patterns use them using the exact same names for certain methods as outlined in literature, say given classes Observer and Observable, they would inherit or impliment these classes such that their interface includes methods named like observe, notify, addSubscriber, etc. I think it would be better to write methods that better communicate what u want to do and even hide the design pattern specific speach inside your class with the appropriate code documentation, the result being code better communities what its doing as opposed to the emphasis on how its doing it.

  5. #5
    Join Date
    Mar 2003
    Posts
    492
    Rep Power
    0

    Default

    Quote Originally Posted by alexdevmaster View Post
    <code>
    Master = new UserMaster
    Master->buildUser(username, password)
    if (Master->isUserAGoodBoy(username))
    Displayer->setUserAGoodBoy
    endif
    </code>

    Ok thats horrible, how about

    <code>
    User = User::create(username, password)
    if (User->isAGoodBoy())
    Displayer->setUserAGoodBoy
    endif
    </code>
    The second snippet is more efficient in the amount of lines you write, but in the case of coding with business objects in your app it could be a nightmare.

    Imagine all your business objects inherit from a base BusinessObject class that has its own constructor. You would let's say have a PersonObject that inherits from that base class. Your user class (UserObject) would inherit from PersonObject. With the second code snippet, you would have to introduce a new constructor to take two parameters. That too could add to code complexity and loss of elegance.

  6. #6
    Join Date
    Jun 2004
    Posts
    296
    Rep Power
    0

    Default

    Quote Originally Posted by Blunty Killer View Post
    The second snippet is more efficient in the amount of lines you write, but in the case of coding with business objects in your app it could be a nightmare.

    Imagine all your business objects inherit from a base BusinessObject class that has its own constructor. You would let's say have a PersonObject that inherits from that base class. Your user class (UserObject) would inherit from PersonObject. With the second code snippet, you would have to introduce a new constructor to take two parameters. That too could add to code complexity and loss of elegance.
    Actually, I used a static method and not a contructor to create my user in the s econd snippet, not so apparant since I didnt define a language and this is semi-pseudocode. I tend to use alot of static factory methods for object creation partly for the reason you just sited. In such a case I dont have to worry too much about inheriting too much in base class constructors, but even that can be managed by proper OO modeling.

  7. #7
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Quote Originally Posted by alexdevmaster View Post
    Ive also found that many people who use design patterns use them using the exact same names for certain methods as outlined in literature, say given classes Observer and Observable, they would inherit or impliment these classes such that their interface includes methods named like observe, notify, addSubscriber, etc. I think it would be better to write methods that better communicate what u want to do and even hide the design pattern specific speach inside your class with the appropriate code documentation, the result being code better communities what its doing as opposed to the emphasis on how its doing it.
    actually i disagree-ish... code that uses names from design pattern retain the names to indicate that they are low level code, objects that form the framework of your application.

    now lets say u inherit that functionality in a business object, thats where u use the business friendly names and lose the framework/design pattern name.

    as an example, microsoft implements base classes and interfaces to connect to a database in ADO.NET...
    Code:
    ICommand ... Command design pattern
    IConnection ... 
    IDataAdapter ... Adapter design pattern
    then in another layer, it implements these classes, keeping the design pattern names
    Code:
    SqlCommand ... Command design pattern implementation
    SqlConnection ... implementation
    SqlDataAdapter ... Adapter design pattern implementation
    now thats still framework, so when it reaches ur level u dont need to say that u have connections and commands, what u need are Users and Managers etc
    Code:
    IUser ... models a user
    IManager ... models a manager
    next layer, implementation... business objects with business rules, they dont even know theres a database, with stored procedures, etc
    Code:
    Cambio.Data.User ... implements a user
    Cambio.Data.Manager ... implements a manager
    next layer ...implements crud operations, ur classes use the connection/command and dataadapter, but u shouldnt expose it on ur interfaces or business objects
    Code:
    Cambio.DataAccess.User 
    Cambio.DataAccess.Manager ...
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  8. #8
    Join Date
    Sep 2004
    Posts
    281
    Rep Power
    0

    Default

    Quote Originally Posted by alexdevmaster View Post
    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. - Martin Fowler
    I don't agree with this notion, as one off the posters mentioned, the readers level of understanding is important. What i do agree on is, the use of frameworks,design patterns and established best practices. I am only recently come started working with design patterns find them more suited for dynamic languages(php,etc)(still working to fit them into delphi).

    One more thing..........i belive good understanding of what is required and doing proper system analysis and design still works , i think more of us spend far too much time with the code and less time understanding what it is we should be trying to achieve hence we get lost in the code.
    Anything or Anyone that fails to grow will eventually die. {AI}
    -------------------------------------------------
    Tomorrow is the future!
    Today Is the Tomorrow you made Yesterday!{AI}

  9. #9
    Join Date
    Jun 2007
    Posts
    678
    Rep Power
    0

    Default

    functional programming s the stepping stone for any great progrmmer

  10. #10
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    couldnt agree with u more... even tho i was not formally taught any functional languages (UTECH). I been around people who have, and assisted with assignments, and i've done ma research. the closest i've been is Lambda Calculus.

    the problem is there is no efficient implementation of purely functional language (correct me if im wrong). most implementations seem to be interpreted... and even microsofts additions to c# for funtional style programming, though compiled, is not purely functional.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •