Elegant Code Club

Forum | by Michael Zuskin


Want to improve your coding habits to make the life easier?

Dealing with tons of code written by others (as a software developer working in teams and a code reviewer), I noticed that programmers usually make the same typical mistakes. The good news: the list of those mistakes is quite short, so changing your coding style requires pretty small efforts. The ideas, described here, will dramatically decrease code complexity and make your code clearer and more understandable. That will result in quicker development and easier maintenance/enhancement. And less bugs. And easier bugs fixing.


Code elegance... Sounds nice, but is it really so important? Does it simplify the developer's work? Can it define a project's success? Can a lack of it contribute to a project's failure? All these questions have a same answer - yes. I believe that the most important component of business applications creators' work is understanding of the business, organization of data storage and processing, GUI events. But code writing style is important too because each aspect of the programmer's work (including code writing professionalism) plays a critical role. Even though coding style mistakes are looking insignificant, they are accumulating over time and, finally, quantity turns into quality. Even a small needle can blow up a big balloon!


I have seen programmers which came to development from the business sector. Usually they wrote code in a very straightforward way, without too much thinking (or even knowing?) about good coding practices. Unfortunately, I've also seen many coders of that kind who came from Computer Science background and it isnít so surprising. I myself never heard about elegant coding style in the nice days of studying Software Engineering in college. But later, dealing with complicated software forced me to think about how to write and organize code...


Only 3.5 developers were occupied in one of the most large-scale projects in my career (the PM, one other developer and me + sometimes fourth guy joined us to help before deadlines). The main reason for our high effectiveness was the use of a good framework library (so we thought mostly about "what" to develop - not "how", in other words - wrote minimum of technical code concentrating on business issues). And the second reason - the application was written using many rules described here. All applications in that company had elegant code, controlled by very strict and even ruthless code reviewers. And on the other hand, I took part in development of much simpler (from the user's perspective) applications with much more people involved. Whole IT departments were struggling with tons of hopelessly knotted code... In fact, these projects helped to keep developers unemployment low have given life to this site.


The ideas are illustrated with multiple code examples in C# and PowerBuilder (abbreviated as PB), sometimes in PL/SQL and T-SQL. Why? There is a very sound reason for that: I worked with them, ha-ha! Examples in C# are also addressed to C, C++, Java, and Objective-C programmers. PowerBuilder code examples are in fact written in PowerScipt - the programming language used to code in PowerBuilder IDE. Sintactically, PowerScipt is a dialect of Basic, so those examples will be very clear to VB developers.


The Art of Naming: Exact meaning
Consistency
Long, descriptive names
Abbreviations
Exact action timing
Naming convention

Code Writing Style: Over-indenting is the enemy number one
Loops with CONTINUE
No loop condition duplicating
Avoid complex nested expressions
Store returned values in variables
Process impossible options (defensive programming)
Application codes
/* Comments */
Private and public affairs in instance variables
Write RETURN in the end of void methods

Managing Functions: Keep functions simple
Code blocks must be short
Code after validations
Refactor to prevent duplication
Private and public affairs in functions
Law of Demeter (or Principle of Least Knowledge)

Intriguing World of Logic: Boolean flags naming
Positive wording in Boolean names
Positive comparison in Boolean expresssions
Comparing ONE value with MANY others
Short conditions in IFs
Boolean functions
Populating Boolean variables
Don't return null from Boolean functions
De Morgan's Laws
Short-circuit evaluation

Ocean of Databases: Only surrogate primary keys
Simple SQL
WHERE clause
OUTER JOIN: only LEFT
Mention fields names in INSERT statement (SQL Server)
Temp table in stored proc (SQL Server)
Existence checking
Avoid client-side SQL (Data Sentinel Layer)
Debugging T-SQL stored procedures without debugger

Next stuff is only for PowerBuider developers only:

Elegant Code: Power Building with Exceptions
Local Variables Declaration
CHOOSE CASE TRUE for a series of validations
Indentation in CHOOSE CASE
Change objects' default names
Technical code vs. business code
Return policy
Use REF keyword
Constants used all over the application
Constants serving local needs
Null constants
Change default access level of class members
Don't DESTROY objects
No global variables and functions
Avoid SQL
Don't duplicate conditions in Boolean expressions
Try to avoid DYNAMIC calls
Access inter-objects parameters by name
Avoid row-by-row scanning
Don't implement functionality in built-in events

Tips and Tricks: DW Spy
Passing parameters between objects
iif()
iin() + global functions overloading
nvl()
Oracle packaged procedure as DW data source
How to overcome long-circuit evaluation
Populate Code Tables from constants
Convert DW to... zebra!
Save DataStore from debugger
Copy data between DWs/DSs
Check if column/row has REALLY been modified
uf_row_exist() to replace Find()
uf_filter() to replace Filter()
uf_set_row_status() to replace SetItemStatus()
uf_lookup_display() to obtain DropDown's display value
uf_destroy() to POST objects' Destroy
uf_get_pb_version() to report PowerBuilder's version
uf_replace_all() to replace all occurrences of substring
uf_in_clause_from_array() to build SQL's IN clause dynamically
uf_ds_from_sql() to create DataStore by dynamic SELECT
uf_ds_from_array() to convert array to DataStore
uf_value_exists_in_array() to check if array has a value
uf_add_unique_element_to_array()
uf_get_field_label()
uf_display_in_center() to group buttons in the middle
uf_show_columns() to show only certain colums in DW

IF you want to ((lose weight) OR (have unbelievable (brain function AND mental clarity))) THEN click:


Professional code inspectors at work:





Copyright © PowerBuilder.us * Michael Zuskin

Since Feb 17, 2013

free counters

eXTReMe Tracker