GitHub: Where the world builds software · GitHub



2246630-400685The Golden Rules00The Golden RulesNamesUse descriptive namesmax_wait_time_in_seconds, iso3166tab.LanguagePrefer object orientation over imperative programmingI.e. classes over functions and reportsPrefer functional over procedural language constructsE.g. index += 1 or index = index + 1Instead of ADD 1 to indexCommentsExpress yourself in code, not in commentsDelete code instead of commenting itFormattingBe consistentOptimize for reading, not for writingConstantsUse constants instead of magic numbersE.g. typekind_date instead of 'D'TablesUse the right table typeHASHED: large, filled at once, never modified, read oftenSORTED: large, always sorted, filled over time or modified, read oftenSTANDARD: small, array-likeBooleansUse XSDBOOL to set Boolean variablesempty = xsdbool( itab IS INITIAL )ConditionsTry to make conditions positiveIF has_entries = abap_true.Consider decomposing complex conditionsDATA(example_provided) = xsdbool(…)IF example_provided = abap_true AND one_example_fits = abap_true.IfsKeep the nesting depth lowELSE. IF <other>. ELSE. IF <something>.Regular expressionsConsider assembling complex regular expressionsCONSTANTS classes …CONSTANTS interfaces …… = |{ classes }|{ interfaces }|.Classes: Object orientationPrefer objects to static classesPrefer composition over inheritanceDATA delegate TYPE REF TOCLASS a DEFINITION INHERITING FROMDon’t mix stateful and stateless in the same classClasses: ScopeMembers PRIVATE by default, PROTECTED only if neededTesting: PrinciplesWrite testable codeThere are no tricks to writing tests, there are only tricks to writing testable code. (Google)Enable others to mock youCLASS my_super_object DEFINITION. INTERFACES you_can_mock_this.Readability rulesgiven_some_data( ).do_the_good_thing( ).and_assert_that_it_worked( ).Test classesCall local test classes by their purposeCLASS unit_testsCLASS tests_for_the_class_under_testCode under testTest interfaces, not classesDATA cut TYPE REF TO some_interfaceDATA cut TYPE REF TO some_classInjectionUse test seams as temporary workaroundThey are not a permanent solution!Don’t misuse LOCAL FRIENDS to invade the tested codeCLASS unit_tests LOCAL FRIENDS cut.cut->db_reader = stub_db_readerTest MethodsTest methods names: reflect what’s given and expectedMETHODS accepts_emtpy_user_inputMETHODS test_1Use given-when-thengiven_some_data( ).do_the_good_thing( ).assert_that_it_worked( ).“When” is exactly one callgiven_some_data( ).do_the_good_thing( ).and_another_good_thing( ).assert_that_it_worked( ).AssertionsFew, focused assertionsassert_not_initial( itab ).assert_equals( act = itab exp = exp ).Use the right assert typeassert_equals( act = itab exp = exp ).assert_true( itab = exp ).Assert content, not quantityassert_contains_message( key )assert_equals( act = lines( messages ) exp = 3 ).Assert quality, not contentassert_all_lines_shorter_than( … )-47865529858Clean ABAP00Clean ABAPMethods: Object orientationPrefer instance to static methodsMETHODS aCLASS-METHODS aPublic instance methods should be part of an interfaceINTERFACES the_interface.METHODS aMethods: Method bodyDo one thing, do it well, do it onlyDescend one level of abstractiondo_something_high_level ( ).DATA(low_level_op) = |a { b }|.Keep methods small3-5 statements, one page, 1000 linesMethods: Parameter numberAim for few IMPORTING parameters, at best less than threeMETHODS a IMPORTING b c d eSplit methods instead of adding OPTIONAL parametersMETHODS a IMPORTING bMETHODS c IMPORTING dMETHODS x IMPORTING b DRETURN, EXPORT, or CHANGE exactly one parameterMETHODS do_it EXPORTING a CHANGING bError handling: Return codesPrefer exceptions to return codesMETHODS check RAISING EXCEPTIONMETHODS check RETURNING resultDon’t let failures slip throughDATA(result) = check( input )IF result = abap_false.Error handling: ExceptionsExceptions are for errors, not for regular casesUse class-based exceptionsMETHODS do_it RAISING EXCEPTIONMETHODS do_it EXCEPTIONSError handling: ThrowingThrow one type of exceptionMETHODS a RAISING EXCEPTION b c dThrow CX_STATIC_CHECK for manageable situationsRAISE EXCEPTION no_customizingThrow CX_NO_CHECK for usually unrecoverable situationsRAISE EXCEPTION db_unavailableError handling: CatchingWrap foreign exceptions instead of letting them invade your codeCATCH foreign INTO DATA(error). RAISE EXCEPTION NEW my( error ). RAISE EXCEPTION error. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download