background preloader

Programming

Facebook Twitter

Clever Programming Tricks. Here is a collection of `programming tricks' which I have found useful. They come from various sources (which I will try to credit) and sometimes I have just invented them (probably for the umpteenth time). I am happy to add your favourite trick to the list -- just e-mail it to me. Alan Mycroft Back to HAKMEMC All of these tricks are intended to save a factor of at least two (and often ten or more) by local changes to code.

Algorithmic Ideas Use a lookup table instead of calculating things when needed. Low level hacks. How to be a Programmer: A Short, Comprehensive, and Personal Summary. Debugging is the cornerstone of being a programmer. The first meaning of the verb to debug is to remove errors, but the meaning that really matters is to see into the execution of a program by examining it. A programmer that cannot debug effectively is blind. Idealists that think design, or analysis, or complexity theory, or whatnot, are more fundamental are not working programmers. The working programmer does not live in an ideal world. Even if you are perfect, your are surrounded by and must interact with code written by major software companies, organizations like GNU, and your colleagues. Debugging is about the running of programs, not programs themselves.

To get visibility into the execution of a program you must be able to execute the code and observe something about it. The common ways of looking into the ‘innards’ of an executing program can be categorized as: Some beginners fear debugging when it requires modifying code. How to Debug by Splitting the Problem Space. Ideone.com | Online IDE & Debugging Tool >> C/C++, Java, PHP, Python, Perl and 40+ compilers and intepreters. Law of Demeter. Each unit should have only limited knowledge about other units: only units "closely" related to the current unit.Each unit should only talk to its friends; don't talk to strangers.Only talk to your immediate friends. The fundamental notion is that a given object should assume as little as possible about the structure or properties of anything else (including its subcomponents), in accordance with the principle of "information hiding".

It is so named for its origin in the Demeter Project, an adaptive programming and aspect-oriented programming effort. The project was named in honor of Demeter, “distribution-mother” and the Greek goddess of agriculture, to signify a bottom-up philosophy of programming which is also embodied in the law itself. In object-oriented programming[edit] When applied to object-oriented programs, the Law of Demeter can be more precisely called the “Law of Demeter for Functions/Methods” (LoD-F). Advantages[edit] Disadvantages[edit] See also[edit] References[edit] MIT OpenCourseWare | Electrical Engineering and Computer Science | 6.945 Adventures in Advanced Symbolic Programming, Spring 2009 | Home.

OOPWeb.com - Win32 Programming for x86 Assembly Language Programmers by Henry Takeuchi. PHP Master | Managing Class Dependencies: An Introduction to Dependency Injection, Service Locators, and Factories, Part 1. Let’s face it: for good or bad, OOP has been actively drilling deep holes in the soil of PHP in the last few years, hence its ubiquitous presence now is anything but breaking news. Furthermore, while this steady incremental “objectification” in the language’s terrain is generally considered a beneficial shift towards new and more promising horizons, for those still rooted in the procedural paradigm, the process has been far from innocuous. There’s a few solid arguments that sustain this mindset: first and foremost the nature of OOP is awkward and inherently complex, plagued with nuance that can take literally years to tame.

Also, defining the APIs that OO components use for interacting with one another can be quite a burden at times, especially when it comes to designing systems whose backbone rests on facilities provided by large and tangled object graphs. Evidently, the topic is a world away from being banal, and as such it deserves a close, in-depth analysis. Closing Remarks. PHP Master | The Single Responsibility Principle.

Robust software systems should be built up from a web of interrelated objects whose responsibilities are tight, neatly cohesive, boiled down to just performing a few narrowed and well-defined tasks. However, it’s admittedly pretty difficult to design such systems, at least in the first take. Most of the time we tend to group tasks in question by following an ad-hoc semantic sense in consonance with the nature of our own human mind. One of the most notorious consequences of this rational associative process is that, at some point, we effectively end up creating classes that do too much.

The so-called “God class” is quite possibly the most extreme and coarse example of a structure that packages literally piles of unrelated operations behind the fence of the same API, but there are other subtle, more furtive situations where assigning of multiple roles to the same class are harder to track down. A good example of this is the Singleton. Putting Data Access Logic in a Data Mapper. Programmer Competency Matrix. Programming Optimization: Techniques, examples and discussion. In the old days, it was pretty easy to understand that writing your programs in assembly would tend to yield higher performing results than writing in higher level languages.

Compilers had solved the problem of "optimization" in too general and simplistic a way, and had no hope of competing with the sly human assembly coder. These days the story is slightly different. Compilers have gotten better and the CPUs have gotten harder to optimize for. Inside some research organizations the general consensus is that compilers could do at least as well as humans in almost all cases.

During a presentation I gave to some folks at AT&T Bell labs (a former employer) I explained that I was going to implement a certain piece of software in assembly language, which raised eyebrows. But have compilers really gotten so good that humans cannot compete? Here's an off the cuff post from Anthony Tribelli on rec.games.programmer: For more, see Randall Hyde's Great Debate page. Robust Programming. Department of Computer Science University of California at Davis Davis, CA 95616-8562 Principles of Robust Programming A robust program differs from a non-robust, or fragile, program by its adherence to the following four principles: Paranoia.

Don't trust anything you don't generate! Whenever someone uses your program or library routine, assume they will try to break it. Stupidity. Part of the problem is that in a week, you most likely will have forgotten the details of your program, and may call it incorrectly or give it bogus input. Dangerous Implements. Hiding data structures also makes your program or library more modular.

Can't happen. Exercise 1. Robust programming is defensive, and the defensive nature protects the program not only from those who use your routine but also from yourself. Example of a Fragile Library This library implements queues in a very straightforward way. The Queue Structure The header file contains: As indicated, the queue management functions are: Exercise 2. Table Oriented Programming. A practical, intuitive, and consistent way to organize and process data and algorithm collections Updated 2/12/2002 Summary of Primary Table-Oriented-Programming Concepts Control Tables - A way of organizing processing, decision, and attribute logic.

Table-Friendly Syntax - Syntax and language constructs that make dealing with tables and T.O.P. easier and more natural. This includes overcoming the weaknesses of SQL. Data Dictionaries - Special Control Tables for storing processing, decision, and attribute logic for relational database fields and/or UI fields. Fundamental and Consistent Collection Operations - A base set of operations (interface) that all collections (tables, trees, stacks, lists, etc.) should have easy or built-in access to regardless of a collection's current size or complexity. Most general-purpose languages use API-like constructs (function library calls) and SQL to deal with tables. A = (b * c) + e + f a = plus(plus(times(b,c),e),f) // silly example My Motivation. Top 15+ Best Practices for Writing Super Readable Code | Nettuts+ Twice a month, we revisit some of our readers’ favorite posts from throughout the history of Nettuts+.

Code readability is a universal subject in the world of computer programming. It's one of the first things we learn as developers. This article will detail the fifteen most important best practices when writing readable code. IDE's (Integrated Development Environment) have come a long way in the past few years. This made commenting your code more useful than ever. Following certain standards in your comments allows IDE's and other tools to utilize them in different ways.

Consider this example: The comments I added at the function definition can be previewed whenever I use that function, even from other files. Here is another example where I call a function from a third party library: In these particular examples, the type of commenting (or documentation) used is based on PHPDoc, and the IDE is Aptana. I assume you already know that you should indent your code. PEAR Style: Here is an example: