1 / 51

Coding For People

Coding For People. Martyn Gigg, Tessella 7 th March 2013. About Me. MPhys at Warwick: 2000 – 2004 PhD in Physics: 2004 – 2008 Tessella: 2008 - ? Mantid - Data Reduction & Analysis in collaboration with Rutherford Appleton Laboratory UK & Oak Ridge National Lab USA.

swann
Télécharger la présentation

Coding For People

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Coding For People Martyn Gigg, Tessella 7th March 2013

  2. About Me • MPhys at Warwick: 2000 – 2004 • PhD in Physics: 2004 – 2008 • Tessella: 2008 - ? • Mantid - Data Reduction & Analysis in collaboration with Rutherford Appleton Laboratory UK & Oak Ridge National Lab USA

  3. About Tessella – www.tessella.com • Services based software company

  4. Tessella Employees Typically have a degree in a numerate discipline Many have PhDs in a similar field Most junior members of technical staff are recent graduates Required to keep skills up to date using 150 hours a year (~20 days) training budget

  5. FUSION Research Project Building a fusion Tokamakin the South of France

  6. Pharmaceutical – Drug Development • Analysis of clinical trials data and adaptive dose allocation using Bayesian statistics

  7. Government Research - Mantid Project

  8. Hardware vs Software

  9. Hardware vs Software • Software is just as important as hardware • Tendency to skimp on software, i.e. post doc written • Good software requires processes • Software Engineering • Requires trained people just like other disciplines

  10. So far … • Variety of Technologies • Variety of Domains • Lots of Different and Interesting Challenges

  11. Introducing Change • How does a business keep on making money? • Software changes to meet business needs • Further development → Reponse to change • One side = ability to track change • Version control • A solved problem

  12. How easy is code to change? Machines don’t read (as in understand)/write code We spend more time reading than writing code Hard to understand  Hard to change Hard to change  Costs more

  13. Writing code for people

  14. Making Changes

  15. Making Changes

  16. Documentation

  17. Documents • Requirements • Design/Architecture • Test Scripts • Installation Guide • Manual • Maintenance Guide

  18. Comments // We need to check if the approver of the // document still has permission to see it Document doc = gateway.GetDocumentById(documentId); PermissionSet perm = new PermissionSet(user, doc); // Changed by John (August) return perm.CanView();

  19. Comments /* ********************************** * r23 21-Jan-2009 John * Created class * * r23 12-Feb-2009 Bob * Added Co-Author property * * r23 01-Mar-2009 Dave * Created class * * r23 21-June-2010 John * Implemented new Workflow * * **************************************/ public class Document {

  20. Comments publicclassWordLimitValidator{  public int wordLimit;  public bool IsWithinWordLimit(Document d)  {           return document.WordCount <= this.wordLimit;  }} publicclassClass1{// Declare the max integer Xpublicint x; // Checks the documentpublicbool Check(Document d)  {// Keeps a running count, start at 0int t = 0;// Loop over the sectionsfor (int i = 0; i < d.Sections.Count; i++)    {// Loop over the paragraphs in the sectionfor (int j = 0; j < d.Sections[i].Paras.Count; j++)      {// Increment the total count        t += d.Sections[i].Paras[j].Words.Count;      }    }// Check that t is less than or equal to xreturn t <= x;  }}

  21. Readability

  22. Re-Factoring

  23. Readability Do not use booleans as arguments! StringCreateFile(Stringfilename, BooleanoverwriteExisting, BooleankeepOpen); CreateFile("file.csv",true, false); CreateFile("file.csv",CreateMode.OverwriteExisting, LockMode.KeepOpen); StringCreateFile(Stringfilename,CreateModefileMode,LockModelockMode);

  24. Readability Does it fit on one screen? AndAlso (document.Author.Department.Head = currentUser.Manager OrElse version.

  25. Readability Does your code describe the domain? Public Float Class String Document Integer Public DateTime Author String Version Class Document XmlDocument XmlDocument Signature

  26. Smells

  27. Smells • Duplicated code: identical or very similar code exists in more than one location. • Long method: a method, function, or procedure that has grown too large. • Large class: a class that has grown too large. See God object. • Feature envy: a class that uses methods of another class excessively. • Inappropriate intimacy: a class that has dependencies on implementation details of another class. • Refused bequest: a class that overrides a method of a base class in such a way that the contract of the base class is not honoured by the derived class. See Liskov substitution principle.

  28. Lazy class / Freeloader: a class that does too little. • Contrived complexity: forced usage of overly complicated design patterns where simpler design would suffice. • Excessively long identifiers: in particular, the use of naming conventions to provide disambiguation that should be implicit in the software architecture. • Excessive use of literals: these should be coded as named constants, to improve readability and to avoid programming errors.

  29. A small detour: Testing • Everyone tests their code • Refactoring should leave functionality intact • Good automated tests ensure this • One of the keys to continuing success is good test coverage and also good feedback from automated tests • Modern approaches such as Test Driven Development encourage testing as part of development

  30. Testing & Reuse

  31. Testing & Reuse • Unmanned but cost ~$370 million

  32. Patterns

  33. Null Object void NotifyDepartmentHead(IPerson person) { if (person.Section != null && person.Section.Division != null && person.Section.Division.Department != null && person.Section.Division.Department.Manager != null) { if (! string.IsNullOrEmpty( person.Section.Division.Department.Manager.EmailAddress)) { SendEmail(person.Section.Division.Department.Manager.EmailAddress); } } }

  34. Null Object public class NullDivision : IDivision { public IDepartment Department { get { return new NullDepartment(); } } public IPerson Manager { get { return new NullPerson() } } } public class NullSection : ISection { public IDivision Division { get { return new NullDivision(); } } public IPerson Manager { get { return new NullPerson(); } } }

  35. Null Object void NotifyDepartmentHead(IPerson person) { String address = person.Section.Division.Department.Manager.EmailAddress; if (!String.IsNullOrEmpty(address)) SendMail(address); }

  36. Factory Pattern

  37. Adapters and Decorators

  38. Conclusion Software needs to change, so it must be EASY to change The CODE is always right Write your code so it is easy to understand You can use patterns to solve common problems Agile methodologies embrace change

  39. Suggested Reading • 97 Things Every Programmer Should Know [O’Reilly] • Patterns of Enterprise Application Architecture, by Martin Fowler • Clean Code: A Handbook of Agile Software Craftsmanship by Robert Martin • Head First Design Patterns, by Eric T Freeman, Elisabeth Robson, Bert Bates, Kathy Sierra • Java Puzzlers: Traps, Pitfalls, and Corner Cases, by Joshua Bloch, Neal Gafter

  40. function GetPosString(position) { switch (position) { case 0: return "1st"; case 1: return "2nd"; case 2: return "3rd" ; default: return (position + 1) + "th"; } }

  41. if (!$email){ global $Email; $email=$Email; if (!$email){ global $Email_Address; $email=$Email_Address; if (!$email){ global $email_address; $email=$email_address; if (!$email){ global $EmailAddress; $email = $EmailAddress; if (!$email){ global $emailaddress; $email = $emailaddress; if (!$email){ global $EMailAddress; $email = $EMailAddress; } } } } } }

  42. public class Document { // Creates a new document object that is not yet in the database public Document(String id, int version) { // ... } // Loads a document object from the database public Document(int version, String id) { // ... }

  43. onreadystatechange = function(){ switch(httpReq.readyState){ case 0: if(httpReq.readyState == 0){ break; } case 1: if(httpReq.readyState == 1){ break; } case 2: if(httpReq.readyState == 2){ break; } case 3: if(httpReq.readyState == 3){ break; } case 4: if(httpReq.readyState == 4){ if(httpReq.status == 200){ var val = httpReq.responseText; alert(httpReq.responseText) dataInsert(val); break; } else{ alert("Error "+httpReq.status); break; } } } };

  44. // assign variables logFile.info("START: Assigning variables."); logFile.debug("Getting particular drug information."); // gets drugs brandNameDrugs = Utils.formatDrugList(dl.getDrugsByLabel(calculateBean.getDrugName())); logFile.debug("Getting major classifications."); // get all major classifications (usage) majorClassifications = cl.getMajorClassifications(); logFile.debug("Getting classifications."); // get all classifications classifications = cl.getClassifications(); logFile.debug("Getting all classifications."); // gets all the major classifications and classifications allClassifications = cl.getAllClassifications(); logFile.info("END: Assigning variables."); // assign variables to session for search.jsp logFile.info("START: Assigning variables to the session."); logFile.debug("Storing brandNameDrugs to the session."); request.setAttribute("brandNameDrugs", brandNameDrugs); logFile.debug("Storing majorClassifications to the session."); request.setAttribute("majorClassifications", majorClassifications); logFile.debug("Storing classifications to the session."); request.setAttribute("classifications", classifications); logFile.debug("Storing allClassifications to the session."); request.setAttribute("allClassifications", allClassifications); logFile.info("END: Assigning variables to the session.");

  45. // "true" or "false" public static string Bool2Str(bool b) { switch(b) { case true: return System.Boolean.TrueString; case false: return System.Boolean.FalseString; default: return "error"; } }

  46. Public Shared Sub isValidQuery(ByVal query As String) ‘… End Sub

  47. ‘'To Get date from SAP XMl as they are providing date like 20090330        Public Shared Function Getformateddate(ByVal strDate As String) As Date            Dim month As String = strDate.Substring(4, 2)            Dim year As String = strDate.Substring(0, 4)            Dim day As String = strDate.Substring(6, 2)            Dim tempdate As Date = Convert.ToDateTime(day + "/" + month + "/" + year)            Return CDate(tempdate.ToShortDateString)        End Function

  48. ''çatch exception in case the date is null not correct format.            Try                If strDate.Trim.Length = 8 AndAlso IsNumeric(strDate) Then                    Dim month As String = strDate.Substring(4, 2)                    Dim year As String = strDate.Substring(0, 4)                    Dim day As String = strDate.Substring(6, 2)                    Dim tempdate As Date = New Date(CInt(year), CInt(month), CInt(day))                    Return CDate(tempdate.ToShortDateString)                End If            Catch ex As Exception                Return Nothing            End Try

  49. DimuserAsIPerson = icp.ContentDispatcher.userDimsuperAsBoolean= user.IsDBManager            user.setAsDbManager() ‘ Snip IfNotsuperThen                user.removeAsDbManager()EndIf

More Related