New topic: 

RB Coding best practices advice

<http://forums.realsoftware.com/viewtopic.php?t=47454>

         Page 1 of 1
   [ 6 posts ]                 Previous topic | Next topic          Author  
Message        p0wn3d          Post subject: RB Coding best practices 
advicePosted: Sun Mar 31, 2013 10:29 am                                 
Joined: Sun Oct 28, 2012 4:54 am
Posts: 174
Location: Herts, UK                Hi Once Again 

Up until only a few months ago I knew pretty much nothing about programming. 
From a novice, hobbyist perspective real studio/Real basic IMHO is by far the 
most friendly IDE and language around today.

Whilst I am slowly getting to grips with programming I am curious as to how 
mature developers tend to structure Methods etc.. As an example I have the 
following method in one of my projects.

Dim domain,name, SMTPserver As String
domain=toAddressFld.text // text field value as "[email protected]"
name=domain.NthField("@",2) // Use NthField to get "anydomain.com"

Dim sh as new shell
sh.TimeOut =8000
sh.execute("nslookup -querytype=mx ")+name // Shell command to do DNS MX record 
lookup on "anydomain.com"
Dim MTA as string = sh.Result // Shell result example  "Non-authoritative 
answer: anydomain.com MX preference = 10, mail exchanger = smtp.anydomain.com"

Dim rg as New RegEx
Dim myMatch as RegExMatch
rg.SearchPattern= "= (?:\d+ )?([^\s]+)$" // Use Regular Expression Search 
Pattern to find SMTP server
myMatch=rg.search(MTA)

if myMatch <> Nil then
  SMTPserver =(myMatch.SubExpressionString(1)) // RegEx Match returns DNS 
server name "smtp.anydomain.com"
  ServerField.Text = (SMTPserver) // Set Server Field Text Area with RegEx 
Matched Value "smtp.anydomain.com"
else
  MsgBox("Cannot get MX Record!")
End if
exception err as RegExException
MsgBox err.message


My question would be is it good practice to code as much as possible into a 
single method or use multiple sub methods/functions and how would you improve 
the code I have posted.

Thanks - Just getting a feel for how to do things from a best practice point of 
view

     
_________________
Real Studio 2012 R2
SysInfo
BackTrack Linux/BackBox Linux/Debian Lenny/Windows 7/Windows 8/OpenWRT/OpenBSD  
                             Top                ktekinay          Post subject: 
Re: RB Coding best practices advicePosted: Sun Mar 31, 2013 10:54 am            
                     
Joined: Mon Feb 05, 2007 5:21 pm
Posts: 540
Location: New York, NY                It's about reusability, readability, and 
compartmentalization. In this case, you have a created a method for looking up 
the MX server for a given name. Very nice. But you've mixed it in with your 
interface, so if you need to do this elsewhere, you would have to recreate the 
code. At a minimum, I'd do something like this:
ServerField.Text = NSLookup( toAddressFld.Text.NthField( "@", 2 ), "mx" )
Then, in a module somewhere:
Function NSLookup (hostname As String, querytype As String = "") As String
  dim cmd as string = "nslookup "
  if querytype <> "" then
  cmd = cmd + "-querytype=" + querytype + " "
  end if
  cmd = cmd + hostname
  
  dim sh as new Shell
  sh.Timeout = 8000
  sh.Execute( cmd )
  dim MTA as string = sh.Result
  
  Dim rg as New RegEx
  Dim myMatch as RegExMatch
  rg.SearchPattern= "(?U)= (?:\d+ )?(\S+)\.?$" // Use Regular Expression Search 
Pattern to find SMTP server
  myMatch=rg.search(MTA)
  
  if myMatch <> Nil then
  return myMatch.SubExpressionString(1) // RegEx Match returns DNS server name 
"smtp.anydomain.com"
  else
  return ""
  End if
End Function

(For your convenience, I "corrected" the regex pattern based on my other post.)

Or something like this. You should decide how you want errors handled and at 
what level, but this gives the general idea. Later, elsewhere (or in another 
app), you can reuse the module and perform lookups simply by calling NSLookup( 
host, query ) or just NSLookup( host ), and you won't have to rewrite or 
recreate a single line of code. This also makes your original code more 
readable as there is no question as to what you're doing. (Also called 
"self-commenting".)

If you'd like to support more feature of nslookup, a better idea would be to 
turn it into a class, but that's a discussion for another day.      
_________________
Kem Tekinay
MacTechnologies Consulting
http://www.mactechnologies.com/

Need to develop, test, and refine regular expressions? Try RegExRX.
  
                             Top                Bob Keeney          Post 
subject: Re: RB Coding best practices advicePosted: Sun Mar 31, 2013 11:21 am   
                              
Joined: Fri Sep 30, 2005 11:48 am
Posts: 3482
Location: Lenexa, KS                Quote:My question would be is it good 
practice to code as much as possible into a single method or use multiple sub 
methods/functions

It is my belief that smaller functions are easier to maintain in the long haul. 
 Since Real Studio exceptions don't give you a line number it is very difficult 
to debug a 100 line method that generated a Nil Object Exception than say a 10 
line method.

My general rule of thumb is that if I have to scroll to see all the code in a  
method it's probably in need of refactoring.  Exceptions abound but you get the 
drift.      
_________________
Bob K.

Real Word Processing for your Real Studio Applications with Formatted Text 
Control
http://www.bkeeney.com/formatted-text-control/  
                             Top                Dale          Post subject: Re: 
RB Coding best practices advicePosted: Sun Mar 31, 2013 3:27 pm                 
                
Joined: Thu Mar 01, 2007 2:02 pm
Posts: 231
Location: Sunny (generally!) Southern California                Readability and 
clarity is very important for two reasons: 1) debugging and 2) maintenance.

Long, involved methods are generally harder to follow when debugging. Within 
reason, a series of calls to sub-methods, each appropriately named, not only 
gives a clear idea of the flow of the code but also helps to identify where 
things may be going wrong.

Consider also; trying to remember what you were thinking when you wrote the 
code a year or two ago, now that you need to go back in and either fix a bug or 
add a new feature, is easier if the code is readable.

I don't know of a single programmer who actually likes this but COMMENT - 
COMMENT - COMMENT! Put in as many comments as necessary to help anyone who may 
have to review the code (and that means you, too) to understand what the code 
is doing and why it is done that way. One company I worked at (back in the 
mainframe days) required that every method (function, procedure, etc.) and a 
comments block at the top that detailed what the routine did, what the inputs 
were and that the routine returned, if anything. When we needed to enhance the 
product 2-3 years later, it was simple to find the right place to look.

Clarity of code is far more important than the number of blank lines between 
statements or the number of spaces to indent loops.

- Dale      
_________________
-----
Real Studio 2012r1 on Windows 7 (64 bit)
-----
It has been said that politics is the second oldest profession. 
I have learned that it bears a striking resemblance to the first.
 - Ronald Reagan  
                             Top                ktekinay          Post subject: 
Re: RB Coding best practices advicePosted: Sun Mar 31, 2013 5:30 pm             
                    
Joined: Mon Feb 05, 2007 5:21 pm
Posts: 540
Location: New York, NY                There is a school of thought that says 
that you should not comment, but that your code, through method and variable 
names, should be self-commenting. The thought is that, as you comment, then 
later change your code, the comments get out-of-sync and are worse than 
useless, they are actually detrimental. If the code is self-commenting, that 
will never be an issue.

Personally, I prefer a mix.      
_________________
Kem Tekinay
MacTechnologies Consulting
http://www.mactechnologies.com/

Need to develop, test, and refine regular expressions? Try RegExRX.
  
                             Top                jjb          Post subject: Re: 
RB Coding best practices advicePosted: Sun Mar 31, 2013 6:59 pm                 
        
Joined: Sat Apr 25, 2009 4:08 am
Posts: 210                ktekinay wrote:There is a school of thought that says 
that you should not comment, but that your code, through method and variable 
names, should be self-commenting. The thought is that, as you comment, then 
later change your code, the comments get out-of-sync and are worse than 
useless, they are actually detrimental. If the code is self-commenting, that 
will never be an issue.

I agree with this. You would need to be incredibly disciplined to keep comments 
up to date.
Commenting is boring. 

If your code is too clever by half such that it's not self explanatory then 
that's a bad smell.

Having said that I appreciate a brief explanation of the purpose of a method    
  
_________________
RB 2012 Release 2.1 Personal edition (Windows)  
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 6 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to