Path: utzoo!attcan!uunet!zephyr.ens.tek.com!uw-beaver!mit-eddie!bloom-beacon!eru!hagbard!sunic!lth.se!newsuser From: magnus%thep.lu.se@Urd.lth.se (Magnus Olsson) Newsgroups: comp.misc Subject: Re: In defence of *Real* programming - was Re: MEL - A *Real* Programmer Message-ID: <1990Oct26.111612.27868@lth.se> Date: 26 Oct 90 11:16:12 GMT References: <7380.271c3129@ccvax.ucd.ie> <1990Oct23.235720.16178@nas.nasa.gov> <6089@nisca.ircc.ohio-state.edu> <7386.2726d343@ccvax.ucd.ie> Sender: newsuser@lth.se (LTH network news server) Organization: Theoretical Physics, Lund University, Sweden Lines: 88 In article <7386.2726d343@ccvax.ucd.ie> b_haughey@ccvax.ucd.ie (Brian J Haughey) writes: >First of all, when I write code I make no apologies for its succintness >or cleverness. The project I am working on involves writing code in >assembler for the 1802 microprocessor. (Please ! No flames ! If it's >good enough for space probes...). Because of the relatively small >address space, we are hard-pressed to fit in all the features andcapabilities >that we are committed to doing, even using bank-switching and other >methods. Having recently read through the data sheet for the 1802, I can understand why you have to write obscure code. You have my sympathies... :-| >I do not write obscure code for the sake of being obscure. Any code I write >is designed to be as small and fast as possible. If it is not clear what >the code does, then I comment it. >Once the code is properly commented, and is efficient, it is sufficient ! I think this is a very important point: The code has to be properly commented. If you have some compelling reason to write code that may be a bit obscure to others, then be sure to comment it, so it is clear what it does! I've once had the doubtful pleasure of having to understand a Fortran IV program that calculated atomic energy levels (I was going to add some new features to it). Now, this program was written by "real programmers" in the late sixties, and run on some really small and slow machines by todays standards, so I don't blame them at all for producing some pretty obscure code - they simply *had* to re-use COMMON variables for different things in different subroutines and things like that. What *did* cause me a lot of trouble was that the code was almost totally undocumented - in 5000 lines of code, there couldn't have been more than five or ten comments! Good programming style is important, and there are certain rules, like "Don't use GOTO's", which should be followed perhaps 99% of the time, not only because it makes your program more readable to others, but also because it reduces the risk of your making a mistake. Sometimes, however, you have to break these rules. The sad thing is that a lot of people seem to think of these rules as immutable laws, written down in the Holy Scriptures of Computer Science: "Thou Shalt Not Use The Abominable Goto In Thy Programs". Maybe one should put the point about "obscure" programming like this: Don't write obscure code or invent "clever" constructs unless you really have to. If you have to, be sure to comment exactly what your code does. Don't do it just to show how clever you are. An example: Suppose I want to exchange the values of the two integer variables A and B. The standard way of doing it is TEMP:=A; A:=B; B:=TEMP; Another way, which "saves" you from declaring the temporary variable, is A:=A+B; B:=A-B; A:=A-B; This is (arguably) "clever". It also totally obfuscates the meaning of the code. Normally, the gain of not having to declare a temporary variable is totally negligible and does not compensate for the lack of readability. Also, the code above is non-portable: If you decided to change the type of A and B to some type where addition and subtraction aren't defined (e.g. if you switched from representing people by their SSN (which is common in Sweden) to representing them by their name), then you'll have to rewrite the code. Conceivably, there could be a case where you really have strong reasons to use the "clever" code (e.g. on a Basic machine with only 26 variables). In that case, nobody should blame you for using it. If you're using it just to show off (which many hackers seem to be doing all the time), it's another matter. Magnus Olsson | \e+ /_ Dept. of Theoretical Physics | \ Z / q University of Lund, Sweden | >----< Internet: magnus@thep.lu.se | / \===== g Bitnet: THEPMO@SELDC52 | /e- \q