Ways to not suck at programming

Source: Internet
Author: User
Tags documentation vars

Original address: http://www.finalint.com/2010/05/17/10-ways-to-not-suck-at-programming/

Ways to not suck at programming

Recently I wrote a therapeutic rant for myself, called "Ten Ways to suck at programming".  The article got syndicated to some link sharing sites and picked up quite a bit of traffic and comments.  The comments here is in my site were, for the most part and nice.  Comments elsewhere were a mixed bag with many people saying the sarcastic nature of the post wasn ' t helpful. While I didn ' t the it as sarcastic (after all, it is quite literally telling you to suck), and even though I ' ve Writt En many, many helpful technical articles that did nothing but collect-dust, I figured I would take a shot at offsetting my Previous rant with real steps programmers can follow to make themselves better.

Obviously, this are not a comprehensive list and most experienced programmers should already ' s But it's a decent start for newbies and probably a worthwhile reminder to some of the more experienced.

... and it'll be interesting to the if article intended to is helpful gets as much traffic as a minute. ;) #10 Store settings in a configuration file

Configuration files are a necessity in today's programming world. With most applications connecting to databases, e-mail servers, file shares and other outside resources it ' s imperative th At a good programmer store, any information, has a possibility to change outside of the release cycle of the Applicatio  N in a configuration file. This would allow the recompile of the application ' s settings to be changed without has to retest (redeploy, application).

It is important, however, not to overuse configuration files. Some things belong, Some things don ' t;  And experience is really the best way to know which is which.  Can ask yourself, "are this something that would likely change in the future?"  If The answer is yes, consider putting it in a configuration file. The more often your can change the behavior of your application without has to recompile it, the more often your ' ll look Like a hero.

Examples of good information to store in a configuration File:database connection strings SMTP Server information any STA Ndard To/from e-mail addresses File upload/download/archive paths #9 Scope and store your variables

Databases are awesome tools and nowadays pretty much of the any application you'll be asked to develop'll use one of the some so Rt.  however, when thinking of application performance database access and transactions are very expensive operations .  with that in mind a good programmer would access the database as few times as possible into order to fill the needs of The current application request.

One way to facilitate this noble goal be to store no information that your application needs in memory once it has n retrieved from the database.  a "Get once, use often" approach can go a long way towards helping with performance (and to A lesser degree code REA dability).  some information that is used so often this is even worth storing as a sessions variable in order to eliminate a requ EST (database, disk, whatever) altogether.  be careful with those, though, as they are often targeted for hacks and should never to solely on for depended Ionality and should never contain any private information. #8 Never Use arcane plugins

Plugins and Third-party code can make development go a much too much faster.  often times you can get more complete fun Ctionality by using them but there are trade-offs for this convenience. You are have to learn to "who wrote" the plugin in order to use it you usually don "t know how the internal s work They ' re almost never meant for the exact situation you ' re in

With this said, the right add-on or plugin can is a life saver and you must, must, must is careful when evaluating whe  ther or not to use one. Here are some questions to ask to help decide whether a plugin be right for your app:does it do everything I need exactly As I need it right out of the box? Is it the documented? Does it have an active user and support community? Has it been updated to coincide with major platform? is the documentation in your native language (yes, seriously)? Would it take longer to write the functionality myself than learn to work and this plugin? When I (or someone else) inevitably has to maintain or change this app in the future'll it is easy to work with or Aroun D This plugin?

If the answer to any of the questions are "no" you should be wary. #7 Always Remove unneeded functionality

This one kind the comes with a caveat:every programmer should know to use a good and dependable source control tool. Even if you are the ' only person ' working on the code, a good source control tool can (and likely'll) prove to being a life s  Aver at some point.  You'll cruise along not even knowing you need it and then bam–you implement the code sweeping.  If you are using a source control tool correctly, undoing your changes should is fairly quick and painless. If not, your may have just lost a client or a job.

With this in mind, try to never, ever leave unneeded functionality code base. If you have written a great piece of functionality so is no longer needed delete it immediately.

When you ' re actually making changes this steer you away from existing functionality you understand why you no Longer need the functionality and you likely have a idea of what else it touches.  when you come back in six months, after seeing two or three other projects, you'll likely have  no idea Why that functionality wasn ' t previously removed.  you ' ll, Boy, I didn ' t remove this, something somewhere must to be using it. " this problem compounds itself When you ' re forced to start writing code around undeleted, unnecessary functionality.  so in the end it's easier to just remove code and keep your code base clean. #6 Always Consider performance

What makes a good programmer? The ability to effectively juggle the following points and still deliver timely, effective solutions:performance–is the Code minimal and fast? Scalability–is the code able to be expanded upon easily? Manageability–how quickly can modifications be evaluated, estimated and made?

The preference for which of, to, on, I, differs, programmer to programmer, and often from project to Projec  What gives each programmer their individual style.  But Good programmers always consider all three, no matter the situation.  Readability falls under manageability, abstraction falls under scalability and everything falls under. So no matter what your programming personality and style are, you should are considering performance.

I ' m not (necessarily) referring to spending hours pouring over lines of the code to make sure it executes as quickly as Possible.  i ' m talking about taking a solid fundamental approach to gathering and manipulating data that doesn ' t does any one pie Ce of functionality more times than absolutely necessary.   #9 above eludes to this, but to go a little further:get your data once and use it in as many places as possible BEF Ore disposing of it. Have the absolute minimum amount of functionality inside. Never perform accesses (disk, database, service) from within a loop unless absolutely. (in strongly typed languages) Know and use appropriate data types. (in Web Apps) Perform as few server requests as possible.

If you are things like this from the beginning of your project you are less likely to have performance  End. If you don ' t agree with me now, it's worth the initial extra effort you'll after you spend 4 weekends straight Rewri Ting Poor performing code because you tried to save a minutes on the front end. #5 Never Nest major logic/functionality in loops

I mentioned this one under heading #6 above, but it bears repeating.  Loops are often the heart of any application, but they'll absolutely kill your app if used improperly. So your must be wary of the anything that falls into a loop.

Loops are something we have to use often, so every good programmer should-how-to-abstract know out of a loop  (#1 below for a simple example). Besides the performance hits, tracing through functionality this is inside of a loop can to much difficult because of  All of the variables this come into play while looping. Modern debuggers help tremendously with the this task, but better tools shouldn ' t excuse poor the programming.

When you have the review your code, start by reviewing loops and seeing if anything the can be moved outside of them. #4 Document effectively

There is a school of thought among some programmers this says, "Either you can read code or you can ' t, why should I Docume  NT It for you? " With is true in theory, in practice adding appropriate documentation inside code makes it far, far easier to Mai Ntain.

Proper documentation is a skill and overdoing it is almost as the big of the annoyance to those trying to maintain code as  Doing it all. Consider the following: do –denote reason and logic did not –denote functionality or process

What that means Is:tell the future programmer looking in this code why your did something, not necessarily how. The "how" are the stuff they should usually be able to read on their own (unless and have to do some crazy, non-standard St  UFF).  More often when looking in the code they need to know "why" for you did things a certain way. Consider the following simple example:

for (int i = 0; i < var.length i++) {dosomething (var[i]);

Now let's consider two commenting options://Iterate over per var for (int i = 0; i < var.length; i++) {//increment I if it ' s less than vars.length dosomething (var[i)); Do something with the Var at position i}//Finish the Loop

-or-for (int i = 0; i < var.length i++) {dosomething (var[i]);//Mark inactive vars for deletion}

Now the "the" tells you what are happening, about as pointlessly as a voice-over in a movie saying exactly the cha Racters on screen are doing. The second one assumes that your know what is happening functionally and instead just tells to what ' s going on  Which is far more valuable information when you ' re in ' put out the fire of the forest variety ' mode. In the "the", the future programmer would need to look up dosomething (Var) and figure out what it is accomplishing. In the second version, that information, at hand and valuable.

Which would you prefer if your got a call from a executive in 4:45pm on Friday "that forced" to review the This code? #3 use effective and logical variable names

There are a LOT of thoughts on this one and almost every programmer the has a slightly different way of doing it. So I'll try to be ambiguous so we don ' t start a war over it.

It is important to consider your variable names carefully. Sometimes has the data type as part of the name was appropriate (INTID) and sometimes that sort of thing can be inferred  (ID). Either way, the name of the variable should hint towards the usage of the variable (note that "id" is in both). Names like (Vulspok) and (Humkirk) are not useful.

Now possibly just as important as choosing your style of naming variables carefully are being consistent. As a programmer your code would have a certain "style" that others the WHO inherit your code would have to learn into order to qui  Ckly and easily maintain your code. That "style" may isn't be the same as theirs, but as long as it's consistent (and logical) you've done what you can do. #2 Catch errors, or don ' t

Let me start by saying the I know, academically, all errors should is caught and handled. Let me also say that realistically, which almost never happens when deadlines are.

Hopefully the days of "Dr. Watson encountered an error" are behind us for good.  Most modern platforms would let applications die and a pretty error message. So while it may isn't ideal, letting errors fall through to the default error handling is better than manually Mishandlin  G The errors. On internal or limited usage applications this can actually provide a pretty good idea of what the actual error is and whe Re to start looking to fix it.

Now in the ideal world, the programmers would have time to code a catch for anything this went wrong and do something cool, sup  Er-ninja style.  So if you are have time and budget your should catch all possible errors and handle. If not, don t catch them in all.

Just never, ever catch errors and do nothing. #1 Never Duplicate functionality

Reusable code is the Holy grail of good programmers. A good programmer should always is on the lookout for a way to write functionality once and then the  . The benefits to this are too numerous to count.

So what is reusable code?

Let's say, using our example above so you need to loop through your VARs and dosomething () with them. Could do it this way:for (int i = 0; i < var.length i++) {//Mark inactive VARs for deletion if (!var[i].isactiv E) {var[i].readytodelete = true;} else {var[i].readytodelete = false;}}

-Or-private void DoSomething (Var) {if (!var.isactive) {var.readytodelete = true;} else {var.readytodelete = false; (int i = 0; i < var.length i++) {dosomething (var[i]);//Mark inactive vars for deletion}

It ends up being 3 extra lines of the code in this example that'll save you 4 lines of the code each time your use it somewhere e  Lse. So the "re-use" the "code in this" rudimentary example you ' ve saved yourself 75% effort across the board (Codin  g, debugging, refactoring, etc). Pretty nifty, huh?

Additionally, if you are ever need to change the evaluation logic to, say, make sure that there weren ' no any records so may B e Orphaned by deletion, your could change is logic in a and it would work everywhere throughout code.

If you are have any major items to add, feel free to do so in the comments. If I get enough good ones perhaps I ' ll update this article to include them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.