Jesus 'n Jim
A mainly PC (some Mac) site w/Software, Computer Repair Info, How-To's on Using Computers
Technical Support 1-360-521-2060 (my business line cell)

some guidelines or rules of programming

 

programming tips that can save you headaches

use defensive programming.

for your syntax, use hungarian notation, where you put the type before the name of the variable.

char sFilePath[2048+1];
void * pvBuffer = malloc(12);
int nBufferSize=12;
(C code)

Be consistent (unless you must learn a new coding style for your job).


There are two brace styles.

if () {
}
and
if ()
{
}

the latter being more common, though I choose the former. choose one and stick with it. you need to conform to your company's coding standards.


when doing a comparison, always place the constant on the left hand side, in case it accidentally has become an assignment. Then the compiler will generate an error.

if ('3'==ch) {
in C,PHP,Javascript,Java, comparison with no error.
if ('3'=ch) {
in C,PHP,Javascript,Java, error generated, logic error shown by compiler.
if (ch='3') {
in C,PHP,Javascript,Java, no error generated, but assignment performed!  in BASIC, this does not do an assignment, but a comparison.

try to remember to always put a break or return statement in the cases of your switch statements. get in the habit of formatting it that way by default.

There are some things for which a switch is not designed, such as ranges of values larger than 3. know what those are. switch is designed for specific constants and should only be used for such.


If you are using C++, use the Standard Template Library (C++ Standard Library) where possible to simplify your code. You may need to wrap types in classes in order to use collections. In fact, in any language, make as much use of the libraries that you can to simplify your work and make it as cross-compiler as you can, but try to use libraries that are common to all compilers of the same type.

For instance, when you are using javac to compile your java code, you may want to target your code a few revisions back so people who have not updated their java can run your code. Some people are afraid to update anything or never to because they don;t know how or they have an old version. So make provision for them.


This one is from me. always put your limits first in a while conditional. for instance, while (!isdigit(s[i]) && i < l) { is more likely to break under a short-circuit evaluation and contain a bug, this should converted to while (i < l && !isdigit(s[i])) {

installers

people like .dmg's or installers (setup.msu or setup.msi or setup.exe). they don't want to have to deal with trying to figure out your unusually complicated instructions for installing your program. learn to write an installer. you will be a better programmer for it, and the job industry will probably like you for it too. In fact, If you see an Open Source application out there that needs an installer and you know how to write a good one, write one for them, and offer it to them, or take a simplified version of one of your own and give it to them as an example to study, and tell them where to get the NSIS installer (google it to find).

actually, installers can be a pain to learn at first, quite a learning curve, but you can use the Open Source installer I have written (setup.nsi) in http://JesusnJim.com/code/pwdgen.html or http://JesusnJim.com/code/kitchentimer.html just look at the zip file to get the source code. it is an easily modifiable installer you can use for your .exe files. use it like a template and learn from it.

you will need to do a global search and replace on pwdgen to your application name at least, and fix up the version numbers. the .nsi file must be in the same directory as the root of whatever you want added to the setup executable. gpl.txt is the gpl license text file in the same directory and gets compiled into the setup program, so make sure it's there or you will get an error.

NSIS, Auto-it3

if it is necessary, then go ahead, but make sure you document the fact that your system requirements are such-and-such. Even better, learn how to write an installer and see if you can include those required frameworks in your package in a legal manner.

There is a developer-includable licensed version of Java SE JRE and look here for the .Net redistributable.

scriptable installers like NSIS aren't too difficult to learn (takes about 3-6 months to get good at it, then you just use a template from then on), they are very similar to learning macro assembly language, except with functions and sections and section groups. and using the File command, you can make standalone monolithic installers that contain everything compressed, and it's accessable just like a local directory.

licenses

learn to read EULAs, license agreements, and legalese. it will take you far.

the more important things you will need to know about are the distribution section of the document, but it doesn't hurt to read the entire thing, because sometimes there are some real surprises out there about how you can use a product (like the firebird license), and whether or not you must include a copy of the source code with your product (or point to where it can be found, as is the case with Open Source Software(OSS)/Open Source Initiative(OSI) licenses.

You may have to read the same section over and over 20 times to understand it, but keep trying until you do (this doesn't usually happen, it's usually very clear).

which compilers you can use use

When choosing a compiler, you need to take into account several things:

  • am I writing Open Source Software or otherwise distributing the source code?
  • Are you a student? see above line. you come under the category of distributing source code.
  • what are your distribution rights for the compiler you want?
  • what are the licensing requirements and distribution requirements for any redistributables? (DLL's, [Visual C++] runtimes, [.Net] frameworks, [Java] virtual machines, etc)
  • If you are using a database server like PostgreSQL or other 3rd party product, what are their licensing requirements for distribution if you are embedding the product? and for their DLL's? you might need to visit a forum to find out.
  • does the language efficiently solve the problem and is it relatively easy to use for you?
  • if you need portability, is it a common language? is it a portable language, or can you make provisions for portability (such as macros or #ifdefs for multiple platforms and compilers)?
  • If you don't have to use a new feature that makes everybody upgrade their .Net framework or version of java (or if they have to use one at all and you are not including it), or have to install some sort of runtime manually, (and you are not going to do it for them) don't use it. It's a royal pain and makes your app look very unappetizing. I just wonder how many people don't have java installed on their browsers because they just don't know how...

some compilers like Visual Studio Express have stipulations as to how you may distribute your executables and source.
See the license agreement for your compiler.
The Microsoft Express licensed products disallow distributing source, which is the antithesis of Open Source policy of providing source or providing source along with the binary.
This is one of the reasons why you need to read the EULA or license agreements on things.
The $800-$2500 Professional version probably doesn't have these stipulations on it.

GPL or BSD license means Open Source, go ahead and use it (but your company may not approve of having Open Source Software used inside the company for security reasons, so that is why you may end up using a commercial pay-for compiler there).

command-line arguments

for command-line programs, people like to use /help, /Help, /HELP, /h, /H, -h, -H, --h, --help for doing help, and sometimes no arguments gives help. so be prepared to handle arguments like that. that's as easy as doing this (can also be done by #ifdef #else #endif and #define -ing a replacement macro for _stricmp since (I think) windows-based compilers are the weird case here.

#if defined(__MINGW32__)
/*mingw-w64 toolchain sezero configures
the CRT using --enable-wildcard which enables globbing. To disable it, you
can either add a global to your source, like "int _dowildcard = 0;" or you
can add CRT_noglob.o to your list of objects to be linked.*/

int _dowildcard = 0; /*disable globbing in mingw-w64 sezero toolchain*/
#elif defined(__DJGPP__)
/*http://www.delorie.com/djgpp/v2faq/faq16_2.html*/
char **__crt0_glob_function(char *_argument) {
    return 0; //cause no expansion
}
#endif

if ( argc >= 1 && (
#if defined(_MSC_VER)||defined(__BORLANDC__)||defined(__MINGW32__)
	0==_stricmp(argv[0],"--h"))||
	0==_stricmp(argv[0],"--help"))||
	0==_stricmp(argv[0],"-h"))||
	0==_stricmp(argv[0],"-help"))||
	0==_stricmp(argv[0],"/h"))|
	0==_stricmp(argv[0],"/help"))||
	0==_stricmp(argv[0],"--?"))||
	0==_stricmp(argv[0],"-?"))||
	0==_stricmp(argv[0],"/?"))
#else
	0==stricmp(argv[0],"--h"))||
	0==stricmp(argv[0],"--help"))||
	0==stricmp(argv[0],"-h"))||
	0==stricmp(argv[0],"-help"))||
	0==stricmp(argv[0],"/h"))|
	0==stricmp(argv[0],"/help"))||
	0==stricmp(argv[0],"--?"))||
	0==stricmp(argv[0],"-?"))||
	0==stricmp(argv[0],"/?"))
#endif
    )) {
    help();
    return 0;
}

GUI or TUI vs command-line

provide a GUI or TUI where possible. People in general don't know what to do with a command-line utility or with DOS anymore.

if you have no clue about GUI code, a simple start may be doing command-line programs and wrapping them with a GUI like Auto-it3. Windows XP SP3 now has an 8190-character command-line. and so, I think do all the later versions of windows.

Or you may choose to do Java Swing (get a good book!) for your GUI and use one of the many 3rd party java-to-EXE utilities (may require you to make a jar first). know that they will still have to install Java to run it, unless you include a copy of Java with your package and do some sort of Java version checking before installing.