Confidential © 2008 Teleca AB QT coding conventions Author: Johnny Liu Date: July 14, 2010 Version: 1.0 Teleca Chengdu.

Slides:



Advertisements
Similar presentations
Web Center Certification Sitemap / Formatting Content Web Center Certification Training Intuit Financial Services University.
Advertisements

Chapter 5: Control Structures II (Repetition)
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 3-1 Created by Cheryl M. Hughes The Web Wizards Guide to XML by Cheryl M. Hughes.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 1 The Study of Body Function Image PowerPoint
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
Tutorial 10 – Managing Long Documents
Tutorial 3 – Creating a Multiple-Page Report
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Programming Language Concepts
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Data Structures Using C++
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
ABC Technology Project
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Microsoft Access.
Vanderbilt Business Objects Users Group 1 Reporting Techniques & Formatting Beginning & Advanced.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
INTRODUCTION Lesson 1 – Microsoft Word Word Basics
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
VOORBLAD.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
1 Advanced C Programming from Expert C Programming: Deep C Secrets by Peter van der Linden CIS*2450 Advanced Programming Concepts.
© 2012 National Heart Foundation of Australia. Slide 2.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
1 of 31 Images from Africa. 2 of 31 My little Haitian friend Antoine (1985)
1 of 32 Images from Africa. 2 of 32 My little Haitian friend Antoine (1985)
INTRODUCTORY MICROSOFT WORD Lesson 7 – Working With Documents
Dr. Alexandra I. Cristea XHTML.
25 seconds left…...
Januar MDMDFSSMDMDFSSS
Chapter Three Arithmetic Expressions and Assignment Statements
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Pointers and Arrays Chapter 12
Intracellular Compartments and Transport
PSSA Preparation.
Chapter 11 Describing Process Specifications and Structured Decisions
Essential Cell Biology
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. a lesson approach Microsoft® PowerPoint 2010 © 2011 The McGraw-Hill Companies,
Chapter 8 Improving the User Interface
What’s new in WebSpace Changes and improvements with Xythos 7.2 Effective June 24,
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Java Coding Standards and Best Practices Coding Standards Introduction: After completing this chapter, you will able to keep your code up to standards.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Bill Tucker Austin Community College COSC 1315
Chapter 1: Computer Systems
CSCE-221 C++ Coding Standard/Guidelines
Presentation transcript:

Confidential © 2008 Teleca AB QT coding conventions Author: Johnny Liu Date: July 14, 2010 Version: 1.0 Teleca Chengdu

Confidential © 2008 Teleca AB 2 Have two parts about Qt coding convention 1.The official parts 2.Our defined parts(We defined this parts according to C++ coding convention)

Confidential © 2008 Teleca AB 3 QT coding conventions 1. The official parts

Confidential © 2008 Teleca AB 4 Indentation 4 spaces are used for indentation Spaces, not tabs!

Confidential © 2008 Teleca AB 5 Declaring variables Declare each variable on a separate line Avoid short (e.g. a, rbarr, nughdeget) names whenever possible Single character variable names are only okay for counters and temporaries, where the purpose of the variable is obvious. E.g. for (int i = 0 ; i < 100; ++i) Wait with declaring a variable until it is needed // Wrong int a; char *c; // Correct int iHeight char *pchNameOfThis;

Confidential © 2008 Teleca AB 6 Declaring variables Variables and functions start with a small letter. Each consecive word in a variables name starts with a capital letter //Wrong Int IBookNumber; // Correct Int iBookNumber; Avoid abbreviations // Wrong short Cntr; // Correct short shCounter;

Confidential © 2008 Teleca AB 7 Whitespace Use blank lines to group statements together where suited Always use only one blank line Always use a single space after a keyword, and before a curly brace. // Wrong if(foo){ } // Correct if (foo) { }

Confidential © 2008 Teleca AB 8 Whitespace For pointers or references, always use a single space between the type and * or &, but no space between the * or & and the variable name. char *x; const QString &strMyString; No space after a cast. Avoid C-style casts when possible. // Wrong char* pchBlockOfMemory = (char* ) malloc(data.size()); // Correct char *pchBlockOfMemory = (char *)malloc(data.size()); char *pchBlockOfMemory = reinterpret_cast (malloc(data.size()));

Confidential © 2008 Teleca AB 9 Braces As a base rule, the left curly brace goes on the same line as the start of the statement: // Wrong if (a>b) { } // Correct if (a>b) { }

Confidential © 2008 Teleca AB 10 Braces Exception: Function implementations and class declarations always have the left brace on the start of a line: static void foo(int g) { qDebug("foo: %i", g); } class Moo { };

Confidential © 2008 Teleca AB 11 Braces Use curly braces when the body of a conditional statement contains more than one line, and also if a single line statement is somewhat complex. // Wrong if (address.isEmpty()) { return false; } // Correct if (address.isEmpty()) return false;

Confidential © 2008 Teleca AB 12 Braces Exception 1: Use braces also if the parent statement covers several lines / wraps // Correct if (address.isEmpty() || !isValid() || !codec) { return false; }

Confidential © 2008 Teleca AB 13 Braces Exception 2: Use braces also in if-then-else blocks where either the if-code or the else-code covers several lines // Wrong if (address.isEmpty()) return false; else { qDebug("%s", qPrintable(address)); ++it; } // Correct if (address.isEmpty()) { return false; } else { qDebug("%s", qPrintable(address)); ++it; }

Confidential © 2008 Teleca AB 14 Braces // Wrong if (a) if (b)... else... // Correct if (a) { if (b)... else... }

Confidential © 2008 Teleca AB 15 Braces Use curly braces when the body of a conditional statement is empty // Wrong while (a); // Correct while (a) {}

Confidential © 2008 Teleca AB 16 Parentheses Use parentheses to group expressions: // Wrong if (a && b || c) // Correct if ((a && b) || c) // Wrong a + b & c // Correct (a + b) & c

Confidential © 2008 Teleca AB 17 Switch statements The case labels are on the same column as the switch Every case must have a break (or return) statement at the end or a comment to indicate that theres intentionally no break. E.g. switch (myEnum) { case Value1: doSomething(); break; case Value2: doSomethingElse(); break; default: defaultHandling(); break; }

Confidential © 2008 Teleca AB 18 Line breaks Keep lines shorter than 100 characters; insert breaks if necessary. Commas go at the end of a broken line; operators start at the beginning of the new line. The operator is at the end of the line to avoid having to scroll if your editor is too narrow. // Wrong if (longExpression + otherLongExpression + otherOtherLongExpression) { } // Correct if (longExpression + otherLongExpression + otherOtherLongExpression) { }

Confidential © 2008 Teleca AB 19 General exception Feel free to break a rule if it makes your code look bad. Notes: The attachment is the official Qt Coding Convention and QtFw for S60 coding conventions, those documents are very important, please check it.

Confidential © 2008 Teleca AB 20 Qt Coding Convention 2.Our defined parts(We defined this parts according to C++ coding convention)

Confidential © 2008 Teleca AB 21 Add file and version information At the beginning of header files and.cpp files, need to use comment make simple notes about Copyright, Function description,Version, Author,ect. Function description is a little more important, we often simplify it over or forget to update when some big changes are made. ********************************************************************* * Copyright(c)2010 Teleca company. * All rights reserved * * Name: filename.h/ filename.cpp * Description: Brief description files contents and features. * * Version: 1.0 * Author: Johnny Liu * Date: July 14, 2010 ************************************************************************/

Confidential © 2008 Teleca AB 22 Function header comment: Features, Parameters and Return value E.g: /******************************************************** * Function: Briefly describe the features of function * Parameters: param1Description * param2Description * param3Description * Return value: Briefly describe the return value ********************************************************/

Confidential © 2008 Teleca AB 23 Variables definition and comment 1)The definition of variables use Hungarian notation (The compound words or phrases in which the elements are joined without spaces, the first letter is low case and the abbreviation of variable category, the words behind are all start with upper case). The name should usePrefix + Noun or Prefix + Adj. + Noun. E.g. float fValue; int iOldValue; QString strNewValue; Important variables are need additional comments, because the name of variables usually cant show what it is completely, it need to tell developers more information.

Confidential © 2008 Teleca AB 24 Variables definition and comment The details of Hungarian notation please check the this attachment. 2)The common controls variable name use the Hungarian notation with controls abbreviation together. E.g. QGroupBox *pGboxReceive; QLabel *pLblName; QPushButton *pPbtnSend; The usual controls abbreviation are in this attachment.

Confidential © 2008 Teleca AB 25 Variables definition and comment 3)Global variables and class data members use long name, local variables use short name. The name of class data members beginning with m_, static variables beginning with s_, global variables beginning with g_. The prefix of class data members start with m_(m means member) can avoid confuse with another kinds of variables. E.g. Class Square { private: int m_iWidth;//Data member int m_iHeight; //Data member }

Confidential © 2008 Teleca AB 26 Variables definition and comment The prefix of static variables is s_(smeans static), E.g. void init(…) { static int s_iInitValue;// static variables … } If we have to use global variables, the prefix of it is g_ (means global), E.g. int g_iHowManyPeople;// global variables int g_iHowMuchMoney;// global variables Notes: We should try to avoid use global variables as we can.

Confidential © 2008 Teleca AB 27 Variables definition and comment 4)Local variables should simple and easy to understand, use common variables, E.g. nCount strName, ect. 5)In programs, if two or more classes have same abbreviation.e.g. QToolBar and QToolButton, both abbreviation are tb. Then we need to change the abbreviation in one of them, the change principle is avoid conflict and can express the mean of classes. e.g. The abbreviation of QToolBar can be change to tbar, but QToolButton still use tb for arrreviation. 6) Class name starts with the combination of words that all beginning with upper case, but the function name use camel case style. E.g. class Node;// class name class LeafNode;// class name void drawRect(void); // function name void setValue(int value); // function name The camel case style document is in this attachment.

Confidential © 2008 Teleca AB 28 Header files structure and class declaration arrangement 1) The header files consist with three parts. The version declaration at the beginning of header files. Reference the first rule. Preprocessing block.(e.g. The header files start with(#ifndef***, #define***) end with(#endif //***). Function and class declaration, ect.

Confidential © 2008 Teleca AB 29 Header files structure and class declaration arrangement 2)In class declaration, the order is: Q_OBJECT-> public-> siganls-> slots->protected-> priavte. If need to declaration some another data types(Structure,Enumerate,etc.), should put those declaration before the class declaration. If in same classes, both data variables and function declaration are use same type declaration, split both of them. E.g. private void addAll(); … private int m_iNum; … 3)At usual, if we use the Signals/Slots, the first sentence of class declaration is Q_OBJECT.

Confidential © 2008 Teleca AB 30 UI layout principle 1)When make Qt UI, use QLayout as more as you can for layout management, try to avoid use absolute coordinates, unless you are certain that the UI wont changed its coordinate forever. 2)If one area have many widgets, try to put those widgets in a window box(e.g. QWidget, QFrame,QGroupBox,etc.), then put those window boxes in the UI.

Confidential © 2008 Teleca AB 31 Debug information At first, in order to debug our program, we should add debug information in our codes. We should define a macro wrapped by QT_NO_DEBUG_OUTPUT in *.h file, E.g. #ifndef QT_NO_DEBUG_OUTPUT #define PRINT(s) qDebug(s) #else #define PRINT(s) #endif // QT_NO_DEBUG_OUTPUT

Confidential © 2008 Teleca AB 32 Enums and Constants Name of Enums and Constants are use upper case beginning words compounds. E.g. 1) enum SwitchStateType { SwitchOn, SwitchOff }; 2)enum { StateError, StateOpen, StateRunning, StateClose}; 3) const int NumberOfMaxVolume; 4)const int TopSectionHeight

Confidential © 2008 Teleca AB The End Author: Johnny Liu Date: July 14, 2010 Version: 1.0