Posted by admin on March 1, 2008 under Programming |
- All data should be hidden within its class.
- Users of a class must be dependent on its public interface, but a class should not be dependent on its users.
- Minimize the number of messages in the protocol of a class.
- Implement a minimal public interface that all classes understand [e.g., operations such as copy (deep versus shallow), equality testing, pretty printing, parsing from an ASCII description, etc.].
- Do not put implementation details such as common-code private functions into the public interface of a class.
- Do not clutter the public interface of a class with things that users of that class are not able to use or are not interested in using.
- Classes should only exhibit nil or export coupling with other classes, that is, a class should only use operations in the public interface of another class or have nothing to do with that class.
- A class should capture one and only one key abstraction.
- Keep related data and behavior in one place.
- Spin off non related information into another class (i.e., non communicating behavior).
- Be sure the abstractions that you model are classes and not simply the roles objects play.
— Object-Oriented Design Heuristics By Arthur J. Riel
Posted by admin on under Fun, Programming |
- Anytime things appear to be going well, you have overlooked something.
- If it looks easy, it’s tough. If it looks tough, it’s damn near impossible.
- You always find any bug in the last place you look.
- Anything can be made to work if you fiddle with it long enough.
- If you do not understand a particular word in a piece of technical writing, ignore it. The piece will make perfect sense without it.
- What you don’t do is always more important than what you do do. No matter how much you do, you’ll never do enough.
- Procrastination avoids boredom; one never has the feeling that there is nothing important to do.
- Always leave room in source code to add an explanation if it doesn’t work out.
- Nothing is impossible for a man who doesn’t have to do it himself.
- If builders built buildings the way programmers write programs, then the first woodpecker than came along would destroy civilization.
- Any cool program always requires more memory than you have. When you finally buy enough memory, you will not have enough disk space
- If a program actually fits in memory and has enough disk space, it is guaranteed to crash. If such a program has not crashed yet, it is waiting for a critical moment before it crashes.
- A working program is one that has only unobserved bugs.
- “The hard you try to do something *new*, the sooner you find out that somebody has already done it.”
- No matter how good of a deal you get on computer components, the price will always drop immediately after the purchase.
- Software bugs are impossible to detect by anybody except the end user.
- No matter how hard you work, the boss will only appear when you access the internet.
- The hard drive on your computer will only crash when it contains vital information that has not been backed up.
- Each computer code has five bugs, and tis number does not depend on how many bugs have been already found (it is conservative).
- The number of bugs always exceeds the number of lines found in a program.
- The most ominous words for those using computers: “Daddy, what does ‘Now formatting Drive C mean’?”
- An expert is someone brought in at the last minute to share the blame.
- For any given software, the moment you manage to master it, a new version appears. The new version always manages to change the one feature you need most.
- Whenever you need a crucial file from the server, the network will be down.
- A patch is a piece of software which replaces old bugs with new bugs.
- Failure is not an option, it’s included with the software.
- It’s not a bug, it’s an undocumented feature.
- Bugs mysteriously appear when you say, “Watch this!”. The probability of bugs appearing is directly proportional to the number and importance of people watching.
- The only program that runs perfectly every time, is a virus.
- If it works, it’s production. If it doesn’t, it’s a test.
- Computers let you waste time efficiently
- If you make the letters in your Word document bigger and then you print it out, you’ll have everything on the first page and only one line on the second.
- After a software is released, the first bug found will be by a person who normally does not use that portion of the program but was wondering why he can’t do something he normally would not do.
- The troubleshooting guide contains the answer to every problem except yours.
- No matter what problem you have with your computer – Its Always Microsoft’s fault. If its not their fault – Blame them anyway
- Walking on water and developing software to specification are easy as long as both are frozen.
- The smaller the size of your email account, the more junk mail you will get
- When designing a program to handle all possible dumb errors, nature creates a dumber user.
- Adding manpower to a late software project makes it later. No matter how many resources you have, it is never enough.
- There is always one more bug.
Posted by admin on February 22, 2008 under Programming |
“Hollywood Principle” – “Don’t call us; we’ll call you.“
What’s a callback? Suppose you’re taking a long trip, and I lend you my car for the purpose. Given the condition of my car, I’ll probably also hand you a sealed envelope with a telephone number in it, along with instructions to call the number in the envelope if you experience any engine problems. That’s a callback.
You do not have to know the number in advance (it may be the number of a good repair shop, a bus line, or the city dump), and in fact you may never have to call the number.

In effect, the task of handling the “engine trouble” event has been partitioned between you (also known as the “framework”) and me (also known as the “client of the framework”).
You know when it’s time to do something but not what to do. I know what to do if a particular event occurs but not when to do it. Together we make a complete application.
Callbacks are a common programming technique and have traditionally been implemented as simple pointers to functions. When a function object is used as a callback, that’s an instance of the Command pattern. A better approach is typically to use a function object rather than a function pointer. Use of a function object—or more typically a function object hierarchy—in conjunction with the Hollywood Principle is an instance of the Command pattern
– from C++ Common Knowledge: Essential Intermediate Programming By Stephen C. Dewhurst
Posted by admin on February 18, 2008 under Programming |
This is a short version of the Sed tips published at http://sed.sourceforge.net/sed1line.txt

Adjust File Spacing
# double space a file
sed G
# triple space a file
sed ‘G;G’
# insert a blank line above every line which matches “REGULAR_EXPRESSION”
sed ‘/REGULAR_EXPRESSION/{x;p;x;}’
# insert a blank line below every line which matches “REGULAR_EXPRESSION”
sed ‘/REGULAR_EXPRESSION/G’
# insert a blank line above and below every line which matches “REGULAR_EXPRESSION”
sed ‘/REGULAR_EXPRESSION/{x;p;x;G;}’
Text Conversion and Substitution
# delete leading whitespace (spaces, tabs) from front of each line
# aligns all text flush left
sed ‘s/^[ \t]*//’ # see note on ‘\t’ at end of file
# delete trailing whitespace (spaces, tabs) from end of each line
sed ‘s/[ \t]*$//’ # see note on ‘\t’ at end of file
# delete BOTH leading and trailing whitespace from each line
sed ‘s/^[ \t]*//;s/[ \t]*$//’
# insert 5 blank spaces at beginning of each line (make page offset)
sed ‘s/^/ /’
# align all text flush right on a 79-column width
sed -e :a -e ‘s/^.\{1,78\}$/ &/;ta’ # set at 78 plus 1 space
# substitute (find and replace) “FIND_TERM” with “REPLACE_TERM” on each line
sed ‘s/FIND_TERM/REPLACE_TERM/’ # replaces only 1st instance in a line
sed ‘s/FIND_TERM/REPLACE_TERM/4′ # replaces only 4th instance in a line
sed ‘s/FIND_TERM/REPLACE_TERM/g’ # replaces ALL instances in a line
sed ‘s/\(.*\)FIND_TERM\(.*FIND_TERM\)/\1REPLACE_TERM\2/’ # replace the next-to-last case
sed ‘s/\(.*\)FIND_TERM/\1REPLACE_TERM/’ # replace only the last case
# substitute “FIND_TERM” with “REPLACE_TERM” ONLY for lines which contain “baz”
sed ‘/baz/s/FIND_TERM/REPLACE_TERM/g’
# substitute “FIND_TERM” with “REPLACE_TERM” EXCEPT for lines which contain “baz”
sed ‘/baz/!s/FIND_TERM/REPLACE_TERM/g’
# change “scarlet” or “ruby” or “puce” to “red”
sed ‘s/scarlet/red/g;s/ruby/red/g;s/puce/red/g’ # most seds
gsed ‘s/scarlet\|ruby\|puce/red/g’ # GNU sed only
# join pairs of lines side-by-side (like “paste”)
sed ‘$!N;s/\n/ /’
# add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
gsed ’0~5G’ # GNU sed only
sed ‘n;n;n;n;G;’ # other seds
Selective Printing of Certain Lines
# print first 10 lines of file (emulates behavior of “head”)
sed 10q
# print first line of file (emulates “head -1″)
sed q
# print the last 10 lines of a file (emulates “tail”)
sed -e :a -e ‘$q;N;11,$D;ba’
# print the last 2 lines of a file (emulates “tail -2″)
sed ‘$!N;$!D’
# print the last line of a file (emulates “tail -1″)
sed ‘$!d’ # method 1
sed -n ‘$p’ # method 2
# print only lines which match regular expression (emulates “grep”)
sed -n ‘/REGULAR_EXPRESSIONp/p’ # method 1
sed ‘/REGULAR_EXPRESSIONp/!d’ # method 2
# print only lines which do NOT match REGULAR_EXPRESSIONp (emulates “grep -v”)
sed -n ‘/REGULAR_EXPRESSIONp/!p’ # method 1, corresponds to above
sed ‘/REGULAR_EXPRESSIONp/d’ # method 2, simpler syntax
# print the line immediately before a REGULAR_EXPRESSIONp, but not the line
# containing the REGULAR_EXPRESSIONp
sed -n ‘/REGULAR_EXPRESSIONp/{g;1!p;};h’
# print the line immediately after a REGULAR_EXPRESSIONp, but not the line
# containing the REGULAR_EXPRESSIONp
sed -n ‘/REGULAR_EXPRESSIONp/{n;p;}’
# grep for AAA and BBB and CCC (in any order)
sed ‘/AAA/!d; /BBB/!d; /CCC/!d’
# grep for AAA and BBB and CCC (in that order)
sed ‘/AAA.*BBB.*CCC/!d’
# grep for AAA or BBB or CCC (emulates “egrep”)
sed -e ‘/AAA/b’ -e ‘/BBB/b’ -e ‘/CCC/b’ -e d # most seds
gsed ‘/AAA\|BBB\|CCC/!d’ # GNU sed only
# print only lines of 65 characters or longer
sed -n ‘/^.\{65\}/p’
# print only lines of less than 65 characters
sed -n ‘/^.\{65\}/!p’ # method 1, corresponds to above
sed ‘/^.\{65\}/d’ # method 2, simpler syntax
# print section of file from regular expression to end of file
sed -n ‘/REGULAR_EXPRESSIONp/,$p’
# print section of file based on line numbers (lines 8-12, inclusive)
sed -n ’8,12p’ # method 1
sed ’8,12!d’ # method 2
# print line number 52
sed -n ’52p’ # method 1
sed ’52!d’ # method 2
sed ’52q;d’ # method 3, efficient on large files
# print section of file between two regular expressions (inclusive)
sed -n ‘/Prashant/,/Mhatre/p’ # case sensitive
Selective Deletion Of Certain Lines
# print all of file EXCEPT section between 2 regular expressions
sed ‘/Prashant/,/Mhatre/d’
# delete the first 10 lines of a file
sed ’1,10d’
# delete the last line of a file
sed ‘$d’
# delete the last 2 lines of a file
sed ‘N;$!P;$!D;$d’
# delete the last 10 lines of a file
sed -e :a -e ‘$d;N;2,10ba’ -e ‘P;D’ # method 1
sed -n -e :a -e ’1,10!{P;N;D;};N;ba’ # method 2
# delete every 8th line
gsed ’0~8d’ # GNU sed only
sed ‘n;n;n;n;n;n;n;d;’ # other seds
# delete lines matching pattern
sed ‘/pattern/d’
# delete ALL blank lines from a file (same as “grep ‘.’ “)
sed ‘/^$/d’ # method 1
sed ‘/./!d’ # method 2
# delete all leading blank lines at top of file
sed ‘/./,$!d’
Posted by admin on February 16, 2008 under Programming |
Here are some general guidelines for Unified Modeling Language Diagrams based on – The Elements of UML Style – by Scott W. Ambler
Do not use crossing lines. If you need to use crossing lines, depict them as a jump.
- No diagonal or curved lines.
- Use the same size for all symbols and arrange them symmetrically.
- Use white space in diagrams. It improves readability.
- Organize diagrams left to right, top to bottom.

- No visual noise ! Just show only what you need to show.
- Always prefer popular notation over cryptic notation.
- Split large UML diagrams into many smaller ones.
- Focus on content first, appearance second. Prefer single-page diagrams.
- Use notes to describe a UML diagram.
- Use conventions for diagram legends.
- Apply consistent, readable fonts.
- Follow effective naming conventions.
- Prefer common domain terminology in names.
- Apply specific language naming conventions on design diagrams.
- Use question marks to indicate unknowns. Use consistent colors for elements.
- Left-justify text in notes.
- Name stereotypes in <<graphical use interface>> and <<GUI>> format.
- Prefer naming conventions over stereotypes.
- Think twice before introducing a new stereotypes and use stereotypes consistently.
- Prefer notes over OCL or ASL to indicate constraints.
- Use strong verb to indicate action ! (for Use case modeling)
Posted by admin on February 9, 2008 under Programming |
Some of the most frequently VIM shortcuts I use. This is just a copy paste from my notes – so forget grammar and consistency.

|
yyp
|
duplicate line
|
|
ddp
|
swap lines
|
|
^G
|
Current line Number
|
|
guu
|
change line to lowercase
|
|
gUU
|
change line to uppercase
|
|
u
|
unlimited Undos
|
|
ctrl+r
|
unlimited Re-Do’s
|
|
‘.
|
(single quote and dot) similar to shift + F5 in MS-Word) Jump to Last modified line
|
|
`.
|
(back quote and dot) Jump to the exact point where we did last modification
|
|
*
|
Serach Forward
|
|
#
|
Search Reverse
|
Use ‘map’ to map important shortcuts:
While working on source code, sometimes I prefer to see code with line numbers, sometimes I don’t.
:map #1 :set nu^M
:map #2 :set nonu^M
Note: (^M characters are created by typing the key sequence [CTRL-V][CTRL-M]. This key sequence represents the carriage return)
Now You can Hit the following keys to toggle between source code with and without line numbers.
F1 Shows source code with line numbers
F2 Shows source code without line numbers
‘map’ is used like a ‘macro’ in MS Word /Excel. Read stuff on ‘map’ to know the magics . Read how can you put the maps in .vimrc file.
Some more programming specific shortcuts.
|
Programming Specific:
|
|
%
|
match brackets {}[]()
|
|
ga
|
Displays ascii, hex and octal value of a character under cursor
|
|
gf
|
Open file name under cursor, I prefer to spilt window before doing this
|
|
Ctrl+n /Ctrl+p
|
Word completion in insert mode
|
|
[I
|
list lines containing the word under cursor
|
|
:sp pnm.cpp
|
Opens pnm.cpp file in a split window
|
|
gD
|
To highlight all functions/Jump to function
|
|
#
|
Jump to function/subroutine
|
|
ctrl+w ctrl+w
|
Toggle within split windows
|
If you are at the beiginning of ‘/prashant/narayan/mhatrep/ok’, pressing dw will just delete ‘/’. Hit dW, It will delete complete ‘/prashant/narayan/mhatrep/ok’ word.
( beginning of previous sentence
) beginning of next sentence
{ beginning of previous paragraph
} beginning of next paragraph
20G or :20 move to line 20
G End of the file
J Join two lines (position cursor anywhere on the line)
:set ts=4 set tabstop = 4
When auto-indent is enabled (:set ai), we can use ‘ctrl+d’ to move one ident level to the left and ‘ctrl+t’ to move one ident level to the right.